我想动态地将标签添加到TabLayout
。我可以将不同的对象添加为不同的自定义视图。
问题是我必须动态添加各种数量的自定义视图,如果可能的话,我不想为每个标签创建view
的不同实例。
TabLayout mTabLayout = (TabLayout) findViewById(R.id.tabs);
TextView textView = (TextView)getLayoutInflater ().inflate ( R.layout.simple_text_layout,null,false );
textView.setText ( "HIM" );
mTabLayout.addTab(mTabLayout.newTab().setCustomView ( textView).setTag ( "0" ));
TextView textView1 = (TextView)getLayoutInflater ().inflate ( R.layout.simple_text_layout,null,false );
textView1.setText ( "HER" );
mTabLayout.addTab(mTabLayout.newTab().setCustomView ( textView1).setTag ( "1" ));
textView1 = (TextView)getLayoutInflater ().inflate ( R.layout.simple_text_layout,null,false );
textView1.setText ( "OTHERS" );
mTabLayout.addTab(mTabLayout.newTab().setCustomView ( textView1).setTag ( "2" ));
TabLayout mTabLayout = (TabLayout) findViewById(R.id.tabs);
TextView textView = (TextView)getLayoutInflater ().inflate ( R.layout.simple_text_layout,null,false );
textView.setText ( "HIM" );
mTabLayout.addTab(mTabLayout.newTab().setCustomView ( textView).setTag ( "0" ));
TextView textView1 = (TextView)getLayoutInflater ().inflate ( R.layout.simple_text_layout,null,false );
textView1.setText ( "HER" );
mTabLayout.addTab(mTabLayout.newTab().setCustomView ( textView1).setTag ( "1" ));
textView1 = (TextView)getLayoutInflater ().inflate ( R.layout.simple_text_layout,null,false );
textView1.setText ( "OTHERS" );
mTabLayout.addTab(mTabLayout.newTab().setCustomView ( textView1).setTag ( "2" ));
textView1.setText ( "ACCESS.." );
mTabLayout.addTab(mTabLayout.newTab().setCustomView ( textView1).setTag ( "3" ));
如果我将上述项目附加到我的tabLayout中,我会得到这个结果。
有人可以通过尝试为具有更改值的tabLayout的多个自定义视图使用相同的视图对象来指出我在做什么错误吗?
答案 0 :(得分:0)
我使用过SmartTabLayout。您可以根据自己的需要进行定制。
初始化布局:
viewPager.setOffscreenPageLimit(tabContent.size());
final SmartTabLayout viewPagerTab = (SmartTabLayout) view.findViewById(R.id.viewpagertab);
setUpTab(viewPagerTab, viewPager.getContext());
viewPagerTab.setViewPager(viewPager);
然后,
创建标签列表。
List<String> tabContent= new ArrayList<>();
SetupTab功能下面:
private void setUpTab(final SmartTabLayout layout, Context context) {
final LayoutInflater inflater = LayoutInflater.from(context);
layout.setCustomTabView(new SmartTabLayout.TabProvider() {
@Override
public View createTabView(ViewGroup container, int position, PagerAdapter adapter) {
@SuppressLint("InflateParams") View view = inflater.inflate(R.layout.custom_tab, null);
TextView customtextView = (TextView) view.findViewById(R.id.txtCustomTab);
String tab= tabContent.get(position);
customtextView.setText(tab);
return customtextView;
}
});
}
设置寻呼机适配器:
pagerAdapter = new TabPagerAdapter(getChildFragmentManager(), tabContent);
viewPager.setAdapter(pagerAdapter);
TabPagerAdapter类
public class TabPagerAdapter extends FragmentStatePagerAdapter {
List<String> list;
private Context context;
public TabPagerAdapter(FragmentManager fm, List<String> list) {
super(fm);
this.list = list;
}
public void setContext(Context context) {
this.context = context;
}
@Override
public Fragment getItem(int position) {
return setListItems(position);
}
private Fragment setListItems(int pos) {
YourCustomFragment fragment = new YourCustomFragment();
fragment.setData(list.get(pos));
return fragment;
}
@Override
public int getCount() {
return list.size();
}
public TabPagerAdapter setTitle(boolean hasTitle) {
return this;
}
}