【发布时间】:2018-09-23 10:35:17
【问题描述】:
我想知道,我有一个用于 TabLayout 中的选项卡的 custom_tab_layout,它只有一个带有 id 的 TextView,但是我想要做的是,当它被单击/选择时,所述 TextView 的文本会改变它的颜色,这个下一个代码有效,但是我不确定这是否是实现此功能的正确方法,因为我是 android 新手,感觉必须有更好的方法。这是一些 cmets 的代码!我只是使用 ViewPager 并创建了一个扩展 FragmentPagerAdapter 的自定义适配器类。
public class MainActivity extends AppCompatActivity{
public static class CategoryPagerAdapter extends FragmentPagerAdapter{
private static int NUM_PAGES = 4;
public CategoryPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position){
case 0:{
return new NumbersFragment();
}
case 1:{
return new FamilyFragment();
}
case 2:{
return new ColorsFragment();
}
case 3:{
return new PhrasesFragment();
}
default:{
return null;
}
}
}
@Override
public int getCount() {
return NUM_PAGES;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_pager);
TabLayout tlCategoryTab = this.findViewById(R.id.tlCategoryTab);
final ViewPager vpPager = this.findViewById(R.id.vpPager);
tlCategoryTab.addTab(tlCategoryTab.newTab().setCustomView(R.layout.custom_tab_layout));
tlCategoryTab.addTab(tlCategoryTab.newTab().setCustomView(R.layout.custom_tab_layout));
tlCategoryTab.addTab(tlCategoryTab.newTab().setCustomView(R.layout.custom_tab_layout));
tlCategoryTab.addTab(tlCategoryTab.newTab().setCustomView(R.layout.custom_tab_layout));
String [] categories = {"Numbers", "Family", "Colors", "Phrases"};
for (int i = 0; i < categories.length; i++) {
TabLayout.Tab tab = tlCategoryTab.getTabAt(i);
//Here I get the custom tab layout
View v = tab.getCustomView();
//Assign the TextView and the Tab name
TextView tvTabItem = v.findViewById(R.id.tvTabItem);
tvTabItem.setText(categories[i]);
//I was having issues when the app first started because it wasn't
//getting assigned the first color, so I came up with this kinda lame solution
if(i == 0){
tvTabItem.setTextColor(getResources().getColor(R.color.category_numbers));
tab.setCustomView(v);
}
}
CategoryPagerAdapter myAdapter = new CategoryPagerAdapter(this.getSupportFragmentManager());
vpPager.setAdapter(myAdapter);
vpPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tlCategoryTab));
//And here is the actual functionality that changes the TextColor when it gets clicked/selected
tlCategoryTab.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
int colorToUse = 0;
View v = tab.getCustomView();
TextView tvTabItem = v.findViewById(R.id.tvTabItem);
if(tvTabItem.getText().toString().equalsIgnoreCase("Numbers")){
colorToUse = R.color.category_numbers;
}
else if(tvTabItem.getText().toString().equalsIgnoreCase("Family")){
colorToUse = R.color.category_family;
}
else if(tvTabItem.getText().toString().equalsIgnoreCase("Colors")){
colorToUse = R.color.category_colors;
}
else if(tvTabItem.getText().toString().equalsIgnoreCase("Phrases")){
colorToUse = R.color.category_phrases;
}
tvTabItem.setTextColor(getResources().getColor(colorToUse));
tab.setCustomView(v);
vpPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
View v = tab.getCustomView();
TextView tvTabItem = v.findViewById(R.id.tvTabItem);
tvTabItem.setTextColor(getResources().getColor(R.color.white));
tab.setCustomView(v);
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
有没有办法以更有效的方式实现相同的目标?提前感谢您花时间阅读本文!
【问题讨论】:
标签: android android-tablayout android-tabs