TabLayout 使用自定义视图更新选项卡内容 [英] TabLayout update tab content with a custom view
问题描述
我正在使用新材料设计的 TabLayout
并且我遇到了一个问题,我无法在创建选项卡后更新自定义视图的选项卡内容:
I'm using TabLayout
of the new material design and i have a problem, i can't update tab content of a custom view once the tab is created:
我可以使用 PagerAdapter 简化我的方法
I can simplify my method inside my PagerAdapter with
public View setTabView(int position, boolean selected) {
View v = LayoutInflater.from(context).inflate(R.layout.default_tab_view, null);
tv = (TextView) v.findViewById(R.id.tabTextView);
if(selected)
tv.setText("SELECTED");
else
tv.setText("UNSELECTED");
return v;
}
在活动中,我可以通过以下方式简化我的代码:
And in activity i can simplify my code with:
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
ViewPager pager = (ViewPager) findViewById(R.id.viewpager);
PagerAdapter adapter = new PagerAdapter(getSupportFragmentManager());
pager.setAdapter(adapter);
tabLayout.setupWithViewPager(pager);
for (int i = 0; i < tabLayout.getTabCount(); i++) {
boolean isFirstTab = i == 0;
TabLayout.Tab tab = tabLayout.getTabAt(i);
View v;
v = adapter.setTabView(i, isFirstTab);
v.setSelected(isFirstTab);
tab.setCustomView(v);
}
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
adapter.setTabView(tab.getPosition(), true);
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
adapter.setTabView(tab.getPosition(), false);
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
标签的标题在应用启动时设置正确,但当我更改标签时,内容仍然保持不变.
The tabs' titles are set right when the app starts but when i change tab, the content still remains the same.
推荐答案
好的我认为这是android设计支持库v22的错误.
因为你说的是内容的变化,但我不能改变文本的颜色.创建选项卡后,如果您更改布局,它将不会反映.
Because You are talking about the changes in content but I can't change the color of Text. Once the tab are created and if you change the layout it will not reflecting.
正如我所见,您正在使用 setCustomView 提供自定义布局.为了更改文本,您再次调用方法 setTabView().取而代之的是方法 getCustomView() 以便您可以更改布局.TabLayout.Tab.getCustomView 中有一个方法,但它没有标识符,我已经报告了这个错误.
And As i have seen your code you are giving the custom layout using setCustomView. and for changing a text you are calling again the method setTabView(). Instead of that there should be method getCustomView() so that you can change the layout. There is a method in a TabLayout.Tab.getCustomView but it doesn't have identifier and I have report this bug.
https://code.google.com/p/android/问题/详细信息?id=177492
[2015 年 8 月 7 日更新]
最终 bug 被 android bug source traking 接受并标记为 Future Release .所以我们可以说未来的库中将不再存在该错误.我们可以有方法 getCustomView()
以便我们可以轻松地获得我们的自定义视图.
Finally bug is accepted by android bug source traking and marked as Future Release . So we can say that bug will no more exist on future library.
and we can have method getCustomView()
so that we can easily get our custom view.
[2015 年 8 月 18 日更新]
终于在 v23 支持库 中解决了这个错误.只需使用 SDK 管理器更新支持库,您就可以公开 getCustomView().
Finally the bug is resolved Released in the v23 support libs . Just update support library using SDK manager and you have getCustomView() as a public.
只需在 gradle 中更改这一行
just change this line in gradle
compile 'com.android.support:design:23.0.0'
并确保编译和目标 sdk 设置为 23.
and make sure compile and target sdk set to 23.
请检查我的代码是否适合我
please check my code working fine for me
TabLayout.Tab tab=tabLayout.getTabAt(position);
View view=tab.getCustomView();
TextView txtCount= (TextView)
view.findViewById(R.id.txtCount);
txtCount.setText(count+"");
这篇关于TabLayout 使用自定义视图更新选项卡内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!