whizzzkey
whizzzkey

Reputation: 946

PagerTabStrip change color of first & last tabs

I've a ViewPager and a PagerTabStrip and I want to fade first visible and last visible tabs like this enter image description here

But I haven't the solution to how it implement yet. First of all I have a problem - how to know is a tab is last or first? How to detect is a tab are showing on screen now? Or maybe you can direct me to right way?

Upvotes: 2

Views: 490

Answers (1)

Alexander Mikhaylov
Alexander Mikhaylov

Reputation: 1800

So, PagerTabStrip extends common ViewPager. It's mean you can do something like that to add fade effect:

pagerTabStrip.getChildAt(0).setAlpha()

Other question is when call this method. The best place to add this code is place where you page is changing:

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

            @Override
            public void onPageSelected(int position) {
                //easiest way to implement it here
                int viewsCount = pagerTabStrip.getChildCount();
                pagerTabStrip.getChildAt(0).setAlpha(.5f);
                pagerTabStrip.getChildAt(1).setAlpha(1); // be sure that next element has properly alpha
                pagerTabStrip.getChildAt(viewsCount - 2).setAlpha(1);
                pagerTabStrip.getChildAt(viewsCount - 1).setAlpha(.5f);
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
    });

If you want to smoothly change alpha while user scrolling page, you need to do it inside onPageScrolled() method. You need to calculate alpha from 0.5 to 1 depends on positionOffset.

Upvotes: 1

Related Questions