美文网首页
十分钟学会Fragment复用

十分钟学会Fragment复用

作者: 崽子猪 | 来源:发表于2019-06-04 16:09 被阅读0次

废话不多说我们直接上代码

我们首先创建两个Fragment

FragmentA布局需要写Tab栏与ViewPager对应

<LinearLayout android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
tools:context="com.example.weekday02demo3.view.fragment.ProjectFragment"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <!-- TODO: Update blank fragment layout -->

    <android.support.design.widget.TabLayout
        android:id="@+id/tab"
        ***app:tabMode="scrollable"***
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <android.support.v4.view.ViewPager
        android:id="@+id/vp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</LinearLayout>

随后我们需要给tab栏赋值,我们这边是通过解析玩安卓接口获取的数据
// 项目分类 http://www.wanandroid.com/project/tree/json
// 项目列表数据 http://www.wanandroid.com/project/list/1/json?cid=294

我们首先在FragmentB界面定义一个方法来获取数据里所对应的ID[将FragmentA与FragmentB建立连接]

 public static Fragment isListFragment(int id){

        ListFragment listFragment = new ListFragment();

        Bundle bundle = new Bundle();
        bundle.putInt("id",id);
        listFragment.setArguments(bundle);

        return listFragment;
    }

然后回到FragmentA界面通过For循环把Id值发送到FragmentB然后通过Bean类给Tab栏赋值

ProjectBean projectBean = gson.fromJson(data, ProjectBean.class);
        List<ProjectBean.DataBean> projectTabList = projectBean.getData();

        ArrayList<Fragment> fragments = new ArrayList<>();

        if ( projectBean != null && projectTabList != null && projectTabList.size() > 0 ){

            for (ProjectBean.DataBean dataBean : projectTabList) {

                Fragment listFragment = ListFragment.isListFragment(dataBean.getId());
                fragments.add(listFragment);

            }

            FragmentAdapterTwo fragmentAdapterTwo = new     FragmentAdapterTwo(getChildFragmentManager(), fragments, projectTabList);
            vp.setAdapter(fragmentAdapterTwo);
            tab.setupWithViewPager(vp);

        }

FragmentA的适配器[FragmentAdapterTwo]

  public FragmentAdapterTwo(FragmentManager fm, ArrayList<Fragment> fragments, List<ProjectBean.DataBean> projectTabList) {
        super(fm);
        this.fragments = fragments;
        this.projectTabList = projectTabList;
    }

    @Override
    public Fragment getItem(int position) {
        return fragments.get(position);
    }

    @Override
    public int getCount() {
        return fragments.size();
    }

    @Nullable
    @Override
    public CharSequence getPageTitle(int position) {
        return projectTabList.get(position).getName();
    }

回到FragmentB界面获取对应的ID解析页面数据
在onCreate()方法里获取Id

 Bundle arguments = getArguments();
        id = arguments.getInt("id");

接下来就是RecycleView绑定适配器的操作了,本片Fragment复用到这里就结束了希望对大家有所帮助

相关文章

  • 十分钟学会Fragment复用

    废话不多说我们直接上代码 我们首先创建两个Fragment FragmentA布局需要写Tab栏与ViewPage...

  • Fragment复用

    Activit页面 //for循环遍历title集合for (int i = 0; i < list; i++) ...

  • Fragment复用

    1· 主fragment或者主activity里面定义一个数组与titles 2·for循环遍历添加到复用frag...

  • android :fragment 复用

    1.先来看看fragment的代码 [java]view plaincopy importjava.util.Ar...

  • Android零基础入门第86节:探究Fragment生命周期

    一个Activity可以同时组合多个Fragment,一个Fragment也可被多个Activity 复用。Fra...

  • 带你从源码理解Fragment机制

    相信大家平时用到很多Fragment, 手机平板的代码共用,各种东西的复用,用Fragment也很方便。如今Fra...

  • 碎片fragment

    代码为了复用,多写在fragment里,以备需求到处改变位置,比如首页的fragment,希望从单独入口进入。所需...

  • Fragment里面嵌套Fragment的处理

    如图,首页是一个fragment,热门,Android,ios这些是一个被多次复用的fragment,并且使用vi...

  • Fragment传值小结

    fragment的传值 如果某个fragment需要用很多次,则需要复用,然后就是单例的干货 就是长下面这样,如果...

  • Adapter的泛型

    宗旨:GetView方法放在具体的Activity/Fragment里面实现,其他的均可以复用 MainActivity

网友评论

      本文标题:十分钟学会Fragment复用

      本文链接:https://www.haomeiwen.com/subject/dmqrxctx.html