gpt4 book ai didi

android - setColorFilter 在 android 5 的 onTabSelected 事件中不起作用

转载 作者:行者123 更新时间:2023-11-30 00:50:04 26 4
gpt4 key购买 nike

我有适用于 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);
}

}

最佳答案

根据我的研究,setColorFilter 与 android 5 没有冲突,问题是我选择的方式。我为选中和未选中所做的是根据@pskink 在上面的评论中所说的获取自定义 View ,因为它是线性布局,所以我得到了 subview 并将滤色器直接设置为自定义布局中的 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) {
}
});
}

关于android - setColorFilter 在 android 5 的 onTabSelected 事件中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41206792/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com