Responsive Ad Area

Share This Post

test

Circular (Vertical) ViewPager – wrong order and UI issues

I am trying to set up a circular vertical (see class below) viewpager but it does not quite work like i want.

Use link https://github.com/BloodWorking/TestCircularViewPager to see gifs of what I mean:

(Downscroll.gif) On start/ first downscroll it goes to page 1 again instead of page 2. After this first time it is normal (page 1 to 2).

(Upscroll.gif) On start/ first upscroll it goes to page 2 instead of page 3. It jumps over it. After this first time everytime I scroll up from page 1 to page 3, page 3 is visible a short time and then disapears (only visibility).

I cannot figure out why and how to fix it. I would be appreciated for help.

I think my solution for CircularViewPager is maybe not the best, so I would take offers to other solutions as well ;).

    mViewPager.setAdapter(mSectionsPagerAdapter);
    mViewPager.setCurrentItem(1, false);
    mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        }

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

        @Override
        public void onPageScrollStateChanged(int state) {

            if (mCurrentPosition == 0)
                mViewPager.setCurrentItem(lastPageIndex - 1, false); 

           //lastPageIndex is 4 (global variable)

            if (mCurrentPosition == lastPageIndex)
                mViewPager.setCurrentItem(1, false);

        }
    });




public class SectionsPagerAdapter extends FragmentPagerAdapter {

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        switch (position) {
            case 0: return PlaceholderFragment.newInstance(3);
            case 1: return PlaceholderFragment.newInstance(1);
            case 2: return PlaceholderFragment.newInstance(2);
            case 3: return PlaceholderFragment.newInstance(3);
            case 4: return PlaceholderFragment.newInstance(1);
            default: return null;
        }

    }

    @Override
    public int getCount() {
        return 5;
    }
}

VerticalViewPager:

public class VerticalViewPager extends ViewPager {
public VerticalViewPager(Context context) {
    super(context);
    init();
}

public VerticalViewPager(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
}

private void init() {
    setPageTransformer(true, new VerticalPageTransformer());
    setOverScrollMode(OVER_SCROLL_NEVER);
}

private class VerticalPageTransformer implements ViewPager.PageTransformer {

    @Override
    public void transformPage(@NonNull View view, float position) {

        if (position < -1) {
            view.setAlpha(0);
        } else if (position <= 1) {
            view.setAlpha(1);

            view.setTranslationX(view.getWidth() * -position);

            float yPosition = position * view.getHeight();
            view.setTranslationY(yPosition);
        } else {
            view.setAlpha(0);
        }
    }
}

private MotionEvent swapXY(MotionEvent ev) {
    float width = getWidth();
    float height = getHeight();

    float newX = (ev.getY() / height) * width;
    float newY = (ev.getX() / width) * height;

    ev.setLocation(newX, newY);

    return ev;
}

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    boolean intercepted = super.onInterceptTouchEvent(swapXY(ev));
    swapXY(ev);
    return intercepted;
}

@Override
public boolean onTouchEvent(MotionEvent ev) {
    return super.onTouchEvent(swapXY(ev));
}

}


Circular (Vertical) ViewPager – wrong order and UI issues
Circular (Vertical) ViewPager – wrong order and UI issues
test
{$excerpt:n}

Share This Post

Leave a Reply

Your email address will not be Publishedd. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Skip to toolbar