簡體   English   中英

Android:VerticalViewPager內的垂直滾動視圖無法正常工作

[英]Android: Scrollview vertical inside VerticalViewPager doesn't work properly

這是我的VerticalViewPager:

    public class VerticalViewPager extends ViewPager {

    public interface OnTouchListener {
        void onTouchDown();
        void onTouchUp();
        void onTouchMove();
    }

    private OnTouchListener mListener;

    public VerticalViewPager(Context context) {
        super(context);
        init();
    }

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

    private void init() {
        // The majority of the magic happens here
        setPageTransformer(true, new VerticalPageTransformer());
        // The easiest way to get rid of the overscroll drawing that happens on the left and right
        setOverScrollMode(OVER_SCROLL_NEVER);
    }

    public void setOnTouchListener(OnTouchListener listener) {
        mListener = listener;
    }

    private class VerticalPageTransformer implements ViewPager.PageTransformer {

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

            if (position < -1) { // [-Infinity,-1)
                // This page is way off-screen to the left.
                view.setAlpha(0);

            } else if (position <= 1) { // [-1,1]
                view.setAlpha(1);

                // Counteract the default slide transition
                view.setTranslationX(view.getWidth() * -position);

                //set Y setNum to swipe in from top
                float yPosition = position * view.getHeight();
                view.setTranslationY(yPosition);

            } else { // (1,+Infinity]
                // This page is way off-screen to the right.
                view.setAlpha(0);
            }
        }
    }

    /**
     * Swaps the X and Y coordinates of your touch event.
     */
    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 touch coordinates to original reference frame for any child views
        return intercepted;
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {

        switch (ev.getAction() & MotionEventCompat.ACTION_MASK) {
            case MotionEvent.ACTION_DOWN:
                mListener.onTouchDown();
                break;
            case MotionEvent.ACTION_UP:
                mListener.onTouchUp();
                break;
            case MotionEvent.ACTION_MOVE:
                mListener.onTouchMove();
                break;
        }

        return super.onTouchEvent(swapXY(ev));
    }
}

我在此ViewPager中放入了一些片段。 在最后一個片段中,有一個ScrollView:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

            ...

        <ScrollView
            android:id="@+id/vertical_scrollview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:scrollbars="none">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                android:paddingBottom="@dimen/keyline_4">

                ....

            </LinearLayout>

        </ScrollView>

    </LinearLayout>

</RelativeLayout>

在最后一個片段中,我可以正常滾動此頁面。 但是,如果我想滑動到VerticalViewPager的上一頁,就不會發生任何事情。

謝謝大家!

不要將任何VerticalViewPager與帶有PagerSnapHelper的簡單RecyclerView一起使用,就可以像非常平滑的方式一樣實現它,並將所需的布局添加到其中。 您可以獲得更好的性能。

檢查此: https : //link.medium.com/aBN5iEFk30

嘗試使用NestedScrollView而不是ScrollView

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM