我不能在RecyclerView中禁用滚动。我尝试调用rv.setEnabled(false),但我仍然可以滚动。
我如何禁用滚动?
我不能在RecyclerView中禁用滚动。我尝试调用rv.setEnabled(false),但我仍然可以滚动。
我如何禁用滚动?
当前回答
为此,您应该重写recycleView的layoutManager。这样它只会禁用滚动,没有其他功能。你仍然能够处理点击或任何其他触摸事件。例如:-
原:
public class CustomGridLayoutManager extends LinearLayoutManager {
private boolean isScrollEnabled = true;
public CustomGridLayoutManager(Context context) {
super(context);
}
public void setScrollEnabled(boolean flag) {
this.isScrollEnabled = flag;
}
@Override
public boolean canScrollVertically() {
//Similarly you can customize "canScrollHorizontally()" for managing horizontal scroll
return isScrollEnabled && super.canScrollVertically();
}
}
这里使用“isScrollEnabled”标志,你可以暂时启用/禁用循环视图的滚动功能。
另外:
简单覆盖现有的实现,以禁用滚动和允许单击。
linearLayoutManager = new LinearLayoutManager(context) {
@Override
public boolean canScrollVertically() {
return false;
}
};
在芬兰湾的科特林:
object : LinearLayoutManager(this) { override fun canScrollVertically() = false }
其他回答
2022年5月
在新版本的android和api中进行了多次尝试后,这种方法对我来说很有效。
Kotlin的新答案
创建一个名为ScrollDisabledRecyclerView的类,并放入如下代码:
class ScrollDisabledRecyclerView : RecyclerView { constructor(context: Context?) : super(context!!) constructor(context: Context?, @Nullable attrs: AttributeSet?) : super(context!!, attrs) constructor(context: Context?, @Nullable attrs: AttributeSet?, defStyle: Int) : super( context!!, attrs, defStyle ) override fun onTouchEvent(e: MotionEvent): Boolean { return e.action == MotionEvent.ACTION_MOVE } override fun onInterceptTouchEvent(e: MotionEvent): Boolean { return false } }
在你的XML中使用这个类而不是RecyclerView(这个类是从它扩展来的):
<info.sanaebadi.ScrollDisabledRecyclerView android:id="@+id/recyclerView" android:layout_width="0dp" android:layout_height="0dp" android:layout_marginStart="8dp" android:layout_marginTop="8dp" android:layout_marginEnd="8dp" android:layout_marginBottom="8dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" android:clipToPadding="true" tools:listitem="@layout/multiple_questions_row" />
最后,使用mainactivity中的按钮处理next和previous,如下所示:
注意:我正在使用视图绑定
binding.buttonNextQuestion.setOnClickListener {
val totalItemCount: Int = binding.recyclerView.adapter!!.itemCount
if (totalItemCount <= 0) return@setOnClickListener
val lastVisibleItemIndex: Int = linearLayoutManager.findLastVisibleItemPosition()
if (lastVisibleItemIndex >= totalItemCount) return@setOnClickListener
linearLayoutManager.smoothScrollToPosition(
binding.recyclerView,
null,
lastVisibleItemIndex + 1
)
}
binding.buttonPreviousQuestion.setOnClickListener {
val firstVisibleItemIndex: Int =
linearLayoutManager.findFirstCompletelyVisibleItemPosition()
if (firstVisibleItemIndex > 0) {
linearLayoutManager.smoothScrollToPosition(
binding.recyclerView,
null,
firstVisibleItemIndex - 1
)
}
}
答案很简单。
LinearLayoutManager lm = new LinearLayoutManager(getContext()) {
@Override
public boolean canScrollVertically() {
return false;
}
};
上面的代码禁用了RecyclerView的垂直滚动。
另一个替代方案是setLayoutFrozen,但它带来了一堆其他副作用。
https://developer.android.com/reference/android/support/v7/widget/RecyclerView.html setLayoutFrozen(布尔)
如果你只是禁用RecyclerView的滚动功能,那么你可以使用setLayoutFrozen(true);RecyclerView的方法。但它不能禁用触摸事件。
your_recyclerView.setLayoutFrozen(true);
Add
android:descendantFocusability="blocksDescendants"
在你的子SrollView或NestedScrollView(和父ListView, recyclerview和gridview任意一个)