按像素垂直滚动 Android 中的 RecyclerView
Vertical scroll the RecyclerView in Android by pixels
在我的聊天应用程序中,我使用 RecyclerView
和 LayoutManager
来显示聊天消息列表。我有一个案例,当用户浏览旧消息并且新消息到达时。那时我想通过小距离滚动聊天以确认收到新消息的用户。
我需要按距离(以像素为单位)滚动 RecyclerView
。
我发现 LayoutManager
有方法 scrollVerticallyBy()
,
public int scrollVerticallyBy (int dy, RecyclerView.Recycler recycler, RecyclerView.State state)
但我对它需要的参数感到困惑,RecyclerView.Recycler recycler
,RecyclerView.State state
我不确定它是否适合我的工作。
换句话说,我想找到 ListView.smoothScrollBy(int distance, int duration)
的替代品
您可以使用:
recyclerView.smoothScrollToPosition(recyclerView.getAdapter().getItemCount());
if蚂蚁相关职位.
recyclerView.smoothScrollToPosition(0);
参考:http://blog.stylingandroid.com/scrolling-recyclerview-part-1/
@Override
public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state,
int position) {
LinearSmoothScroller linearSmoothScroller =
new LinearSmoothScroller(recyclerView.getContext()) {
@Override
public PointF computeScrollVectorForPosition(int targetPosition) {
return LinearLayoutManager.this
.computeScrollVectorForPosition(targetPosition);
}
};
linearSmoothScroller.setTargetPosition(position);
startSmoothScroll(linearSmoothScroller);
}
负责执行实际滚动的 LinearSmoothScroller 实例。这不是一个微不足道的 class – 它包含了很多逻辑。然而,它实现的行为相当容易理解。它不是尝试确定在到达终点之前需要滚动的确切像素数,而是以 25 的速度执行一系列 10000 像素 的快速移动每英寸毫秒数,直到最终目标项目进入视口,然后它减速停止,最终目标可见。
实现此目的的最佳方法是使用此方法:
recyclerView.smoothScrollBy(0, 100);
这是方法的签名。您可以在 x 和 y 轴上滚动:
public void smoothScrollBy(int dx, int dy)
注意: 如果 smothScrollBy(dx,dy)
不起作用是因为 RecyclerView 尚未加载其元素。为此,我建议使用:
new Handler().postDelayed(new Runnable() {
@Override public void run() {
recyclerView.smoothScrollBy(0, 100);
}
}, 200);
这样就可以确定视图已经加载了
您可以像这样直接滚动到回收站视图的底部,而无需使用
smoothScroll.
/* Method to set the recycler view focus to bottom */
private void scrollToBottom() {
mRecyclerView.scrollToPosition(mAdapter.getItemCount() - 1);
}
在我的聊天应用程序中,我使用 RecyclerView
和 LayoutManager
来显示聊天消息列表。我有一个案例,当用户浏览旧消息并且新消息到达时。那时我想通过小距离滚动聊天以确认收到新消息的用户。
我需要按距离(以像素为单位)滚动 RecyclerView
。
我发现 LayoutManager
有方法 scrollVerticallyBy()
,
public int scrollVerticallyBy (int dy, RecyclerView.Recycler recycler, RecyclerView.State state)
但我对它需要的参数感到困惑,RecyclerView.Recycler recycler
,RecyclerView.State state
我不确定它是否适合我的工作。
换句话说,我想找到 ListView.smoothScrollBy(int distance, int duration)
您可以使用:
recyclerView.smoothScrollToPosition(recyclerView.getAdapter().getItemCount());
if蚂蚁相关职位.
recyclerView.smoothScrollToPosition(0);
参考:http://blog.stylingandroid.com/scrolling-recyclerview-part-1/
@Override
public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state,
int position) {
LinearSmoothScroller linearSmoothScroller =
new LinearSmoothScroller(recyclerView.getContext()) {
@Override
public PointF computeScrollVectorForPosition(int targetPosition) {
return LinearLayoutManager.this
.computeScrollVectorForPosition(targetPosition);
}
};
linearSmoothScroller.setTargetPosition(position);
startSmoothScroll(linearSmoothScroller);
}
负责执行实际滚动的 LinearSmoothScroller 实例。这不是一个微不足道的 class – 它包含了很多逻辑。然而,它实现的行为相当容易理解。它不是尝试确定在到达终点之前需要滚动的确切像素数,而是以 25 的速度执行一系列 10000 像素 的快速移动每英寸毫秒数,直到最终目标项目进入视口,然后它减速停止,最终目标可见。
实现此目的的最佳方法是使用此方法:
recyclerView.smoothScrollBy(0, 100);
这是方法的签名。您可以在 x 和 y 轴上滚动:
public void smoothScrollBy(int dx, int dy)
注意: 如果 smothScrollBy(dx,dy)
不起作用是因为 RecyclerView 尚未加载其元素。为此,我建议使用:
new Handler().postDelayed(new Runnable() {
@Override public void run() {
recyclerView.smoothScrollBy(0, 100);
}
}, 200);
这样就可以确定视图已经加载了
您可以像这样直接滚动到回收站视图的底部,而无需使用 smoothScroll.
/* Method to set the recycler view focus to bottom */
private void scrollToBottom() {
mRecyclerView.scrollToPosition(mAdapter.getItemCount() - 1);
}