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

后端 未结 5 866
自闭症患者
自闭症患者 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:40

    With the new support library (I'm using 23.2.1) you should only add to the Viewpager and not the TabLayout (otherwise you end up with double tabs and other funky behavior). So to update TouchBoarder's answer:

    Add a removeTabPage method to your PagerAdapter

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

    Add a addTabPage method to your PagerAdapter

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

提交回复
热议问题