安卓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;
	}

}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值