有没有RecyclerView等价于ListView的transcriptMode alwaysScroll?
Is there a RecyclerView equivalent to ListView's transcriptMode alwaysScroll?
使用 ListView,我可以通过设置轻松实现自动滚动的聊天视图:
android:transcriptMode="alwaysScroll"
在其 XML 中。 RecyclerView 中是否有等效项?
http://developer.android.com/reference/android/widget/AbsListView.html#attr_android:transcriptMode
RecyclerView 似乎没有该选项。
您必须手动滚动。
做一些像....
notifyItemInserted(int posn);
recyclerView.smoothScrollToPosition(la.getItemCount()); // <= use this.
还有void notifyItemInserted(int posn);是最终的。所以你也不能覆盖它总是滚动到那里。
您需要制作一个方法,您可以在其中调用与上述代码类似的东西作为捆绑包。
另外请记住平滑滚动有点问题。如果你有很多元素(例如 1000 个元素)并且你想滚动很长的距离,滚动需要永远。我发现有一个教程解决了这个问题,并解释了如何解决这个问题。 http://blog.stylingandroid.com/scrolling-recyclerview-part-1/
尽情享受,
在我的例子中,我将OnLayoutChangeListener
添加到RecyclerView
,因为当键盘隐藏或显示时,RecyclerView
的底部位置将会改变。像这样的代码:
recyclerView.addOnLayoutChangeListener(new OnLayoutChangeListener() {
@Override
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight,
int oldBottom) {
if (bottom < oldBottom) {
recyclerView.post(new Runnable() {
@Override
public void run() {
recyclerView.scrollToPosition(recyclerView.getAdapter().getItemCount() - 1);
}
});
}
}
});
它的工作原理与将 android:transcriptMode="alwaysScroll"
设置为 ListView
相同。
使用 ListView,我可以通过设置轻松实现自动滚动的聊天视图:
android:transcriptMode="alwaysScroll"
在其 XML 中。 RecyclerView 中是否有等效项?
http://developer.android.com/reference/android/widget/AbsListView.html#attr_android:transcriptMode
RecyclerView 似乎没有该选项。 您必须手动滚动。 做一些像....
notifyItemInserted(int posn);
recyclerView.smoothScrollToPosition(la.getItemCount()); // <= use this.
还有void notifyItemInserted(int posn);是最终的。所以你也不能覆盖它总是滚动到那里。
您需要制作一个方法,您可以在其中调用与上述代码类似的东西作为捆绑包。
另外请记住平滑滚动有点问题。如果你有很多元素(例如 1000 个元素)并且你想滚动很长的距离,滚动需要永远。我发现有一个教程解决了这个问题,并解释了如何解决这个问题。 http://blog.stylingandroid.com/scrolling-recyclerview-part-1/
尽情享受,
在我的例子中,我将OnLayoutChangeListener
添加到RecyclerView
,因为当键盘隐藏或显示时,RecyclerView
的底部位置将会改变。像这样的代码:
recyclerView.addOnLayoutChangeListener(new OnLayoutChangeListener() {
@Override
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight,
int oldBottom) {
if (bottom < oldBottom) {
recyclerView.post(new Runnable() {
@Override
public void run() {
recyclerView.scrollToPosition(recyclerView.getAdapter().getItemCount() - 1);
}
});
}
}
});
它的工作原理与将 android:transcriptMode="alwaysScroll"
设置为 ListView
相同。