安卓Fragment的用法

一、程序圖


二、佈局文件

(1)main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <FrameLayout
        android:id="@+id/contain"
        android:layout_width="fill_parent"
        android:layout_height="20px"
        android:layout_weight="9" >
    </FrameLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="20px"
        android:layout_weight="1"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/fg_one_btn"
            android:layout_width="20px"
            android:layout_height="fill_parent"
            android:layout_margin="3px"
            android:layout_weight="1"
            android:text="First" />

        <Button
            android:id="@+id/fg_two_btn"
            android:layout_width="20px"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:layout_margin="3px"
            android:text="Second" />

        <Button
            android:id="@+id/fg_three_btn"
            android:layout_width="20px"
            android:layout_height="fill_parent"
            android:layout_margin="3px"
            android:layout_weight="1" 
            android:text="Three"/>
    </LinearLayout>

</LinearLayout>

  (2) first.xml

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="fill_parent"
    android:layout_width="fill_parent"
    android:background="#123"
    android:gravity="center"
    android:orientation="vertical">
    
    <TextView 
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="FirstFragment"/>
    
    <Button 
        android:id="@+id/hello"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="點擊"/>
    
</LinearLayout>

 (3) second.xml

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="fill_parent"
    android:layout_width="fill_parent"
    android:background="#000"
    android:gravity="center">
    
    <TextView 
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="SecondFragment"
        android:textColor="#fff"/>
    
    
</LinearLayout>

(4) three.xml

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="fill_parent"
    android:layout_width="fill_parent"
    android:background="#bd0708"
    android:gravity="center">
    
    <TextView 
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="ThreeFragment"/>
    
    
</LinearLayout>

三、程序

(1)FragmentActivity.java


import com.liu.cn.fragment.FirstFragment;
import com.liu.cn.fragment.SecondFragment;
import com.liu.cn.fragment.ThirdFragment;

import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class FragmentActivity extends Activity implements OnClickListener{
   Button one,two,three;
   LayoutInflater inflater;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        inflater=(LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        init();
    }
    public View getLayoutView(int layoutId){
    	View v=inflater.inflate(layoutId, null);
    	return v;
    }
    private void init(){
    	one=(Button) this.findViewById(R.id.fg_one_btn);
    	two=(Button) this.findViewById(R.id.fg_two_btn);
    	three=(Button) this.findViewById(R.id.fg_three_btn);
    	one.setOnClickListener(this);
    	two.setOnClickListener(this);
    	three.setOnClickListener(this);
    	normalColor();
    	//初始化第一個fragment
    	FirstFragment ff=new FirstFragment();
    	switchView(ff, one);
    }
    //正常顏色
    private void normalColor(){
    	one.setBackgroundColor(Color.BLUE);
    	two.setBackgroundColor(Color.BLUE);
    	three.setBackgroundColor(Color.BLUE);
    }
    //fragment之間的轉換,並且改變對應的按鈕顏色
    private void switchView(Fragment fg,View v){
    	v.setBackgroundColor(Color.GRAY);
    	getFragmentManager().beginTransaction().
    		replace(R.id.contain, fg).commit();
    }
	public void onClick(View v) {
		normalColor();
		int id=v.getId();
		switch (id) {
		case R.id.fg_one_btn:
			FirstFragment ff=new FirstFragment();
			switchView(ff, v);
			break;
		case R.id.fg_two_btn:
			SecondFragment sf=new SecondFragment();
			switchView(sf, v);
			break;
		case R.id.fg_three_btn:
			ThirdFragment tf=new ThirdFragment();
			switchView(tf, v);
			break;
		default:
			break;
		}
	}
}
(2)   BaseFragment .java


import com.liu.cn.R.layout;

import android.R.integer;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

//定義一個抽象基類BaseFragment,當3個fragment之間有公用的方法時,可把方法寫在這裏,其他fragment繼承
//Basefragment,方法可以之間調用,好處就在這兒,用的久了就體現出來了
public abstract class BaseFragment extends Fragment{

	private FragmentActivity activity;
	public  View LayoutView;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//強轉,調用fragmentActivity中的getLayoutView方法;
		activity=(FragmentActivity) getActivity();
		//下面倆行不能顛倒,否則空指針錯誤,原因是找不到佈局文件
		LayoutView=activity.getLayoutView(getLayoutId());
		createChild(savedInstanceState);
	}
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return LayoutView;
	}
	public abstract void createChild( Bundle savedInstanceState);
	public abstract int getLayoutId();
}
(3)  FirstFragment.java


import com.liu.cn.BaseFragment;
import com.liu.cn.R;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
//在找佈局文件中的組件時,可按下面的按鈕示例初始化,並做其他的事情,其他的fragment初始化都一樣
//在這裏就不多囉嗦了,剩下的倆個fragment中你們可以練習下
public class FirstFragment extends BaseFragment{

	@Override
	public void createChild(Bundle savedInstanceState) {
		Button firstBtn=(Button) LayoutView.findViewById(R.id.hello);
		firstBtn.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				Toast.makeText(getActivity(),
						"點擊了第一個Framgment中的按鈕", 0).show();
			}
		});
	}
	@Override
	public int getLayoutId() {
		// TODO Auto-generated method stub
		return R.layout.first;
	}
}

(4) Secondfragment.java

import android.os.Bundle;

import com.liu.cn.BaseFragment;
import com.liu.cn.R;

public class SecondFragment extends BaseFragment{

	@Override
	public void createChild(Bundle savedInstanceState) {
		
	}

	@Override
	public int getLayoutId() {
		// TODO Auto-generated method stub
		return R.layout.second;
	}

}

(5) ThirdFragment,java


import android.os.Bundle;

import com.liu.cn.BaseFragment;
import com.liu.cn.R;

public class ThirdFragment extends BaseFragment {

	@Override
	public void createChild(Bundle savedInstanceState) {
		
	}

	@Override
	public int getLayoutId() {
		// TODO Auto-generated method stub
		return R.layout.three;
	}

}




發佈了30 篇原創文章 · 獲贊 10 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章