使用ViewPager AddOnPageListener为TabLayout图标设置动画

时间:2016-06-13 14:53:27

标签: android android-fragments android-viewpager android-tablayout

我使用viewPager.addOnPageListener为我的标签布局制作了一个简单的填充和未填充标签图标。我通过创建一系列图标来尝试它。

Filled

Unfilled

问题是viewPager.addOnPageListener没有返回false。它总是返回true。如果我反转语句,它总是返回false

这是我的示例代码:

//TAB ICONS
    private final int[] icons = {R.drawable.home_96px,R.drawable.view_details_75px,R.drawable.today_75px,R.drawable.group_75px};
    private final int[] iconsFilled = {R.drawable.home_filled_96px,R.drawable.view_details_filled_75px,R.drawable.today_filled_75px,R.drawable.group_filled_75px};


//MAP VIEWPAGER AND TAB
public void setUpViewTab(){
    viewPager = (ViewPager) findViewById(R.id.viewpager);
    mTabLayout = (TabLayout) findViewById(R.id.tablayout);
    setupViewPager(viewPager);
    viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            //CHANGE THE TOOLBAR TEXT AS IT TRANSFER TO OTHER FRAGMENTS
            switch (position){
                case 0:
                    if (position == 0) {
                        mTabLayout.getTabAt(0).setIcon(iconsFilled[0]);
                        mToolbar.setTitle("Yayong");
                    }else {
                        mTabLayout.getTabAt(0).setIcon(icons[0]);
                    }
                    break;

                case 1:
                    mTabLayout.getTabAt(1).setIcon(iconsFilled[1]);
                    mToolbar.setTitle("News");
                    break;

                case 2:
                    mTabLayout.getTabAt(2).setIcon(iconsFilled[2]);
                    mToolbar.setTitle("Events");
                    break;

                case 3:
                    mTabLayout.getTabAt(3).setIcon(iconsFilled[3]);
                    mToolbar.setTitle("Users");
                    break;
            }
        }

        @Override
        public void onPageScrollStateChanged(int state) {

            }

        });

    mTabLayout.setupWithViewPager(viewPager);
}

我尝试if else语句,看它是否返回false但没有。

1 个答案:

答案 0 :(得分:1)

我知道它'太迟了......但仍然在寻找答案,这里是片段

将侦听器设置为viewpager

viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            setupTabIcons(position);
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    });

更改图标的方法

 private int tabIconsFocus [] = new int[]{R.drawable.ic_conversation_focus,R.drawable.ic_contact_people_focus, R.drawable.ic_settings_focus};
private int tabIconsNoFocus [] = new int[]{R.drawable.ic_conversation_no_focus,R.drawable.ic_contact_people_no_focus, R.drawable.ic_settings_no_focus};

private void setupTabIcons(int pos) {
    switch (pos){
        case 0:
            tabLayout.getTabAt(0).setIcon(tabIconsFocus[0]);
            tabLayout.getTabAt(1).setIcon(tabIconsNoFocus[1]);
            tabLayout.getTabAt(2).setIcon(tabIconsNoFocus[2]);

            break;
        case 1:
            tabLayout.getTabAt(0).setIcon(tabIconsNoFocus[0]);
            tabLayout.getTabAt(1).setIcon(tabIconsFocus[1]);
            tabLayout.getTabAt(2).setIcon(tabIconsNoFocus[2]);

            break;

        case 2:
            tabLayout.getTabAt(0).setIcon(tabIconsNoFocus[0]);
            tabLayout.getTabAt(1).setIcon(tabIconsNoFocus[1]);
            tabLayout.getTabAt(2).setIcon(tabIconsFocus[2]);

            break;
    }
}

注意:这不会为项目图标设置动画..但会在页面更改时更改图标