具有gridlayout smoothScrollTo()速度的Android RecyclerView
我有一个gridlayoutmanager的回收视图 如果我运行代码具有gridlayout smoothScrollTo()速度的Android RecyclerView,android,android-recyclerview,gridlayoutmanager,Android,Android Recyclerview,Gridlayoutmanager,我有一个gridlayoutmanager的回收视图 如果我运行代码 recycler.smoothScrollTo(adapter.getItemCount()) 回收器快速滚动到最后一个元素。我在Stackoverflow上尝试了一些解决方案,以使滚动速度减慢,但所有这些都应用于LinearayOutManager而不是Gridlayoutmanager 有什么帮助吗?我不能肯定地说你有什么问题。但我很幸运有一个非常简单的GridLayoutManager回收视图演示,非常小的示例项目。我
recycler.smoothScrollTo(adapter.getItemCount())
回收器快速滚动到最后一个元素。我在Stackoverflow上尝试了一些解决方案,以使滚动速度减慢,但所有这些都应用于LinearayOutManager而不是Gridlayoutmanager
有什么帮助吗?我不能肯定地说你有什么问题。但我很幸运有一个非常简单的GridLayoutManager回收视图演示,非常小的示例项目。我创建了一个
so
分支,并添加了一个按钮,该按钮与您的操作相同
查一下:
.setOnClickListener{mainRecyclerView.smoothScrollToPosition(data.size)}
仅此而已
检查源代码,这是一个非常简单的示例,用于一些无关的内容,但碰巧有一个带有网格布局的RV:)
更新
您实际需要的是控制recyclerView滚动的速度。嗯
LayoutManager
。怎么会这样public void smoothScrollToPosition(int position) {
...
mLayout.smoothScrollToPosition(this, mState, position);
}
因此它最终调用了mLayout
。这是什么
@VisibleForTesting LayoutManager mLayout代码>
所以,你的布局经理#平滑滚动。。。方法采用
现在反编译GridLayoutManager for science:
注意:此方法实际上位于LinearLayoutManager
中,因为GridLayoutManager
是一个子类,它不会覆盖该方法
一个LinearSmoothScroller
!;但没有参数指定速度
看看它:
public class LinearSmoothScroller extends RecyclerView.SmoothScroller {
private static final boolean DEBUG = false;
private static final float MILLISECONDS_PER_INCH = 25f;
private static final int TARGET_SEEK_SCROLL_DISTANCE_PX = 10000;
...
}
此类有一个start()
方法,描述如下:
*启动给定目标位置的平滑滚动。
那么这叫什么
mLayout.smoothScrollToPosition方法在startSmoothScroll(…)
调用的末尾执行
public void startSmoothScroll(SmoothScroller SmoothScroller){
使用提供的{@link SmoothScroller}启动平滑滚动。
mSmoothScroller.start(mRecyclerView, this);
因此,除了这些,你的问题的答案是:
您需要通过子类化GridLayoutManager来创建扩展,并在其中重写smoothScrollToPosition
方法,以提供您自己的滚动程序逻辑
不过,仔细想想,LayoutManager并不是有史以来最“简单”的类,它们的掌握可能相当复杂
祝你好运!:)我的简单工作解决方案目前仍在实现一个计时器,然后使用它
final CountDownTimer scrollUp_timer = new CountDownTimer(50000, 30) {
@Override
public void onTick(long millisUntilFinished) {
if (layoutManager != null && layoutManager.findFirstVisibleItemPosition() != 0) searchRecyclerView.smoothScrollToPosition(layoutManager.findFirstVisibleItemPosition()-1);
}
@Override
public void onFinish() {
try{
}catch(Exception e){
// log
}
}
};
scrollUp.setOnDragListener(new View.OnDragListener() {
@Override
public boolean onDrag(View view, DragEvent dragEvent) {
layoutManager = ((GridLayoutManager)searchRecyclerView.getLayoutManager());
int action = dragEvent.getAction();
if (action == DragEvent.ACTION_DRAG_ENTERED) {
scrollUp_timer.start();
} else if (action == DragEvent.ACTION_DRAG_EXITED) {
searchRecyclerView.scrollBy(0,0);
scrollUp_timer.cancel();
}
return true;
}
});
您好Mo Dev,欢迎使用SO!您在哪里执行此调用?您是否尝试使用recycler.post(new Runnable(){recycler.smooth…})
(@Amninder这对我有什么帮助?因为它处理的是目标的位置,但是我问的是滚动的速度。谢谢。实际上我有一个非常相似的代码,它也工作得很好(谈论滚动到所需的最后一个元素),但我的问题是它的速度,因为它滚动到所需的元素非常快(这是此功能的正常速度)但是我正在寻找一种方法使此滚动效果比默认效果慢。感谢您提供详细的更新答案,但是我发现它比我当前的编程能力略高:D
final CountDownTimer scrollUp_timer = new CountDownTimer(50000, 30) {
@Override
public void onTick(long millisUntilFinished) {
if (layoutManager != null && layoutManager.findFirstVisibleItemPosition() != 0) searchRecyclerView.smoothScrollToPosition(layoutManager.findFirstVisibleItemPosition()-1);
}
@Override
public void onFinish() {
try{
}catch(Exception e){
// log
}
}
};
scrollUp.setOnDragListener(new View.OnDragListener() {
@Override
public boolean onDrag(View view, DragEvent dragEvent) {
layoutManager = ((GridLayoutManager)searchRecyclerView.getLayoutManager());
int action = dragEvent.getAction();
if (action == DragEvent.ACTION_DRAG_ENTERED) {
scrollUp_timer.start();
} else if (action == DragEvent.ACTION_DRAG_EXITED) {
searchRecyclerView.scrollBy(0,0);
scrollUp_timer.cancel();
}
return true;
}
});