BennyKok
BennyKok

Reputation: 809

Android get TabView from TabLayout after using setupWithViewPager

I am lazy so I use this instead of creating different TabView

    v = new myTabContent(c);//my custom viewpager
    t = new TabLayout(c);
    t.setupWithViewPager(v);

I wonder if there is any ways to get all the TabView from the TabLayout, because I want to set longclicklistener to each TabView.

Upvotes: 0

Views: 2705

Answers (2)

Vahe Gharibyan
Vahe Gharibyan

Reputation: 5693

            TabLayout.Tab tab =  tabLayout.newTab();

            LinearLayout linearLayout = (LinearLayout) LayoutInflater.from(TagsManagerActivity.this).inflate(R.layout.item_tab, null);
            ((TextView)linearLayout.findViewById(R.id.text1)).setText(tagsManagerTab.getTagNameRes());
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
                ((ImageView)linearLayout.findViewById(R.id.icon)).setImageDrawable(getResources().getDrawable(tagsManagerTab.getIconRes(),TagsManagerActivity.this.getTheme()));
            else
                ((ImageView)linearLayout.findViewById(R.id.icon)).setImageDrawable(getResources().getDrawable(tagsManagerTab.getIconRes()));

            tab.setCustomView(linearLayout);
            tabLayout.addTab(tab);

Upvotes: 0

Nikola Despotoski
Nikola Despotoski

Reputation: 50578

TabView essentially is a HorizontalScrollView that hosts a SlidingTabStrip a LinearLayout for each tab.

Simple iteration over the SlidingTabStrip will give you the TabViews:

    LinearLayout tabStrip = (LinearLayout) tabLayout.getChildAt(0);
    for (int i = 0; i < tabStrip.getChildCount(); i++) {
        View tabView = tabStrip.getChildAt(i);
        if (tabView != null){
          //do something with the TabView
        }
    }
}

However this has to be performed after TabLayout has successfully been laid out.

Upvotes: 4

Related Questions