TabLayout使用自定义视图更新选项卡内容 [英] TabLayout update tab content with a custom view

查看:88
本文介绍了TabLayout使用自定义视图更新选项卡内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用新材料设计的 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/issues/detail?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.

[更新18-08-2015]

最后,该错误已解决.已在 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'

并确保将compile和target 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆