ViewPager内部的水平Recycler视图不滚动

May*_*ies 5 java xml android android-layout android-recyclerview

问题

我在ViewPager中的NestedScrollView中有一个水平的RecyclerView.现在,当我尝试滚动RecyclerView时,有时它会滚动,但有时只有ViewPager滚动.

代码

这就是我的RecyclerView XML的样子:

<android.support.v7.widget.RecyclerView
            android:id="@+id/sidescroll"
            android:layout_below="@+id/movie_more_movies2"
            android:layout_marginTop="@dimen/material_layout_keylines_horizontal_margin"
            android:layout_marginBottom="@dimen/material_layout_keylines_horizontal_margin"
            android:layout_width="match_parent"
            android:orientation="horizontal"
            app:layoutManager="android.support.v7.widget.LinearLayoutManager"
            android:layout_height="wrap_content"/>
Run Code Online (Sandbox Code Playgroud)

这就是RecyclerView所在的嵌套滚动的样子:

<android.support.v4.widget.NestedScrollView
    android:id="@+id/detail_holder"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clipToPadding="false"
    android:descendantFocusability="blocksDescendants"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    >
Run Code Online (Sandbox Code Playgroud)

这是viewpager xml:

<com.mt.moviesiwanttowatch.ui.ViewPagerWithHorizontalRecyclerView
            android:id="@+id/viewpager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:clipChildren="false"
            android:clipToPadding="false"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" />
Run Code Online (Sandbox Code Playgroud)

我正在使用此自定义ViewPager:

public class ViewPagerWithHorizontalRecyclerView extends ViewPager {

    public ViewPagerWithHorizontalRecyclerView(Context context) {
        super(context);
    }

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

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

    @Override
    protected boolean canScroll(View v, boolean checkV, int dx, int x, int y) {
        if(v instanceof RecyclerView){
            Log.e("PAGER", "IS");
            return false;
        } else {
            Log.e("PAGER", "IS NOT " + v.toString());
        }

        return super.canScroll(v, checkV, dx, x, y);
    }
}
Run Code Online (Sandbox Code Playgroud)

我的方法

到目前为止我尝试过,你可以看到我写了一个自定义的ViewPager.我试图告诉ViewPager,如果滚动来自RecyclerView,它无法滚动.但这不起作用.

日志:

当ViewPager滚动而不是RecyclerView时,这是一个日志

>     06-27 17:50:53.506 32362-32362/com.mt.moviesiwanttowatch E/PAGER: IS NOT
> com.mt.moviesiwanttowatch.ui.ViewPagerWithHorizontalRecyclerView{c506165
> VFED..... ........ 0,341-1080,1794 #7f090287 app:id/viewpager}
>     06-27 17:50:53.506 32362-32362/com.mt.moviesiwanttowatch E/PAGER: IS NOT android.support.v4.widget.NestedScrollView{d21952 VFED.....
> ........ 0,0-1080,1453 #7f0900b9 app:id/detail_holder}
>     06-27 17:50:53.506 32362-32362/com.mt.moviesiwanttowatch E/PAGER: IS NOT android.widget.RelativeLayout{6ddeec0 V.E...... ........
> 0,0-1080,2860 #7f090199 app:id/movie_overview_holder}
>     06-27 17:50:53.506 32362-32362/com.mt.moviesiwanttowatch E/PAGER: IS
Run Code Online (Sandbox Code Playgroud)

小智 0

尝试为recycleView设置onTouchListener:

recycleView.setOnTouchListener(new OnTouchListener() {
        private float mLastX = 0, mLastY = 0;

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_MOVE:
                    float deltaX = event.getX() - mLastX;
                    float deltaY = event.getY() - mLastY;
                    if (Math.abs(deltaX) > 20 && Math.abs(deltaX) > Math.abs(deltaY)) {
                        getParent().requestDisallowInterceptTouchEvent(true);
                    }
                    mLastX = event.getX();
                    mLastY = event.getY();
                    break;
                case MotionEvent.ACTION_CANCEL:
                case MotionEvent.ACTION_UP:
                    getParent().requestDisallowInterceptTouchEvent(false);
                    break;
            }
            return onTouchEvent(event);
        }
    });
Run Code Online (Sandbox Code Playgroud)