取消RecyclerView垂直fling / swipe

时间:2018-06-16 22:59:21

标签: android android-recyclerview kotlin

我目前正在检测特定视图何时对用户可见或者在视线之外停止并在该视图位置停止RecyclerView滚动。到目前为止,我已经能够在OnScrollListener以及RecyclerView.stopScrollRecyclerView.scrollTo的帮助下实现这一目标,但现在唯一的问题是,如果用户将列表移动,它将会过去这个观点的位置,因为投掷仍然是"表演"在调用滚动到位置后,导致列表超出项目位置。

如何以编程方式结束用户fling操作?像recyclerView.fling(0)之类的东西存在吗?

2 个答案:

答案 0 :(得分:1)

在调用LinearLayoutManager.scrollToPositionWithOffset后,我最终使用RecyclerView.stopScroll作为目标位置,0作为偏移量。

到目前为止,当将这两者结合使用时,抛出行为不再导致列表滚动超过预期位置。

答案 1 :(得分:0)

 public class CustomLayoutManager extends LinearLayoutManager {
 private boolean isScrollEnabled = true;

 public CustomLayoutManager (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();
 }
}

将此设置为recyclerview布局管理器,并使用setManager对象

切换setScrollEnabled(true / false)的滚动

假设您的Recyclerview rv

 CustomLayoutManager lm=new CustomLayoutManager(context of your activity);
 rv.setLayoutManager(lm);
 lm.setScrollEnabled(true/false);
相关问题