![](/img/trans.png)
[英]Android: Custom LinearLayoutManager not allowing recyclerView to scroll
[英]Scroll to top in RecyclerView with LinearLayoutManager
我有一個片段,其中有RecyclerView
和LinearLayoutManager
,其中有CardView
項。 單擊項目應滾動到頂部有一個浮動操作按鈕。 我嘗試使用scrollToPosition
以及scrollToPositionWithOffset
和RecyclerView
以及LinearLayoutManager
,如下所示。 但它根本沒有效果。 為什么會這樣? 誰能幫幫我。
我已將RecyclerView
直接放在SwipeRefreshView
文件中的 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()
是否被調用。 嘗試在其中添加一條日志消息,看看它是否打印出來。
使用方法smoothScrollToPosition()
在最新的 Android 版本中為我工作。
layoutManager.smoothScrollToPosition(mRecyclerView, null, 0);
使用以下命令調用“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);
}
}
});
}
}
就我而言,要求是以相反的順序填充視圖,這使得布局顯示滾動視圖的底視圖
layoutManager.smoothScrollToPosition(mRecyclerView, null, 'last_index')
並沒有真正的幫助。
當我制作回收器視圖以在最后一個位置顯示孩子時,我的問題得到了解決
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);
有時用戶使用 NestedScrollView 作為我們可以使用下面的代碼的 NestedScrollView。
/*Scrolls NestedScrollView to the top of screen.*/
viewBinding.scrollview.fullScroll(View.FOCUS_UP)
也許它可以在將來對某人有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.