Fragment简单复用Dome

    xiaoxiao2021-03-25  139

    原因:一般多个相同的界面,可以重复利用。一般是多个fragment用相同的布局,为了节省时间使用

    1:在Activity或Fragment中定义一个List

     fragmentList.add(Fragments.newInstance());//Fragments 类

    newInstance()//是一个静态的方法,主是返回复用的fragment 的对象实例,也可以传递参数。

    2:定义一个fragment

     public static Fragment newInstance(){         Fragments fragments=new Fragments();

           Bundle args = new Bundle();

           fragment.setArguments(args);

            return fragments;     }

     public void initView() {         Bundle bundle = getArguments();     }

    在复用的fragment中使用Bundle 对象传递,使用setArguments,getArguments方法传递和接受。

    转载请注明原文地址: https://ju.6miu.com/read-7190.html

    最新回复(0)