[英]Scroll to top in RecyclerView with LinearLayoutManager
I have a fragment in which there is RecyclerView
with LinearLayoutManager
in which there are CardView
items.我有一个片段,其中有
RecyclerView
和LinearLayoutManager
,其中有CardView
项。 There is a floating action button on clicking which the items should scroll to top.单击项目应滚动到顶部有一个浮动操作按钮。 I have tried using
scrollToPosition
as well as scrollToPositionWithOffset
with RecyclerView
and also with LinearLayoutManager
as shown below.我尝试使用
scrollToPosition
以及scrollToPositionWithOffset
和RecyclerView
以及LinearLayoutManager
,如下所示。 But it has no effect at all.但它根本没有效果。 Why is this so?
为什么会这样? Can anyone please help me out.
谁能帮帮我。
And i have placed the RecyclerView
directly inside the SwipeRefreshView
in the xml file.我已将
RecyclerView
直接放在SwipeRefreshView
文件中的 SwipeRefreshView 内。 I am calling setFloatingActionButton
as soon as set adapter to RecyclerView
.一旦将适配器设置为
RecyclerView
,我就会调用setFloatingActionButton
。
public void setFloatingActionButton(final View view) {
float = (android.support.design.widget.FloatingActionButton) getActivity().findViewById(R.id.float);
float.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mRecyclerView.smoothScrollToPosition(0);
android.support.design.widget.Snackbar.make(view, "Scrolled to Top", android.support.design.widget.Snackbar.LENGTH_SHORT)
.setAction("Ok", new View.OnClickListener() {
@Override
public void onClick(View v) {
LinearLayoutManager llm = (LinearLayoutManager) mRecyclerView.getLayoutManager();
llm.scrollToPosition(0);
}
})
.setActionTextColor(getActivity().getResources().getColor(R.color.coloLink))
.show();
}
});
}
Continuing from above comments, ideally, replacing继续上述评论,理想情况下,替换
mRecyclerView.smoothScrollToPosition(0);
in the onClick
of the floating action button with在浮动操作按钮的
onClick
中
mLayoutManager.scrollToPositionWithOffset(0, 0);
should work.应该管用。 You can also remove the
SnackBar
code, because you don't need it anyways.您还可以删除
SnackBar
代码,因为无论如何您都不需要它。 So, all in all your above method should look like所以,总而言之,你上面的方法应该看起来像
public void setFloatingActionButton(final View view) {
float actionButton = (android.support.design.widget.FloatingActionButton) getActivity()
.findViewById(R.id.float);
actionButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LinearLayoutManager layoutManager = (LinearLayoutManager) mRecyclerView
.getLayoutManager();
layoutManager.scrollToPositionWithOffset(0, 0);
}
});
}
And if you say that the above doesnt work, then test if the onClick()
is even being called or not.如果你说上面的方法不起作用,那么测试
onClick()
是否被调用。 Try adding a log message in it and see if its printed.尝试在其中添加一条日志消息,看看它是否打印出来。
使用方法smoothScrollToPosition()
在最新的 Android 版本中为我工作。
layoutManager.smoothScrollToPosition(mRecyclerView, null, 0);
Call 'scrollToPosition(0)' using this:使用以下命令调用“scrollToPosition(0)”:
public void scrollToPosition(final int position) {
if (mRecyclerView != null) {
getHandler().post(new Runnable() {
@Override
public void run() {
mRecyclerView.scrollToPosition(position);
if (position == 0) {
LinearLayoutManager layoutManager = (LinearLayoutManager) mRecyclerView.getLayoutManager();
layoutManager.scrollToPositionWithOffset(0, 0);
mScrollView.fullScroll(View.FOCUS_UP);
}
}
});
}
}
In my case, the requirement was to populate the view in reverse order, which made the layout show the bottom view of the scroll view就我而言,要求是以相反的顺序填充视图,这使得布局显示滚动视图的底视图
layoutManager.smoothScrollToPosition(mRecyclerView, null, 'last_index')
didn't really help. layoutManager.smoothScrollToPosition(mRecyclerView, null, 'last_index')
并没有真正的帮助。
My problem was resolved when I made the recycler view to show the child at the last position当我制作回收器视图以在最后一个位置显示孩子时,我的问题得到了解决
recyclerView.scrollToPosition(length-1)
In Kotlin在科特林
// Scroll to the top of the list
GlobalScope.launch(Dispatchers.Main) {
delay(200)
if (binding.rateList.size > 1) {
//binding.recyclerView.smoothScrollToPosition(0)
binding.recyclerView.layoutManager?.scrollToPosition(0)
}
The above solutions didn't work for me coz my recycler view was inside the NestedScrollView.上述解决方案对我不起作用,因为我的回收器视图位于 NestedScrollView 内。 So this solution worked for me.
所以这个解决方案对我有用。
nestedScroll.smoothScrollTo(0,recycler.top)
如果您不想平滑滚动,这将适用于任何LayoutManager
:
myRecyclerView.getLayoutManager().scrollToPosition(0);
Some time user uses NestedScrollView for the NestedScrollView we can use below code.有时用户使用 NestedScrollView 作为我们可以使用下面的代码的 NestedScrollView。
/*Scrolls NestedScrollView to the top of screen.*/
viewBinding.scrollview.fullScroll(View.FOCUS_UP)
May be it can help to somebody in future.也许它可以在将来对某人有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.