(Android)Fragment的基础入门学习笔记---创建一个简单的fragment

Fragment入门学习笔记

简介

Fragment是Android3.0后引入的一个新的API,他出现的初衷是为了适应大屏幕的平板电脑, 当然现在他仍然是平板APP UI设计的宠儿,而且我们普通手机开发也会加入这个Fragment, 我们可以把他看成一个小型的Activity,又称Activity片段!想想,如果一个很大的界面,我们 就一个布局,写起界面来会有多麻烦,而且如果组件多的话是管理起来也很麻烦!而使用Fragment 我们可以把屏幕划分成几块,然后进行分组,进行一个模块化的管理!从而可以更加方便的在 运行过程中动态地更新Activity的用户界面!另外Fragment并不能单独使用,他需要嵌套在Activity 中使用,尽管他拥有自己的生命周期,但是还是会受到宿主Activity的生命周期的影响,比如Activity 被destory销毁了,他也会跟着销毁!

注意!

  • Fragment是依赖于Activity的,不能独立存在的。、
  • 一个Activity里可以有多个Fragment。
  • 一个Fragment可以被多个Activity重用。
  • Fragment有自己的生命周期,并能接收输入事件。
  • 我们能在Activity运行时动态地添加或删除Fragment。

生命周期

在这里插入图片描述

基本使用

创建一个fragment

public class AFragment extends Fragment {

    private TextView mTvTitle;

	//构造方法,并利用bundle从Activity获取初始化数据
    public  AFragment(String title){
        Bundle bundle = new Bundle();
        bundle.putString("title",title);
        setArguments(bundle);
    }
    
    @Nullable
    @Override
    //绑定布局文件
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    	//这里inflate方法的第三个参数为false,否则会重复向container添加布局,报错
        View view = inflater.inflate(R.layout.fragment_a,container,false);
        return view;
    }
    
    //布局文件绑定后初始化控件
    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
;        mTvTitle = view.findViewById(R.id.tv_title);
		if ( getArguments()!=null) {
            mTvTitle.setText(getArguments().getString("title"));
        }

    }

}

以上是创建一个基本的fragment过程,其中onCreateView和onViewCreated方法来完成初始化的操作,值得一提的是,fragment添加到Activity有俩种方式:1.静态添加;2.动态添加,区别在于静态添加是直接将我们创建的fragmen写入我们需要添加的Activity的XML配置文件中,而动态添加相对灵活,可以在运行时删除。这里,我们以动态添加的方式,ContainerActivity代码如下:

public class ContainerActivity extends AppCompatActivity {

    AFragment aFragment;
    private TextView mTvTitle;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_container);
        mTvTitle = findViewById(R.id.tv_title);
        //实例化fragment
        aFragment = new AFragment("初始化标题");
        //把AFragment添加到activity中。记得调用commit
        getSupportFragmentManager().beginTransaction().add(R.id.fl_container,aFragment).commitNowAllowingStateLoss();
    }
}

注意:

  • 由于是动态添加fragment,需要在activity的XML文件中添加一个frameLayout用于存放fragment,之后利用fragmentManager来动态添加我们写的fragment。
  • 可以利用addToBackStack(null)方法使FragmentManager具有和类似activity的回退栈,达到可以打开多个fragment,退回键返回上一个fragment,否则返回键摧毁activity。
  • 如果你使用了addToBackStack(null)方法,小心当回退的时候,会重新初始化fragment并调用它的onCreateView方法,解决办法是利用hide与add方法防止fragment重建。具体示例如下:
if (bFragment == null) {
                   bFragment = new BFragment();
               }
               //添加到返回栈 addtobackstack()
               Fragment fragment = getFragmentManager().findFragmentByTag("a");
               if (fragment != null) {
                   //此处代码逻辑:由于直接replace到返回栈中会导致返回时重新执行onCreateView方法,fragment重新初始化,hide()后在添加到返回栈保持状态。
                   getFragmentManager().beginTransaction().hide(fragment).add(R.id.fl_container, bFragment).addToBackStack(null).commitNowAllowingStateLoss();
               } else {
                   getFragmentManager().beginTransaction().replace(R.id.fl_container, bFragment).addToBackStack(null).commitNowAllowingStateLoss();
               }

参考学习:《Android Fragment 非常详细的一篇》

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值