我正在寻找一种方法来滚动RecyclerView,以显示选中的项目在顶部。
在一个ListView中,我能够通过使用scrollTo(x,y)来做到这一点,并获得需要居中的元素的顶部。
喜欢的东西:
@Override
public void onItemClick(View v, int pos){
mylistView.scrollTo(0, v.getTop());
}
问题是RecyclerView在使用它的scrollTo方法时返回一个错误说
RecyclerView不支持滚动到绝对位置
我如何滚动一个RecyclerView把选定的项目放在视图的顶部?
如果你寻找垂直线性布局管理器,你可以实现平滑滚动使用自定义线性平滑scroller:
import android.content.Context;
import android.graphics.PointF;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.LinearSmoothScroller;
import android.support.v7.widget.RecyclerView;
public class SnappingLinearLayoutManager extends LinearLayoutManager {
public SnappingLinearLayoutManager(Context context, int orientation, boolean reverseLayout) {
super(context, orientation, reverseLayout);
}
@Override
public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state,
int position) {
RecyclerView.SmoothScroller smoothScroller = new TopSnappedSmoothScroller(recyclerView.getContext());
smoothScroller.setTargetPosition(position);
startSmoothScroll(smoothScroller);
}
private class TopSnappedSmoothScroller extends LinearSmoothScroller {
public TopSnappedSmoothScroller(Context context) {
super(context);
}
@Override
public PointF computeScrollVectorForPosition(int targetPosition) {
return SnappingLinearLayoutManager.this
.computeScrollVectorForPosition(targetPosition);
}
@Override
protected int getVerticalSnapPreference() {
return SNAP_TO_START;
}
}
}
在循环视图中使用布局管理器的实例,然后调用recyclerview . smoothscrolltopposition (pos);将平滑滚动到选定的位置到回收视图的顶部
我可以在这里添加的是如何使它与DiffUtil和ListAdapter一起工作
你可能会注意到调用recyclerView. scrolltopposition (pos)或(recyclerView. scrolltopposition)。layoutManager作为LinearLayoutManager)。如果在adapter.submitList后直接调用scrollToPositionWithOffset(pos, offset)将不起作用。这是因为differ在后台线程中查找更改,然后异步地将更改通知适配器。在一个SO上,我看到了几个错误的答案和不必要的延迟等来解决这个问题。
为了正确地处理这种情况,submitList有一个回调函数,在应用更改时调用它。
因此,在这种情况下,正确的kotlin实现是:
//memorise target item here and a scroll offset if needed
adapter.submitList(items) {
val pos = /* here you may find a new position of the item or just use just a static position. It depends on your case */
recyclerView.scrollToPosition(pos)
}
//or
adapter.submitList(items) { recyclerView.smoothScrollToPosition(pos) }
//or etc
adapter.submitList(items) { (recyclerView.layoutManager as LinearLayoutManager).scrollToPositionWithOffset(pos, offset) }
试试对我有效的方法,酷!
创建一个私有静态变量int displayedposition = 0;
现在你的RecyclerView在你的活动中的位置。
myRecyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
LinearLayoutManager llm = (LinearLayoutManager) myRecyclerView.getLayoutManager();
displayedposition = llm.findFirstVisibleItemPosition();
}
});
将此语句放置在视图中显示前站点的位置。
LinearLayoutManager llm = (LinearLayoutManager) mRecyclerView.getLayoutManager();
llm.scrollToPositionWithOffset(displayedposition , youList.size());
好吧,就是这样,它对我来说很好
如果你寻找垂直线性布局管理器,你可以实现平滑滚动使用自定义线性平滑scroller:
import android.content.Context;
import android.graphics.PointF;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.LinearSmoothScroller;
import android.support.v7.widget.RecyclerView;
public class SnappingLinearLayoutManager extends LinearLayoutManager {
public SnappingLinearLayoutManager(Context context, int orientation, boolean reverseLayout) {
super(context, orientation, reverseLayout);
}
@Override
public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state,
int position) {
RecyclerView.SmoothScroller smoothScroller = new TopSnappedSmoothScroller(recyclerView.getContext());
smoothScroller.setTargetPosition(position);
startSmoothScroll(smoothScroller);
}
private class TopSnappedSmoothScroller extends LinearSmoothScroller {
public TopSnappedSmoothScroller(Context context) {
super(context);
}
@Override
public PointF computeScrollVectorForPosition(int targetPosition) {
return SnappingLinearLayoutManager.this
.computeScrollVectorForPosition(targetPosition);
}
@Override
protected int getVerticalSnapPreference() {
return SNAP_TO_START;
}
}
}
在循环视图中使用布局管理器的实例,然后调用recyclerview . smoothscrolltopposition (pos);将平滑滚动到选定的位置到回收视图的顶部