我有使用ViewPager的Tablayout。我用标签的自定义标签。但问题是Colorfilter在android 5中没有对onTabSelected事件起作用,但它在andrid 4.4中工作得非常好。 这是我的标签的自定义布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:id="@+id/imageView" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="New Text"
android:textColor="@color/custom_selector"
android:gravity="center"
android:id="@+id/tabText" />
</LinearLayout>
这就是我为选中的标签图标所做的:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_page);
tf = Typeface.createFromAsset(getAssets(), "IRANSans_Bold.ttf");
tabCustomization();
/*changeTabsFont();*/
setupTabIcons();
tabLayout.getTabAt(0).getIcon().setColorFilter(Color.parseColor("#a8a8a8"), PorterDuff.Mode.SRC_IN);
}
private void tabCustomization() {
viewPager = (CustomViewPager) findViewById(R.id.viewPager);
viewPager.setPagingEnabled(false);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new MenuFragment(), "menu");
adapter.addFragment(new AddressFragment(), "Address");
adapter.addFragment(new SearchFragment(), "Search");
adapter.addFragment(new IssueFragment(), "Issue");
viewPager.setAdapter(adapter);
for (int i = 0; i < tabLayout.getTabCount(); i++) {
TabLayout.Tab tab = tabLayout.getTabAt(i);
tab.setCustomView(adapter.getTabView(tabLayout,i));
}
ViewGroup vg = (ViewGroup) tabLayout.getChildAt(0);
int tabsCount = vg.getChildCount();
Log.i("TabChild",String.valueOf(tabsCount));
// Iterate over all tabs and set the custom view
}
public void setupViewPager(ViewPager viewPager) {
}
private void setupTabIcons() {
tabLayout.getTabAt(0).setIcon(tabIcons[0]);
tabLayout.getTabAt(1).setIcon(tabIcons[1]);
tabLayout.getTabAt(2).setIcon(tabIcons[2]);
tabLayout.getTabAt(3).setIcon(tabIcons[3]);
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener(){
@Override
public void onTabSelected(TabLayout.Tab tab) {
tab.getIcon().setColorFilter(Color.parseColor("#a8a8a8"), PorterDuff.Mode.SRC_IN);
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
tab.getIcon().setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_IN);
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
public View getTabView(TabLayout tabLayout,int position) {
// Given you have a custom layout in `res/layout/custom_tab.xml` with a TextView and ImageView
View view = LayoutInflater.from(getApplicationContext())
.inflate(R.layout.custom_tab, tabLayout, false);
TextView textView= (TextView) view.findViewById(R.id.tabText);
textView.setText(getPageTitle(position));
textView.setTypeface(tf);
ImageView imageView = (ImageView) view.findViewById(R.id.imageView);
imageView.setImageResource(tabIcons[position]);
return view;
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
答案 0 :(得分:1)
根据我的研究,setColorFilter与android 5没有冲突,问题就是我选择的方式。我为选定和未选中所做的是基于@pskink在上面的注释中所说的基于getcustomview,因为它是linearlayout,我得到了子视图并将colorfilter直接设置为我自定义布局中的imageview:
public void setupViewPager(ViewPager viewPager) {
}
private void setupTabIcons() {
tabLayout.getTabAt(0).setIcon(tabIcons[0]);
tabLayout.getTabAt(1).setIcon(tabIcons[1]);
tabLayout.getTabAt(2).setIcon(tabIcons[2]);
tabLayout.getTabAt(3).setIcon(tabIcons[3]);
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener(){
@Override
public void onTabSelected(TabLayout.Tab tab) {
/*tab.getIcon().setColorFilter(Color.parseColor("#a8a8a8"), PorterDuff.Mode.SRC_IN);*/
linearLayout=(LinearLayout)tab.getCustomView();
ImageView v=(ImageView)linearLayout.getChildAt(0);
v.setColorFilter(Color.parseColor("#a8a8a8"), PorterDuff.Mode.SRC_IN);
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
linearLayout=(LinearLayout)tab.getCustomView();
ImageView v=(ImageView)linearLayout.getChildAt(0);
v.setColorFilter(Color.parseColor("#ffffff"), PorterDuff.Mode.SRC_IN);
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
答案 1 :(得分:0)
这将完成您的工作
private void setupTabIcons() {
tabLayout.getTabAt(0).setIcon(context.getResources().getDrawable(R.drawable.icon_play));
tabLayout.getTabAt(1).setIcon(context.getResources().getDrawable(R.drawable.icon_timer));
tabLayout.getTabAt(2).setIcon(context.getResources().getDrawable(R.drawable.icon_increasing_stocks_graphic));
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
tab.getIcon().setColorFilter(utility.getColor(R.color.blue), PorterDuff.Mode.SRC_IN);
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
tab.getIcon().setColorFilter(utility.getColor(R.color.light_grey), PorterDuff.Mode.SRC_IN);
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
tab.getIcon().setColorFilter(utility.getColor(R.color.blue), PorterDuff.Mode.SRC_IN);
}
});
}