Android布局:具有滚动行为的Viewpager内的垂直Recyclerview内的水平Recyclerview

Sim*_*mon 59 android android-layout android-recyclerview android-coordinatorlayout android-appbarlayout

这是我正在尝试使用以下所有元素构建的应用程序:

蔬菜应用程序

但是,一切正常,我希望内部水平回收视图不捕获任何垂直滚动.所有垂直滚动必须朝向外部垂直回收视图,而不是水平回滚视图,因此垂直滚动将允许工具栏根据其scrollFlag退出视图.

当我将手指放在recyclerview的"StrawBerry Plant"部分并向上滚动时,它会滚出工具栏:

草莓植物

如果我将手指放在水平滚动视图上并向上滚动,它根本不会滚出工具栏.

以下是我目前为止的xml布局代码.

活动的XML布局:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    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:id="@+id/fragment_container"
    android:clipChildren="false">

    <android.support.design.widget.CoordinatorLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/container"
        >

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appBarLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:minHeight="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_scrollFlags="scroll|enterAlways">

        </android.support.v7.widget.Toolbar>

        <android.support.design.widget.TabLayout
            android:id="@+id/sliding_tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="?attr/colorPrimary"
            style="@style/CustomTabLayout"
            />

    </android.support.design.widget.AppBarLayout>
    <android.support.v4.view.ViewPager
        android:id="@+id/viewPager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        />

    </android.support.design.widget.CoordinatorLayout>

</FrameLayout>
Run Code Online (Sandbox Code Playgroud)

"水果"片段 XML布局(其是用于该片段编码-片段被标记在上面图片):

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ProgressBar
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/progressBar"
        android:visibility="gone"
        android:layout_centerInParent="true"
        android:indeterminate="true"/>

<!--    <android.support.v7.widget.RecyclerView-->
    <com.example.simon.customshapes.VerticallyScrollRecyclerView
        android:id="@+id/main_recyclerview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

我使用了一个名为VerticallyScrollRecyclerView的自定义类,该类跟随google在视图组中处理触摸事件的示例.它的目的是拦截和使用所有垂直滚动事件,以便它可以滚动/滚出工具栏:http://developer.android.com/training/gestures/viewgroup.html

VerticallyScrollRecyclerView的代码如下:

public class VerticallyScrollRecyclerView extends RecyclerView {

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

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

    public VerticallyScrollRecyclerView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    ViewConfiguration vc = ViewConfiguration.get(this.getContext());
    private int mTouchSlop = vc.getScaledTouchSlop();
    private boolean mIsScrolling;
    private float startY;

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        final int action = MotionEventCompat.getActionMasked(ev);
        // Always handle the case of the touch gesture being complete.
        if (action == MotionEvent.ACTION_CANCEL || action == MotionEvent.ACTION_UP) {
            // Release the scroll.
            mIsScrolling = false;
            startY = ev.getY();
            return super.onInterceptTouchEvent(ev); // Do not intercept touch event, let the child handle it
        }
        switch (action) {
            case MotionEvent.ACTION_MOVE: {
                Log.e("VRecView", "its moving");
                if (mIsScrolling) {
                    // We're currently scrolling, so yes, intercept the
                    // touch event!
                    return true;
                }
                // If the user has dragged her finger horizontally more than
                // the touch slop, start the scroll
                // left as an exercise for the reader
                final float yDiff = calculateDistanceY(ev.getY());
                Log.e("yDiff ", ""+yDiff);
                // Touch slop should be calculated using ViewConfiguration
                // constants.
                if (Math.abs(yDiff) > 5) {
                    // Start scrolling!
                    Log.e("Scroll", "we are scrolling vertically");
                    mIsScrolling = true;
                    return true;
                }
                break;
            }
        }
        return super.onInterceptTouchEvent(ev);
    }


    private float calculateDistanceY(float endY) {
        return startY - endY;
    }

}
Run Code Online (Sandbox Code Playgroud)

"收藏"的布局是垂直recyclerview内recyclerview:

<?xml version="1.0" encoding="utf-8"?>

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

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Favourite"
        android:layout_marginTop="8dp"
        android:layout_marginBottom="8dp"
        android:layout_marginLeft="16dp"
        android:id="@+id/header_fav"/>

    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_below="@+id/header_fav"
        android:id="@+id/recyclerview_fav">
    </android.support.v7.widget.RecyclerView>

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

这一直困扰着我一段时间,我还没有设法找到解决方案.有谁知道如何解决这个问题?

5分给Griffindor正确的答案,当然还有SO的声望点.

Ari*_*Ari 106

测试解决方案:

您只需要调用mInnerRecycler.setNestedScrollingEnabled(false);你的内心RecyclerView小号


说明:

RecyclerViewAPI 21通过实现NestedScrollingChild接口引入了对嵌套滚动的支持.当您在另一个滚动视图中向同一方向滚动并且您希望View仅在聚焦时滚动内部时,这是一个有价值的功能.

在任何情况下,RecyclerView默认情况下RecyclerView.setNestedScrollingEnabled(true);在初始化时调用自身.现在,回到这个问题,因为这两个你的RecyclerViews为在同一ViewPager具有AppBarBehavior中,CoordinateLayout必须决定哪些滚动,当你从你内心的滚动回应RecyclerView; 当你的内部RecyclerView嵌套滚动被启用时,它会获得滚动焦点,并且CoordinateLayout会选择响应它在外部滚动上RecyclerView的滚动.问题在于,由于你的内部RecyclerView不会垂直滚动,所以没有垂直滚动变化(从CoordinateLayout视角来看),如果没有变化,AppBarLayout也不会改变.

在你的情况下,因为你的内部RecyclerView在不同的方向滚动,你可以禁用它,从而导致CoordinateLayout忽略其滚动并响应外部RecyclerView的滚动.


通知:

xml属性android:nestedScrollingEnabled="boolean"不适用于RecyclerView,并且尝试使用android:nestedScrollingEnabled="false"将导致java.lang.NullPointerException如此,至少现在,您将不得不在代码中执行此操作.

  • 如果您正在使用数据绑定,则可以执行app:nestedScrollingEnabled ="@ {true}",并且不要在代码中执行此操作:D (3认同)

Ahm*_* na 7

如果有人还在寻找,试试这个:

private val Y_BUFFER = 10
private var preX = 0f
private var preY = 0f

mView.rv.addOnItemTouchListener(object : RecyclerView.OnItemTouchListener {
        override fun onTouchEvent(p0: RecyclerView, p1: MotionEvent) {

            }

        override fun onInterceptTouchEvent(rv: RecyclerView, e: MotionEvent): Boolean {
                when (e.action) {
                    MotionEvent.ACTION_DOWN -> rv.parent.requestDisallowInterceptTouchEvent(true)
                    MotionEvent.ACTION_MOVE -> {
                        if (Math.abs(e.x - preX) > Math.abs(e.y - preY)) {
                            rv.parent.requestDisallowInterceptTouchEvent(true)
                        } else if (Math.abs(e.y - preY) > Y_BUFFER) {
                            rv.parent.requestDisallowInterceptTouchEvent(false)
                        }

                    }
                }
                preX = e.x
                preY = e.y
                return false
            }

            override fun onRequestDisallowInterceptTouchEvent(p0: Boolean) {
            }
        })
Run Code Online (Sandbox Code Playgroud)

它检查当前是否水平滚动然后不允许父级处理事件


Sha*_*war 5

我有点晚了,但这绝对适用于面临同样问题的其他人

 mRecyclerView.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() {
            @Override
            public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
                int action = e.getAction();
               // Toast.makeText(getActivity(),"HERE",Toast.LENGTH_SHORT).show();
                switch (action) {
                    case MotionEvent.ACTION_POINTER_UP:
                        rv.getParent().requestDisallowInterceptTouchEvent(true);

                        break;
                }
                return false;
            }
Run Code Online (Sandbox Code Playgroud)

  • 用`case MotionEvent.ACTION_DOWN:` 替换动作对我有用 (3认同)