安卓Fragment与生命周期

本文详细介绍了安卓开发中的Fragment组件,包括其基本概念、如何在Activity中使用Fragment实现动态界面切换,以及Fragment的生命周期等关键技术点。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

部分摘自http://blog.youkuaiyun.com/lmj623565791/article/details/37970961/

安卓Fragment与生命周期

Fragment简介

    Fragment意为片段,顾名思义就是可以作为Activity的一部分。

    针对各式各样的屏幕尺寸,Fragment可以灵活的布局,让用户有更好的体验。

    Fragment为Activity界面的一个组成部分,Activity可以由多个不同的Fragment组成,Fragment可以由自己的生命周期与接收、处理用户事件,这样就不必为Activity写一堆控件代码了,Fragment可以动态的添加、移除、替换。

    

Fragment的使用

    实现像手机QQ那样的布局。

    Fragment可以像手机QQ那样实现联系人、消息、动态的三个板块互相转换。

    先在主Activity中写一个Fragment,然后写三个按钮,在写3个Fragment的XML文件,把Fragment板块依附到主Activity的Fragment板块上面就可以了。

    

主Activity

    <FrameLayout
        android:layout_weight="1"
        android:id="@+id/flconten"
        android:background="#fff"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </FrameLayout>
    <LinearLayout
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:orientation="horizontal"
        >
        <Button
            android:id="@+id/btn1"
            android:layout_weight="1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="消息"
            />
        <Button
            android:id="@+id/btn2"
            android:layout_weight="1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="联系人"
            />
        <Button
            android:id="@+id/btn3"
            android:layout_weight="1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="动态"
            />
    </LinearLayout>

    

消息XML文件

	<TextView
	    android:layout_width="wrap_content"
	    android:layout_height="wrap_content"
	    android:text="消息"
	    android:textSize="30dp"
	    />

联系人XML文件跟消息一样,动态无XML文件,使用Java代码代替。

    

    在Java代码中MainActivity,初始化时获取Fragment管理者的支持,开启新事务,添加一个Fragment片段。

    

		fm = getSupportFragmentManager();
		transaction = fm.beginTransaction();
		transaction.add(R.id.flconten, msgFragment);
		transaction.commit();

	private FragmentTransaction transaction;

	private FragmentManager fm;

运行后效果是这样


    

按钮的监听

开启新事务,然后替换掉旧的Fragment

	public void onClick(View v) {
		int id = v.getId();
		switch (id) {
		case R.id.btn1:
			transaction = fm.beginTransaction();
			transaction = transaction.replace(R.id.flconten, msgFragment);
			transaction.commit();
			break;
		case R.id.btn2:
			transaction = fm.beginTransaction();
			transaction.replace(R.id.flconten, contactFragment);
			transaction.commit();
			break;
		case R.id.btn3:
			transaction = fm.beginTransaction();
			transaction.replace(R.id.flconten, doFragment);
			transaction.commit();
			break;
		default:
			break;
		}
	}

    

消息与联系人的Fragment关联到了XML文件

public class MsgFragment extends Fragment {
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// 打气筒动态去关联一个布局文件
		View inflate = inflater.inflate(R.layout.msg_fragment, null);
		return inflate;
	}

}

动态的Fragment没关联到XML文件,用代码自动生成Fragment

    

public class DoFragment extends Fragment {
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		TextView textView = new TextView(getActivity());
		textView.setText("动态");
		textView.setTextSize(30);
		return textView;
	}
}

最终效果图


    

Fragment的生命周期

    


    

把所有的方法都写上到ContactFragment,一一测试,然后输出的顺序


    

    Fragment必须是依附到Activity上的,所以Activity的生命周期会直接影响到Fragment的生命周期。

    若Activity是暂停状态,所有的Fragment都是暂停状态,若Activity是Stop状态,所有的Fragment都不能被启动,若Activity被销毁,那么所有的Fragment都会被销毁。

    当Fragment进行转换操作,把Fragment放到Activity中的back stack中,用户就可以进行返回操作了。

    

onAttach(Activity)
当Fragment与Activity发生关联时调用。
onCreateView(LayoutInflater, ViewGroup,Bundle)
创建该Fragment的视图
onActivityCreated(Bundle)
当Activity的onCreate方法返回时调用
onDestoryView()
与onCreateView想对应,当该Fragment的视图被移除时调用
onDetach()
与onAttach相对应,当Fragment与Activity关联被取消时调用
注意:除了onCreateView,其他的所有方法如果你重写了,必须调用父类对于该方法的实现,



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值