Fragment基础

1定义:碎片(Fragment)是一种可以嵌入在活动当中UI片段,能让程序更加合理和充分的利用大屏幕空间。

2.碎片的生命周期

  

  *碎片的状态:

    1.运行状态:碎片可见时,它所关联活动处于运行状态时,该碎片也处于运行状态。

    2.暂停状态:当活动进入暂停状态时,所关联碎片就进入暂停状态。

    3.停止状态:当活动进入停止状态时,所关联的碎片就进入停止状态,或通过调用FragmentTransaction的remove()、replace()

       方法将该碎片从活动中移除,但如果在事务提交之前调用addToBackStack(),此时该碎片也会进入停止状态。(对用户完全           不可见,有可能被系统回收)

    4.销毁状态:活动被销毁时,所依附的碎片也进入到销毁状态,或通过调用FragmentTransaction的remove()、replace()方法           将该碎片从活动中移除,但如果在事务提交之前没有调用addToBackStack(),此时该碎片也会进入销毁状态。

  *相比较ac,fragment多出的回调方法:

        onAttach:当碎片和活动建立关联的时候调用。

        onCreateView:为碎片创建视图时调用。(加载布局)

        onActivityCreated:与该碎片相关联的活动创建完成时调用。

        onDestroyView:与碎片关联的视图移除时调用。

        onDetach:碎片和活动解除关联的时候调用。

4.碎片和活动之间的通信

  *活动中调用碎片的方法

    调用getFragmentManager().findFragmentById得到活动中相应的实例,然后即可调用碎片的方法。

 *碎片中调用活动的方法

    调用getActivity()得到和碎片相关联的ac实例,然后就可以调用相应的方法。

4.使用实例

  2.1新建left_fragment,right_fragment布局

left_fragment布局
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent">
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="Button"/>
</LinearLayout>

right_fragment布局
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#00ff00">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:textSize="20sp"
        android:text="this is right fragment"/>
</LinearLayout>

  新建LeftFragment,rightFragment类

public class LeftFragment extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.left_fragmnet,container,false);
        return view;
    }
}

public class RightFragment extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.right_fragment,container,false);
        return view;
    }
}

activity_main布局

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <fragment
        android:id="@+id/left_fragment"
        android:layout_width="0dp"
        android:name="com.wsw.recycledemo.fragment.LeftFragment"
        android:layout_height="match_parent"
        android:layout_weight="1"/>
    <fragment
        android:id="@+id/right_fragment"
        android:layout_width="0dp"
        android:name="com.wsw.recycledemo.fragment.RightFragment"
        android:layout_height="match_parent"
        android:layout_weight="1"/>
</LinearLayout>

  2.2动态添加实例

  新建another_right_fragment布局

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#ffff00">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:textSize="20sp"
        android:text="this is another right fragment"/>
</LinearLayout>

  新建AnotherRightFragment类

public class AnotherRightFragment extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.another_right_fragment,container,false);
        return view;
    }
}

  activity_main布局

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <fragment
        android:id="@+id/left_fragment"
        android:layout_width="0dp"
        android:name="com.wsw.recycledemo.fragment.LeftFragment"
        android:layout_height="match_parent"
        android:layout_weight="1"/>
    <FrameLayout
        android:id="@+id/right_layout"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"/>
</LinearLayout>

  MainActivity代码

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.actvity_main1);
        Button button=findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                replaceFragment(new AnotherRightFragment());
            }
        });
    }

    private void replaceFragment(Fragment fragment){
        FragmentManager fragmentManager=getFragmentManager();
        FragmentTransaction transaction=fragmentManager.beginTransaction();
        transaction.replace(R.id.right_layout,fragment);
        transaction.addToBackStack(null);
        transaction.commit();

    }
}

  动态添加碎片主要分为五步:

  1.创建待添加的碎片实例。

  2.获取FragmentManager。

  3.调用beginTransaction()开启事务。

  4.调用replace()添加或替换碎片。

  5.调用commit提交事务。

4.在碎片中模拟返回栈(作用:按back键返回上一个碎片)

  提交事务之前调用addToBackStack()方法。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值