带有多个片段的Tablayout选项卡

时间:2016-09-13 06:50:11

标签: android android-fragments android-tablayout

我正在开发一个使用带有5个标签的标签布局的应用。我需要在每个选项卡中导航多个片段,并在用户再次重新选择选项卡时打开第一个片段。请帮帮我。

 TabLayout tabLayout = (TabLayout) findViewById(R.id.tabLayout);
        tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.scene_tab_selector));
        tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.my_scene_tab_selector));
        tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.conversation_tab_selector));
        tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.notification_tab_selector));
        tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.more_tab_selector));
        tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

        final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
        final PagerAdaptor adapter = new PagerAdaptor
                (getSupportFragmentManager(), tabLayout.getTabCount());
        viewPager.setAdapter(adapter);
        viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
        tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                viewPager.setCurrentItem(tab.getPosition());
                tab.getIcon().setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_IN);
                if(tab.getPosition()==4)
                {
//                    pageTitleLabel.setText("My Profile");
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                tab.getIcon().setColorFilter(Color.parseColor("#a8a8a8"), PorterDuff.Mode.SRC_IN);

            }

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


            }
        });

我的PagerAdaptor如下

public class PagerAdaptor extends FragmentStatePagerAdapter {
    int mNumOfTabs;


    public PagerAdaptor(FragmentManager fm, int NumOfTabs) {
        super(fm);
        this.mNumOfTabs = NumOfTabs;

    }

    @Override
    public Fragment getItem(int position) {

        switch (position) {
            case 0:
                Tab1ContainerFragment tab1 = new Tab1ContainerFragment();
                return tab1;
            case 1:
                MyCuddllFragment tab2 = new MyCuddllFragment();
                return tab2;
            case 2:
//                CuddllConversationFragment tab3 = new CuddllConversationFragment();
                ConversationFragment conversationFragment = new ConversationFragment();
                return conversationFragment;

            case 3:
//                CuddllNotificationsFragment tab4 = new CuddllNotificationsFragment();
                Tab4ContainerFragment notificationPageFragment = new Tab4ContainerFragment();
                return notificationPageFragment;

            case 4:
                MyProfileFragment tab5 = new MyProfileFragment();
                return tab5;

            default:
                return null;
        }
    }

    @Override
    public int getCount() {
        return mNumOfTabs;
    }



}

1 个答案:

答案 0 :(得分:0)

 public void onTabSelected(TabLayout.Tab tab) {

            tab.getIcon().setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_IN);
            if(tab.getPosition()==0)
            {
               viewpager.setCurrentItem(0);
            }else if(tab.getPosition()==1)
            {
              viewpager.setCurrentItem(1);
            }else if(tab.getPosition()==2)
            {
              viewpager.setCurrentItem(2);
            }else if(tab.getPosition()==3)
            {
              viewpager.setCurrentItem(3);
            }


        }