Dynamically add and remove tabs in TabLayout(material design) android

后端 未结 5 875
自闭症患者
自闭症患者 2020-12-13 03:53

I have a TabLayout and inside that I have ViewPager. I need to dynamically add and remove tab in tablayout(material design). I can able to add the tabs dynamically but while

5条回答
  •  醉话见心
    2020-12-13 04:38

    Remove tab from TabLayout

    ...
    public void removeTab(int position) {
        if (mTabLayout.getTabCount() >= 1 && position

    Add a removeTabPage method to your PagerAdapter

    ...
    public void removeTabPage(int position) {
        if (!tabItems.isEmpty() && position

    Add a Tab

    ...
    private void addTab(String title) {
            mTabLayout.addTab(mTabLayout.newTab().setText(title));
            mPagerAdapter.addTabPage(title);
    }
    ...
    

    Add a addTabPage method to your PagerAdapter

    ...
    public void addTabPage(String title) {
          tabItems.add(title);
          notifyDataSetChanged();
    }
    ...
    

    Check out this sample code for a full example: ...samples/SupportDesignDemos/src/com/example/android/support/design/widget/TabLayoutUsage.java

提交回复
热议问题