我不能在RecyclerView中禁用滚动。我尝试调用rv.setEnabled(false),但我仍然可以滚动。
我如何禁用滚动?
我不能在RecyclerView中禁用滚动。我尝试调用rv.setEnabled(false),但我仍然可以滚动。
我如何禁用滚动?
当前回答
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">
其他回答
另一个替代方案是setLayoutFrozen,但它带来了一堆其他副作用。
https://developer.android.com/reference/android/support/v7/widget/RecyclerView.html setLayoutFrozen(布尔)
答案很简单。
LinearLayoutManager lm = new LinearLayoutManager(getContext()) {
@Override
public boolean canScrollVertically() {
return false;
}
};
上面的代码禁用了RecyclerView的垂直滚动。
真正的答案是
recyclerView.setNestedScrollingEnabled(false);
文档中的更多信息
由于setLayoutFrozen已弃用,你可以通过使用suppressLayout冻结你的RecyclerView来禁用滚动。
冻结:
recyclerView.suppressLayout(true)
解冻:
recyclerView.suppressLayout(false)
您可以在设置适配器之后添加这一行
ViewCompat.setNestedScrollingEnabled(recyclerView, false);
现在您的recyclerview将与平滑滚动工作