滑动时的tabLayout.setOnTabSelectedListener未调用

时间:2016-05-24 05:10:57

标签: android android-viewpager android-tablayout

我正在尝试在选择或滑动viewPager时突出显示选项卡图标。为此我使用'tabLayout.setOnTabSelectedListener()'。但是当我滑动标签时它并没有突出显示,但是当我按下选项卡时,所有工作都完美,标签会突出显示。可能与gradle版本有关。我正在使用'classpath 'com.android.tools.build:gradle:2.1.0''。这是活动:

 private int[] tabIcons = {
        R.drawable.tab_icon_home,
        R.drawable.tab_icon_cart2,
        R.drawable.tab_icon_aksia,
        R.drawable.tab_icon_menu
};
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_icon_tabs);

    toolbar = (Toolbar) findViewById(R.id.toolbar);
    toolbar.setTitleTextColor(Color.WHITE);
    setSupportActionBar(toolbar);

    frameLayout = (FrameLayout) findViewById(R.id.frameLayout);

    adapter = new ViewPagerAdapter(getSupportFragmentManager());
    viewPager = (ViewPager) findViewById(R.id.viewpager);
    viewPager.setAdapter(adapter);
    viewPager.addOnPageChangeListener(pageChangeListener);
    viewPager.setOffscreenPageLimit(10);
    tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(viewPager);
    setupTabIcons();

}


private OnPageChangeListener pageChangeListener = new OnPageChangeListener() {

    int currentPosition = 0;

    @Override
    public void onPageSelected(int newPosition) {

            FragmentLifecycle fragmentToHide = (FragmentLifecycle) adapter.getItem(currentPosition);
            fragmentToHide.onPauseFragment();

            FragmentLifecycle fragmentToShow = (FragmentLifecycle) adapter.getItem(newPosition);
            fragmentToShow.onResumeFragment();

            currentPosition = newPosition;
            adapter.notifyDataSetChanged();

    }

    @Override
    public void onPageScrolled(int arg0, float arg1, int arg2) {

    }

    public void onPageScrollStateChanged(int arg0) { }
};

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.getTabAt(0).getIcon().setColorFilter(Color.WHITE, PorterDuff.Mode.MULTIPLY);
    tabLayout.getTabAt(1).getIcon().setColorFilter(Color.parseColor("#81C784"), PorterDuff.Mode.SRC_IN);
    tabLayout.getTabAt(2).getIcon().setColorFilter(Color.parseColor("#81C784"), PorterDuff.Mode.SRC_IN);
    tabLayout.getTabAt(3).getIcon().setColorFilter(Color.parseColor("#81C784"), PorterDuff.Mode.SRC_IN);



    tabLayout.setOnTabSelectedListener( new TabLayout.ViewPagerOnTabSelectedListener(viewPager) {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            super.onTabSelected(tab);

                tab.getIcon().setColorFilter(Color.WHITE, PorterDuff.Mode.MULTIPLY);


        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {
            super.onTabUnselected(tab);

            tab.getIcon().setColorFilter(Color.parseColor("#81C784"), PorterDuff.Mode.MULTIPLY);
        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {
            super.onTabReselected(tab);

        }
    });


}

3 个答案:

答案 0 :(得分:1)

我找到了解决方案,在gradle中我使用了

dependencies {
    compile 'com.android.support:appcompat-v7:23.0.0'
    compile 'com.android.support:design:23.0.0' }

我在appcompat-v7和design中都从23.0.0更改为23.0.1。现在一切都适合我。

答案 1 :(得分:0)

当您致电setupWithViewPager时,会在内部致电setOnTabSelectedListener(new ViewPagerOnTabSelectedListener(viewPager));,覆盖您的OnTabSelectedListener

您已实施TabLayout.ViewPagerOnTabSelectedListener,然后覆盖onTabSelected()并在setOnTabSelectedListener()之后致电setupWithViewPager()

这是正确的,单击标签时工作正常, 你是说当你滚动页面时,它不起作用。请查看onPageSelected() OnPageChangeListener()。因为在选择特定页面时会调用此方法。

答案 2 :(得分:0)

试试这种方式......

tabLayout = ((TabLayout) findViewById(R.id.tabs));
adapter = new ViewPagerAdapter(getSupportFragmentManager());
viewPager = (ViewPager) findViewById(R.id.viewpager);
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(
                        tabLayout));
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            public void onTabReselected(TabLayout.Tab paramAnonymousTab) {
            }

            public void onTabSelected(TabLayout.Tab paramAnonymousTab) {
                viewPager.setCurrentItem(paramAnonymousTab.getPosition());
                tab.getIcon().setColorFilter(Color.WHITE, PorterDuff.Mode.MULTIPLY);
            }

            public void onTabUnselected(TabLayout.Tab paramAnonymousTab) {
                tab.getIcon().setColorFilter(Color.parseColor("#81C784"), PorterDuff.Mode.MULTIPLY);
            }
});

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.getTabAt(0).getIcon().setColorFilter(Color.WHITE, PorterDuff.Mode.MULTIPLY);
tabLayout.getTabAt(1).getIcon().setColorFilter(Color.parseColor("#81C784"), PorterDuff.Mode.SRC_IN);
tabLayout.getTabAt(2).getIcon().setColorFilter(Color.parseColor("#81C784"), PorterDuff.Mode.SRC_IN);
tabLayout.getTabAt(3).getIcon().setColorFilter(Color.parseColor("#81C784"), PorterDuff.Mode.SRC_IN);

viewPager.setOffscreenPageLimit(4);

快乐的编码。