如何以编程方式调用ViewPagerOnTabSelectedListener?
问题描述:
我想改变我的TabLayout中的选项卡图标的颜色,实际上我使用addOnTabSelectedListener来实现这一点,但这只适用于当我在选项卡之间滑动时。所以我希望第一个选项卡的图标颜色与更改选项卡时的颜色相同。如何以编程方式调用ViewPagerOnTabSelectedListener?
我试过viewPager.setCurrentItem(),但这只适用于传递的索引不同于0(第一个标签)的情况。
那么,如何以编程方式调用ViewPagerOnTabSelectedListener?
这是我的代码:
tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(viewPager) {
@Override
public void onTabSelected(TabLayout.Tab tab){
super.onTabReselected(tab);
int tabIconColor = ContextCompat.getColor(MainActivity.this,R.color.prehipertension);
tab.getIcon().setColorFilter(tabIconColor, PorterDuff.Mode.SRC_IN);
}
@Override
public void onTabUnselected(TabLayout.Tab tab){
super.onTabUnselected(tab);
int tabIconColor = ContextCompat.getColor(MainActivity.this,R.color.blancoTransparencia);
tab.getIcon().setColorFilter(tabIconColor, PorterDuff.Mode.SRC_IN);
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
int tabIconColor = ContextCompat.getColor(MainActivity.this,R.color.blanco);
tab.getIcon().setColorFilter(tabIconColor, PorterDuff.Mode.SRC_IN);
}
});
viewPager.setCurrentItem(0);
当滑动到第二个选项卡(并返回第一个选项卡)时,图标颜色会正确更改。
答
我不知道我能正确或不理解你的问题,但我用寻呼机查看工作和处理它通过该算法的过程。 (检查它可能是你找到你想要的)。
1-首先确定你的标签。
private void createTabIcons(){
// i get custom tab view.
View profileTab = LayoutInflater.from(this)
.inflate(R.layout.custom_tab,null);
// set it's views such as its text and icon
ViewAppHolder.CustomTabViewHolder customTabProfileViewHolder =
new ViewAppHolder.CustomTabViewHolder(profileTab);
// set properties of icon ... note this is my main tab so i set 'color.active'
customTabProfileViewHolder.TAB_IMAGE_VIEW
.setImageDrawable(getDrawable(R.drawable.ic_person_black_24dp));
customTabProfileViewHolder.TAB_IMAGE_VIEW
.setColorFilter(ContextCompat.getColor(this,R.color.active_tab));
tabLayout.getTabAt(Constants.USER_PROFILE_TAB).setCustomView(profileTab);
// it's the next tab by the same previous steps
View friendsTab = LayoutInflater.from(this)
.inflate(R.layout.custom_tab,null);
ViewAppHolder.CustomTabViewHolder customTabFriendsViewHolder =
new ViewAppHolder.CustomTabViewHolder(friendsTab);
// the diff is here .. i set the color of icon 'color.non_active'
customTabFriendsViewHolder.TAB_IMAGE_VIEW
.setImageDrawable(getDrawable(R.drawable.ic_group_black_24dp));
customTabFriendsViewHolder.TAB_IMAGE_VIEW
.setColorFilter(ContextCompat.getColor(this,R.color.non_active_tab));
/// .... and remain tabs also have the same thing with color non_active.
2 - 然后在这两个onTabSelected
和onTabUnselected
我只是调用方法称为setCurrentTab()
和它的实现是
private void setCurrentTab() {
if (tabLayout != null) {
int currentPosition = tabLayout.getSelectedTabPosition();
int unSelectedTabs = currentPosition;
do {
unSelectedTabs = (unSelectedTabs + 1) % 4;
Log.e("un selected ", String.valueOf(unSelectedTabs));
Log.e("un selected ", String.valueOf(currentPosition));
ViewAppHolder.CustomTabViewHolder customTabViewHolder =
new ViewAppHolder.CustomTabViewHolder(
tabLayout.getTabAt(unSelectedTabs).getCustomView()
);
if (unSelectedTabs != currentPosition) {
customTabViewHolder.TAB_IMAGE_VIEW
.setColorFilter(ContextCompat.getColor(this, R.color.non_active_tab));
} else {
customTabViewHolder.TAB_IMAGE_VIEW
.setColorFilter(ContextCompat.getColor(this, R.color.active_tab));
}
} while (unSelectedTabs != currentPosition);
}
}