ras*_*yPi 9 android scroll xamarin.android xamarin android-recyclerview
我有一个片段,在其布局文件中有一个RecyclerView.RecyclerView在聊天中保存消息.很自然地,当聊天片段被打开时,我需要RecyclerView滚动到底部.
我尝试直接在RecyclerView上滚动:
var mRecyclerView = view.FindViewById<RecyclerView>
mRecyclerView.ScrollToPosition(mMessages.Count-1);
Run Code Online (Sandbox Code Playgroud)
第二种方法:
LinearLayoutManager mLinearLayoutManager = new LinearLayoutManager(Application.Context);
mRecyclerView.SetLayoutManager(mLinearLayoutManager);
mLinearLayoutManager.ScrollToPosition(mMessages.Count - 1);
Run Code Online (Sandbox Code Playgroud)
第三种方法:
LinearLayoutManager mLinearLayoutManager = new LinearLayoutManager(Application.Context);
mRecyclerView.SetLayoutManager(mLinearLayoutManager);
mLinearLayoutManager.ScrollToPositionWithOffset(mMessages.Count - 1, 0);
Run Code Online (Sandbox Code Playgroud)
不幸的是,两种情况都没有发生.任何建议将不胜感激!
Nar*_*iya 12
请smoothScrollToPosition
用来解决您的问题.我总是用它smoothScrollToPosition
来重定向到任何位置.
确保,mMessages
你想的大小是好的.
例,
RecyclerView rv = (RecyclerView)findViewById(R.id.recyclerView);
rv.smoothScrollToPosition(mMessages.count-1);
Run Code Online (Sandbox Code Playgroud)
更改代码很简单
RecyclerView recyclerView = findViewById(R.id.recyclerView);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setStackFromEnd(true);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(mAdapter);
recyclerView.smoothScrollToPosition(mAdapter.getItemCount());
Run Code Online (Sandbox Code Playgroud)
如果您在 recyclerView 的父级中提供了 ScrollView 或 NestedScrollView 那么它将不起作用。因为它只在 recyclerView 没有像 ScrollView 这样的父布局时才有效。希望它能解决你的错误。
您可以设置setStackFromEnd=true
将视图设置为显示最后一个元素,布局方向将保持不变。
编辑后,我更新了:就像这样:
LinearLayoutManager mLinearLayoutManager = new LinearLayoutManager(Application.Context);
mLinearLayoutManager.setStackFromEnd(true);
mRecyclerView.SetLayoutManager(mLinearLayoutManager);
mRecyclerView.scrollToPosition(mMessages.Count-1);
Run Code Online (Sandbox Code Playgroud)
请检查文档。
编辑:问题是您正在调用scrollToPosition而不将任何布局管理器设置为recyclerview。
考虑到RecyclerView类中的scrollToPosition函数,您的情况很有意义。
/**
* Convenience method to scroll to a certain position.
*
* RecyclerView does not implement scrolling logic, rather forwards the call to
* {@link android.support.v7.widget.RecyclerView.LayoutManager#scrollToPosition(int)}
* @param position Scroll to this adapter position
* @see android.support.v7.widget.RecyclerView.LayoutManager#scrollToPosition(int)
*/
public void scrollToPosition(int position) {
if (mLayoutFrozen) {
return;
}
stopScroll();
if (mLayout == null) {
Log.e(TAG, "Cannot scroll to position a LayoutManager set. " +
"Call setLayoutManager with a non-null argument.");
return;
}
mLayout.scrollToPosition(position);
awakenScrollBars();
}
Run Code Online (Sandbox Code Playgroud)
无法滚动以定位LayoutManager集。使用非null参数调用setLayoutManager。
归档时间: |
|
查看次数: |
11350 次 |
最近记录: |