• 微信公众号:美女很有趣。 工作之余,放松一下,关注即送10G+美女照片!

Tablayout+ViewPager+Fragment 实现页面切换

互联网 diligentman 6天前 8次浏览

项目也快上线了,就来总结下Tablayout+ViewPager+Fragment 的使用啦

主activity

public class DoSomethingAct extends BaseActivity {


    @BindView(R.id.tooBarTitleTv)
    TextView tooBarTitleTv;
    @BindView(R.id.tabLayout)
    TabLayout tabLayout;
    @BindView(R.id.viewPager)
    ViewPager viewPager;
    FourDetailAdapter adapter;
    private ArrayList<Fragment> fragmentList = new ArrayList<>();//fragmentList

    @Override
    protected int getLayoutId() {
        return R.layout.xm_spfbqk_act;
    }

    @Override
    public void initImmersionBar() {
        super.initImmersionBar();
        ImmersionBar.with(this)
                .statusBarDarkFont(true)
                .applySystemFits(true)
                .init();
    }

    @Override
    protected void initView() {
        super.initView();
        String [] titleArry = null;
        tooBarTitleTv.setText("我的办件");
        getSwipeBackLayout().setEnableGesture(false);
        fragmentList.add(new MyMessageScheduleFragment(0));
        fragmentList.add(new MyMessageScheduleFragment(1));
        titleArry = new String[]{"资质办件","项目办件"};
        setGroupFragment(titleArry);
    }

    private void setGroupFragment(String[] titleArry) {
        adapter = new FourDetailAdapter(getSupportFragmentManager());
        adapter.setFragments(fragmentList);
        viewPager.setAdapter(adapter);
        tabLayout.setupWithViewPager(viewPager);//让tablayout与viewpager建立关联关系

        for (int i =0;i<titleArry.length;i++){
            setTabItem(titleArry,i);
        }
        //tablayout选中状态的一些UI更新
        tabLayout.addOnTabSelectedListener(new TabLayout.BaseOnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                View view = tab.getCustomView();
                TextView textView = view.findViewById(R.id.tv);
                textView.setTextColor(getResources().getColor(R.color.f3474C6));
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                View view = tab.getCustomView();
                TextView textView = view.findViewById(R.id.tv);
                textView.setTextColor(getResources().getColor(R.color.f969597));
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        chooseFirst();
    }
    private void chooseFirst() {
        TabLayout.Tab tabAt = tabLayout.getTabAt(0);
        View view = tabAt.getCustomView();
        TextView textView = view.findViewById(R.id.tv);
        textView.setTextColor(getResources().getColor(R.color.f3474C6));

    }
    private void setTabItem(String[] titleArry,int i) {
        View view = LayoutInflater.from(this).inflate(R.layout.zz_wyb_tablayout_title_items, null);
        TextView textView = (TextView) view.findViewById(R.id.tv);
        textView.setText(titleArry[i]);
        tabLayout.getTabAt(i).setCustomView(view);
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

}
FragmentPagerAdapter
/**
 * tablayout相关adapter
 */
public class FourDetailAdapter extends FragmentPagerAdapter {
    private List<Fragment> mFragmentList;
    public void setFragments(ArrayList<Fragment> fragments) {
        mFragmentList = fragments;
    }

    public FourDetailAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        Fragment fragment = mFragmentList.get(position);
        return fragment;
    }

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

    @Override
    public CharSequence getPageTitle(int position) {
        return super.getPageTitle(position);
    }
}

另一个fragment就不放啦

 


程序员灯塔
转载请注明原文链接:Tablayout+ViewPager+Fragment 实现页面切换
喜欢 (0)