1

在我的應用程序(材料設計)中,我想顯示(說)10個選項卡。在這些標籤中,標籤條應該是例如4.現在標籤欄顯示每個標籤有兩個字母的所有標籤標題。 如何創建滑動標籤頁?所以我可以滾動標籤條到正確的標籤? 我知道有一個定製的標籤條,我想盡可能使用標準設施。Android - 材料設計:帶滑動標籤的標籤條(標題)

1 - 安裝的標籤尋呼機:

private void setupTabPager() { 
    viewPager = (ViewPager) findViewById(R.id.pager); 
    final PagerAdapter adapter = new PagerAdapter(getSupportFragmentManager(), tabLayout.getTabCount()); 
    viewPager.setAdapter(adapter); 
    viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout)); 
    tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { 
     @Override 
     public void onTabSelected(TabLayout.Tab tab) { 
      // UserFeedback.show("Tab selected: " + tab.getText()); 
      viewPager.setCurrentItem(tab.getPosition()); 
     } 
     @Override 
     public void onTabUnselected(TabLayout.Tab tab) { 
     } 
     @Override 
     public void onTabReselected(TabLayout.Tab tab) { 
     } 
    }); 
} 

2 - 安裝的標籤佈局:

private void setupTablayout() { 
    tabLayout = (TabLayout) findViewById(R.id.tabLayout); 
    if (tabLayout == null) { 
     return; 
    } 
    // tabLayout.setTabGravity(TabLayout.GRAVITY_FILL); 
    tabLayout.setTabGravity(TabLayout.MODE_SCROLLABLE); 
    tabLayout.addTab(tabLayout.newTab().setText("Folders")); 
    tabLayout.addTab(tabLayout.newTab().setText("Items")); 

3 - 的PageAdapter延伸FragmentStatePagerAdapter:

public class PagerAdapter extends FragmentStatePagerAdapter { 
    int mNumOfTabs; 
    public PagerAdapter(FragmentManager fm, int NumOfTabs) { 
     super(fm); 
     this.mNumOfTabs = NumOfTabs; 
    } 
    @Override 
    public Fragment getItem(int position) { 
     switch (position) { 
      case MainActivity.FRAGMENT_GROUPS_AND_ITEMS: 
       return new FragmentGroupsAndItems(); 
      case MainActivity.FRAGMENT_FOLDERS: 
       return new FragmentFolders(); 
    .... } 

回答

1

爲什麼不你把標籤放在Horizo​​ntalScrollView裏面嗎?

它可能會幫助你。