垂直 ViewPager。停止滚动到某个点

Vertical ViewPager. Stop scrolling past certain point

我创建了一个垂直 ViewPager,使用 ViewPage.PageTransformer 并交换 X 和 Y 坐标。 (我用的是this approach

现在,我想要它做的是在某个点停止滚动(我希望最后一个视图只占屏幕高度的 65%,但要占整个宽度)。 通常,在这种情况下我会覆盖 getPageWidth(),但由于我的宽度和高度现在有点混淆,当我这样做时,我的视图占据屏幕高度和宽度的 65%。

那么我该如何解决这个问题?

谢谢!

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);
    }

}

您好,据我了解,您正试图在没有过度滚动的情况下在某个点停止。将 overscroll(never) 方法添加到您想要的 viewpager。并管理对象的边距。尽可能包装内容。

使用 this library 解决了这个问题。