我需要修复 recyclerview 中的滚动,如果用户只删除一个可见 View ,我想在可用位置显示不可见 View 。我试过以下事情,
添加
android:overScrollMode="never"
在 xml 上,和
recyclerView.setHasFixedSize(false);
在 Java 上,但没有任何帮助,任何帮助都将不胜感激。
谢谢。
更新
我想禁用滚动选项,用户只能在删除一个可见项目后才能看到不可见项目。
最佳答案
我找到了一个非常简单的解决方案,可以停止回收 View 中的所有垂直滚动。这花了我大约半天的时间才弄清楚,但我认为现在真的很简单和优雅。
创建一个 java 类,它使用在回收器 View 中找到的所有构造函数扩展回收器 View 。在您的 xml 中确保将其更改为新的小部件。然后你必须覆盖方法:onInterceptTouchEvent
和 computeVerticleScrollRange
。 recycler View 中有一个错误,它仍然会中断滚动事件。
所以:
public class FooRecyclerView extends RecyclerView {
private boolean verticleScrollingEnabled = true;
public void enableVersticleScroll (boolean enabled) {
verticleScrollingEnabled = enabled;
}
public boolean isVerticleScrollingEnabled() {
return verticleScrollingEnabled;
}
@Override
public int computeVerticalScrollRange() {
if (isVerticleScrollingEnabled())
return super.computeVerticalScrollRange();
return 0;
}
@Override
public boolean onInterceptTouchEvent(MotionEvent e) {
if(isVerticleScrollingEnabled())
return super.onInterceptTouchEvent(e);
return false;
}
public FooRecyclerView(Context context) {
super(context);
}
public FooRecyclerView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public FooRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
}
来自
RecyclerView
android:id="@+id/task_sub_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
到
com.customGoogleViews.FooRecyclerView
android:id="@+id/task_sub_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
现在在您的代码中,您只需使用 disableVerticleScroll
方法和您的所有设置来控制回收器 View 的状态。
关于android - 停止在 recyclerview 中滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28848831/