2016-09-13 117 views
-2

我正在使用帶有5個選項卡的選項卡布局的應用程序。我需要在每個選項卡內導航多個片段,並在用戶再次選擇該選項卡時打開第一個片段。請幫幫我。帶多個片段的Tablayout選項卡

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; 
    } 



} 
+0

請你能告訴你嘗試過這麼遠嗎?你也可以提到你在實現上面所述的問題 –

+0

我試着查看尋呼機和尋呼機適配器。並在每個選項卡中加載第一個片段。但現在問題是在每個選項卡中遍歷片段 –

+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); 
      } 


     } 
+0

它不能幫助我達到標籤的第一頁。 –

+0

我們可以在臉書上聯繫 –

+0

@VinayakMishra我做了一個修改。現在試試。抱歉,我寫這些時太累了。 –

相關問題