0

I've created a vertical ViewPager, using ViewPage.PageTransformer and swapping X and Y coordinates. (I use this approach)

Now, what I want it to do is to stop scrolling at a certain point (I want the last view to take only 65% of the screen's height, but the full width). Usually, I would override getPageWidth() in this case, but since my width and height are kind of mixed up right now, when I do that, my view takes 65% of both the height and width of the screen.

So how should I fix this?

Thank you!

MyViewPager.java

public class MyViewPager extends ViewPager {

    public MyViewPager(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(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 onTouchEvent(MotionEvent ev) {
        return super.onTouchEvent(swapXY(ev));
    }

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

MyPagerAdapter.java

public class MyPagerAdapter extends PagerAdapter {

    private Context context;
    private LayoutInflater layoutInflater;

    public MyPagerAdapter(Context context) {
        this.context = context;
        layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

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

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, final int position) {

        if (position == 0) {
            View view = layoutInflater.inflate(R.layout.item_profile_picture, container, false);

            container.addView(view);
            return view;
        }

        else {
            View view = layoutInflater.inflate(R.layout.item_profile_info, container, false);

            container.addView(view);
            return view;
        }

    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((FrameLayout) object);
    }

    @Override
    public float getPageWidth(int position) {
        if (position == 1) return (0.65f);
        return super.getPageWidth(position);
    }

}
Daniil Orekhov
  • 379
  • 1
  • 5
  • 17

2 Answers2

0

Hi as far as I understand you are trying to stop at certain point without having the overscroll. Add overscroll(never) method to the viewpager that you want. And manage your margins on the objects. Wrap content as much as possible.

Izvorski
  • 146
  • 1
  • 7
0

Solved this problem by using this library.

Daniil Orekhov
  • 379
  • 1
  • 5
  • 17