Fragment和TabLayout使用

效果图:




一、使用Tablayout,首先需要在项目中加入Design包

dependencies {
  compile 'com.android.support:design:24.1.1'
}

二、在activity_main.xml布局文件中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical"
  tools:context="zhengliang.com.tablayout.MainActivity">
  <android.support.design.widget.TabLayout
    android:id="@+id/tab"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorPrimary"
    >
  </android.support.design.widget.TabLayout>
  <android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1" />
</LinearLayout>

里面一个Tablayout和一个ViewPager就行了

三、MainAndroid里java代码

public class ZhiBoFragment extends BaseFragment {
    private HomePersenter mshouYFragment;

    private ImageView image_my;
    private TabLayout tab_layout;
    private ViewPager vp;
    private View view;
    private List<String> list;
    private List<Fragment> fragmentList;

    @Override
    protected void loadData() {
        mshouYFragment = new HomePersenter(Urls.BASEURL4, this,PandaZhibBt.class);
        mshouYFragment.start();
    }


    protected void initView(View view) {
        image_my = (ImageView) view.findViewById(R.id.image_my);
        tab_layout = (TabLayout) view.findViewById(R.id.tab_layout);
        vp = (ViewPager) view.findViewById(R.id.vp);
        setUserVisibleHint(true);
        image_my.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), PersonalActivity.class);
                startActivity(intent);
            }
        });


    }

    @Override
    protected int getFragmentLayoutId() {
        return R.layout.fragment_zhi_bo;

    }

    @Override
    public void onSucess(Object o) {
            if (o instanceof PandaZhibBt){

                list = new ArrayList<>();
                fragmentList = new ArrayList<>();
                List<PandaZhibBt.TablistBean> tablist = ((PandaZhibBt) o).getTablist();

                for (int i = 0; i < tablist.size(); i++) {
                    list.add(tablist.get(i).getTitle());
                }
                fragmentList.add(new NewFragment());
                for (int i = 0; i < tablist.size() - 1; i++) {
                    fragmentList.add(new FuYonFragment(Urls.BASEURLS[i]));
                }

                MyPandaZhB myPandaZhB = new MyPandaZhB(getChildFragmentManager(), list, fragmentList);
                tab_layout.setupWithViewPager(vp);
                vp.setAdapter(myPandaZhB);
            }
    }

    @Override
    public void onFaile(String msg) {

    }

    @Override
    public void setPresenter(HomePersenter presenter) {
        this.mshouYFragment=presenter;
    }
}

我们还需要写一个适配器让ViewPager和TabLayout关联

public class MyAdapterFreg extends FragmentPagerAdapter {
    private List<String> list;
    private List<Fragment> fragmentList;

    public MyAdapterFreg(FragmentManager fm, List<String> list, List<Fragment> fragmentList) {
        super(fm);
        this.list = list;
        this.fragmentList = fragmentList;
    }

    public Fragment getItem(int position) {
        return fragmentList.get(position);
    }

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

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

阅读更多
文章标签: Android
上一篇Android Fragment 介绍
下一篇Android Activity生命周期
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭