【问题标题】:Scroll behavior of two nested RecyclerViews两个嵌套 RecyclerView 的滚动行为
【发布时间】:2017-12-05 18:48:11
【问题描述】:

我有两个嵌套的RecyclerView。其中之一管理垂直滑动,其中之一处理水平滑动。我面临的问题是,水平 RecyclerView 的滚动有时不会按预期运行。有时它不会识别水平滑动,只会进行垂直滑动。要进行水平滑动,必须在水平方向上画一条真正的直线。偏离几度将被识别为垂直滑动。是否可以调整任何参数以使用户体验更好?

外部布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:gravity="center">

<ProgressBar
    android:id="@+id/pb_new_home"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true" />

<android.support.v7.widget.RecyclerView
    android:id="@+id/rv_modules"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clipToPadding="false"
    android:nestedScrollingEnabled="false"
    android:paddingBottom="@dimen/newhome_recyclerview_paddingbottom" />
</RelativeLayout>

内部布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="@style/NewHomeModuleContainer">

<TextView
    android:id="@+id/tv_module_title"
    style="@style/NewHomeModuleTitle" />

<android.support.v7.widget.RecyclerView
    android:id="@+id/rv_horizontal_recycler"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@+id/tv_show_more"
    android:layout_below="@id/tv_module_title"
    android:layout_marginBottom="-8dp"
    android:clipToPadding="false"
    android:paddingEnd="28dp"
    android:paddingRight="28dp" />

</RelativeLayout>

【问题讨论】:

  • 其实有点复杂,不知道能不能说清楚。但基本上我们有一个代理作为外部模块的适配器,它根据 json 类型实例化不同的模块。每个垂直模块创建另一个 ViewHolder 与自定义 View 和可能另一个 RecyclerView

标签: android android-recyclerview


【解决方案1】:

你需要创建一个自定义的RecyclerView,然后覆盖onInterceptTouchEvent拦截外部RecyclerView的触摸事件

boolean moveInnerRv;
private float startY;

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    float currentY = -1;
    switch(ev.getAction()){
        case MotionEvent.ACTION_DOWN:
            startY = ev.getRawY();
            break;

        case MotionEvent.ACTION_MOVE:
            currentY = ev.getRawY();
            break;

        case MotionEvent.ACTION_CANCEL:
        case MotionEvent.ACTION_UP:

            break;

        default:
            break;
    }
    // if move distance over 100, pass touch to inner RecyclerView
    moveInnerRv = !((currentY - startY) > 100);

    return moveInnerRv;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-12-08
    • 1970-01-01
    • 1970-01-01
    • 2018-09-14
    • 1970-01-01
    • 2016-10-24
    • 2016-07-17
    • 2021-06-10
    相关资源
    最近更新 更多