使用 LinearLayoutManager 滚动到回收视图的顶部

我有一个片段,其中有 RecyclerViewLinearLayoutManager,其中有 CardView项目。在单击哪些项目应该滚动到顶部时,有一个浮动操作按钮。我已经尝试使用 scrollToPosition以及 scrollToPositionWithOffsetRecyclerView,也与 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();
}
});
}
97427 次浏览

Continuing from above comments, ideally, replacing

mRecyclerView.smoothScrollToPosition(0);

in the onClick of the floating action button with

mLayoutManager.scrollToPositionWithOffset(0, 0);

should work. You can also remove the SnackBar code, because you don't need it anyways. 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. Try adding a log message in it and see if its printed.

Call 'scrollToPosition(0)' using this:

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);
}
}
});
}
}

Using the method smoothScrollToPosition() worked for me with the newest Android version.

layoutManager.smoothScrollToPosition(mRecyclerView, null, 0);

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.

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. So this solution worked for me.

nestedScroll.smoothScrollTo(0,recycler.top)

If you don't want smooth scroll this will work with any LayoutManager:

myRecyclerView.getLayoutManager().scrollToPosition(0);

Some time user uses NestedScrollView for the NestedScrollView we can use below code .

 /*Scrolls NestedScrollView to the top of screen.*/
viewBinding.scrollview.fullScroll(View.FOCUS_UP)

May be it can help to somebody in future.