Android 使用 setupWithViewPager 后从 TabLayout 获取 TabView
Android get TabView from TabLayout after using setupWithViewPager
我很懒所以我用这个而不是创建不同的 TabView
v = new myTabContent(c);//my custom viewpager
t = new TabLayout(c);
t.setupWithViewPager(v);
我想知道有没有办法从TabLayout中获取所有的TabView,因为我想为每个TabView设置longclicklistener。
TabView
本质上是一个 HorizontalScrollView
,它为每个选项卡托管一个 SlidingTabStrip
和一个 LinearLayout
。
对 SlidingTabStrip
的简单迭代将为您提供 TabView
s:
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
}
}
}
然而,这必须在 TabLayout
成功布置后执行。
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);
我很懒所以我用这个而不是创建不同的 TabView
v = new myTabContent(c);//my custom viewpager
t = new TabLayout(c);
t.setupWithViewPager(v);
我想知道有没有办法从TabLayout中获取所有的TabView,因为我想为每个TabView设置longclicklistener。
TabView
本质上是一个 HorizontalScrollView
,它为每个选项卡托管一个 SlidingTabStrip
和一个 LinearLayout
。
对 SlidingTabStrip
的简单迭代将为您提供 TabView
s:
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
}
}
}
然而,这必须在 TabLayout
成功布置后执行。
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);