【问题标题】:Vertical RecyclerView reacts to scroll on nested Horizontal RecyclerViewVertical RecyclerView 对嵌套的 Horizo​​ntal RecyclerView 的滚动做出反应
【发布时间】:2017-06-22 13:59:03
【问题描述】:

在我的活动中,我有由片段组成的 ViewPager。我使用 CustomViewPager 禁用了 ViewPager 上的滑动动作。

我的片段布局有 Vertical RecyclerView,在这个 RecyclerView 里面有多个 Horizo​​ntal RecyclerViews。

为确保我的 AppBarLayout 正确响应滚动行为,我在每个 Horizo​​ntal RecyclerView 上以编程方式将 nestedScrollingEnabled 设置为 false。

附:我正在使用 recylerview 版本 25.4.0

问题是正如您在上面附加的 gif 中看到的那样,要滚动水平 recyclerview,触摸应该是完全横向的,即使对于水平 recyclerview 上的轻微倾斜运动,垂直 recyclerview 也会拾取该事件并垂直向上滚动页面 &下。如何解决?

CustomViewPager.java

public class CustomViewPager extends ViewPager {

    private boolean enabled;

    public CustomViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.enabled = true;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (this.enabled) {
            return super.onTouchEvent(event);
        }

        return false;
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        if (this.enabled) {
            return super.onInterceptTouchEvent(event);
        }

        return false;
    }

    public void setPagingEnabled(boolean enabled) {
        this.enabled = enabled;
    }
}

片段布局

<android.support.v7.widget.RecyclerView
    android:id="@+id/recyclerview_productoverview"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/windowBackground"/>

recyclerview_productoverview(Vertical RecyclerView)内物品的布局

<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.constraint.ConstraintLayout
        android:id="@+id/layout_section_header"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="0dp"
        android:background="#ebedff"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <experttag.nurserylive.util.ui.widget.WhitneySemiBoldTextView
            android:id="@+id/textview_item_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp"
            android:text="TOP FEATURED"
            android:textColor="#3f4266"
            android:textSize="16sp"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            android:layout_marginBottom="16dp"
            app:layout_constraintVertical_bias="0.19"
            android:layout_marginLeft="16dp"
            app:layout_constraintLeft_toLeftOf="parent"
            android:layout_marginRight="16dp"
            app:layout_constraintRight_toRightOf="parent"/>

        <experttag.nurserylive.util.ui.widget.WhitneySemiBoldTextView
            android:id="@+id/textview_item_viewall"
            style="@style/AppTheme.TextLink"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#3f4266"
            android:text="@string/btn_view_all_caps"
            android:layout_marginRight="16dp"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintBaseline_toBaselineOf="@+id/textview_item_name"/>
    </android.support.constraint.ConstraintLayout>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerview_item_section"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/layout_section_header"/>

</android.support.constraint.ConstraintLayout>

水平 RecyclerView:@id/recyclerview_item_section

【问题讨论】:

    标签: android android-recyclerview nestedrecyclerview


    【解决方案1】:

    使用 BetterRecyclerView 解决了这个问题

    see demo

    read explanation

    BetterRecyclerView.java

    【讨论】:

      猜你喜欢
      • 2019-03-30
      • 1970-01-01
      • 1970-01-01
      • 2019-06-29
      • 2016-03-19
      • 2017-01-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多