我不能在RecyclerView中禁用滚动。我尝试调用rv.setEnabled(false),但我仍然可以滚动。

我如何禁用滚动?


当前回答

XML格式:-

你可以添加

android:nestedScrollingEnabled=“false”

在子RecyclerView布局XML文件中

or

在爪哇:-

childRecyclerView.setNestedScrollingEnabled(假);

Java代码中的RecyclerView。

使用ViewCompat (Java):-

childRecyclerView.setNestedScrollingEnabled(假);将只在android_version>21设备上工作。要在所有设备上工作,请使用以下方法

ViewCompat。setNestedScrollingEnabled (childRecyclerView假);

其他回答

您可以在设置适配器之后添加这一行

ViewCompat.setNestedScrollingEnabled(recyclerView, false);

现在您的recyclerview将与平滑滚动工作

在activity的onCreate方法中,你可以简单地做:

recyclerView.stopScroll()

然后停止滚动。

Add

android:descendantFocusability="blocksDescendants"

在你的子SrollView或NestedScrollView(和父ListView, recyclerview和gridview任意一个)

API 21及以上版本:

不需要java代码。 你可以设置android:nestedScrollingEnabled="false" 在xml:

<android.support.v7.widget.RecyclerView
     android:id="@+id/recycler"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:clipToPadding="true"
     android:nestedScrollingEnabled="false"
     tools:listitem="@layout/adapter_favorite_place">

你只需添加这一行:

recyclerView.suppressLayout(true)