Android 自动滚动到RecyclerView的底部
我有一个片段,它的布局文件中有一个RecyclerView。RecyclerView在聊天中保存消息。因此,当聊天片段打开时,我自然需要RecyclerView滚动到底部 我尝试直接在RecyclerView上滚动:Android 自动滚动到RecyclerView的底部,android,xamarin,scroll,android-recyclerview,xamarin.android,Android,Xamarin,Scroll,Android Recyclerview,Xamarin.android,我有一个片段,它的布局文件中有一个RecyclerView。RecyclerView在聊天中保存消息。因此,当聊天片段打开时,我自然需要RecyclerView滚动到底部 我尝试直接在RecyclerView上滚动: var mRecyclerView = view.FindViewById<RecyclerView> mRecyclerView.ScrollToPosition(mMessages.Count-1); 第三种方法: LinearLayoutManager mLin
var mRecyclerView = view.FindViewById<RecyclerView>
mRecyclerView.ScrollToPosition(mMessages.Count-1);
第三种方法:
LinearLayoutManager mLinearLayoutManager = new LinearLayoutManager(Application.Context);
mRecyclerView.SetLayoutManager(mLinearLayoutManager);
mLinearLayoutManager.ScrollToPositionWithOffset(mMessages.Count - 1, 0);
不幸的是,这两种情况都没有发生。如有任何建议,将不胜感激 请使用
smoothScrollToPosition
解决您的问题。
我总是使用smoothScrollToPosition
重定向到任何位置
确保,m消息的大小符合您的想法
例如
RecyclerView rv = (RecyclerView)findViewById(R.id.recyclerView);
rv.smoothScrollToPosition(mMessages.count-1);
您可以设置将视图设置为显示最后一个元素的setStackFromEnd=true
,布局方向将保持不变
编辑后我更新了:如下所示:
LinearLayoutManager mLinearLayoutManager = new LinearLayoutManager(Application.Context);
mLinearLayoutManager.setStackFromEnd(true);
mRecyclerView.SetLayoutManager(mLinearLayoutManager);
mRecyclerView.scrollToPosition(mMessages.Count-1);
请查收
编辑:问题是您在调用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();
}
无法滚动定位LayoutManager集。使用非null参数调用setLayoutManager
我相信你说得不对。您必须同时通过LinearLayoutManager和recycler视图才能使其正常工作。这里是我如何使它工作的
mLayoutManager.smoothScrollToPosition(mRecyclerView, null, array.size() - 1);
在设置LayoutManager后,我发现了问题,设置RecyclerView scrollToPosition或smoothScrollToPosition
RecyclerView recyclerView = findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.scrollToPosition(mMessages.count - 1);
希望它能帮助你。很简单
使用更改代码
RecyclerView recyclerView = findViewById(R.id.recyclerView);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setStackFromEnd(true);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(mAdapter);
recyclerView.smoothScrollToPosition(mAdapter.getItemCount());
如果您在recyclerView的父级中提供了ScrollView或NestedScrollView,那么它将不起作用。因为它只在recyclerView没有任何类似ScrollView的父布局时才起作用。希望它能修复您的错误。如果您在recyclerView的父级中提供了ScrollView或NestedScrollView,请尝试以下操作:
parentNestedScrollView.smoothScrollTo(0, parentNestedScrollView.bottom)
有时这可能不起作用,因为在适配器显示新项目之前会尝试更新滚动位置,因此我们需要使用延迟:
popupView.postDelayed({
parentNestedScrollView.smoothScrollTo(0, parentNestedScrollView.bottom)
}, 200)
确保消息大小符合您的想法。你这是什么意思SmoothScrollToPosition
没有解决我的问题。请确保,mMessages
大小大于包含项的屏幕。您的意思是视图实际上是可滚动的吗?那样的话,是的,如果对你有帮助的话。请批准我的回答。也许这与Android在java和Xamarin上的表现不同有关,但是null
在Xamarin中给出了null对象引用异常。@rasperryPi看一下。
popupView.postDelayed({
parentNestedScrollView.smoothScrollTo(0, parentNestedScrollView.bottom)
}, 200)