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

标签 android android-layout android-tabs android-tablelayout

我有适用于 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/

相关文章:

android - Android 中的 EditText 在获得焦点之前不会显示其值

java - 我怎样才能在 Java 中做一个 "return back"按钮?

Android 评分栏超出对话框范围

java - Android 4.0 问题的选项卡

android - mediaplayer 在 android 中播放实时流音频的问题

android - 市场安装应用后点击通知会触发什么Intent?

android - CloudFront 连续丢失

android - 在 OpenGL ES 2.0 中写入深度缓冲区/深度值的解决方法

android - 如何设置 android Horizo​​ntalScrollView 从右到左的行为(默认从右侧选择选项卡,而不是左侧)

启动时选择 Android 设计支持库选项卡