使用 LinearLayoutManager 在 RecyclerView 中滚动到顶部
Scroll to top in RecyclerView with LinearLayoutManager
我有一个片段,其中有 RecyclerView
和 LinearLayoutManager
,其中有 CardView
项。单击项目应滚动到顶部有一个浮动操作按钮。我已经尝试使用 scrollToPosition
以及 scrollToPositionWithOffset
和 RecyclerView
以及 LinearLayoutManager
,如下所示。但它根本没有效果。为什么会这样?谁能帮帮我。
并且我已将 RecyclerView
直接放在 xml 文件中的 SwipeRefreshView
中。一旦将适配器设置为 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();
}
});
}
从上面的评论继续,理想情况下,替换
mRecyclerView.smoothScrollToPosition(0);
在浮动操作按钮的onClick
中加上
mLayoutManager.scrollToPositionWithOffset(0, 0);
应该可以。您也可以删除 SnackBar
代码,因为您根本不需要它。所以,总而言之,你上面的方法应该看起来像
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);
}
});
}
如果您说上述方法不起作用,请测试 onClick()
是否被调用。尝试在其中添加一条日志消息,看看是否打印出来。
使用此调用 '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);
}
}
});
}
}
使用方法 smoothScrollToPosition()
对我来说适用于最新的 Android 版本。
layoutManager.smoothScrollToPosition(mRecyclerView, null, 0);
在我的例子中,要求以相反的顺序填充视图,这使得布局显示滚动视图的底部视图
layoutManager.smoothScrollToPosition(mRecyclerView, null, 'last_index')
并没有真正帮助。
当我使回收站视图在最后位置显示 child 时,我的问题得到解决
recyclerView.scrollToPosition(length-1)
在科特林中
// 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)
}
上述解决方案对我不起作用,因为我的回收器视图在 NestedScrollView 中。所以这个解决方案对我有用。
nestedScroll.smoothScrollTo(0,recycler.top)
如果您不想要平滑滚动,这将适用于任何 LayoutManager
:
myRecyclerView.getLayoutManager().scrollToPosition(0);
我有一个片段,其中有 RecyclerView
和 LinearLayoutManager
,其中有 CardView
项。单击项目应滚动到顶部有一个浮动操作按钮。我已经尝试使用 scrollToPosition
以及 scrollToPositionWithOffset
和 RecyclerView
以及 LinearLayoutManager
,如下所示。但它根本没有效果。为什么会这样?谁能帮帮我。
并且我已将 RecyclerView
直接放在 xml 文件中的 SwipeRefreshView
中。一旦将适配器设置为 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();
}
});
}
从上面的评论继续,理想情况下,替换
mRecyclerView.smoothScrollToPosition(0);
在浮动操作按钮的onClick
中加上
mLayoutManager.scrollToPositionWithOffset(0, 0);
应该可以。您也可以删除 SnackBar
代码,因为您根本不需要它。所以,总而言之,你上面的方法应该看起来像
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);
}
});
}
如果您说上述方法不起作用,请测试 onClick()
是否被调用。尝试在其中添加一条日志消息,看看是否打印出来。
使用此调用 '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);
}
}
});
}
}
使用方法 smoothScrollToPosition()
对我来说适用于最新的 Android 版本。
layoutManager.smoothScrollToPosition(mRecyclerView, null, 0);
在我的例子中,要求以相反的顺序填充视图,这使得布局显示滚动视图的底部视图
layoutManager.smoothScrollToPosition(mRecyclerView, null, 'last_index')
并没有真正帮助。
当我使回收站视图在最后位置显示 child 时,我的问题得到解决
recyclerView.scrollToPosition(length-1)
在科特林中
// 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)
}
上述解决方案对我不起作用,因为我的回收器视图在 NestedScrollView 中。所以这个解决方案对我有用。
nestedScroll.smoothScrollTo(0,recycler.top)
如果您不想要平滑滚动,这将适用于任何 LayoutManager
:
myRecyclerView.getLayoutManager().scrollToPosition(0);