使用LinearLayoutManager在RecyclerView中滚动到顶部

时间:2015-08-22 19:00:31

标签: android android-recyclerview

我有一个片段,其中有RecyclerView LinearLayoutManager,其中有CardView项。点击时有一个浮动操作按钮,项目应滚动到顶部。我已尝试将scrollToPosition以及scrollToPositionWithOffsetRecyclerView以及LinearLayoutManager一起使用,如下所示。但它根本没有效果。为什么会这样?任何人都可以帮助我。

我已将RecyclerView直接放在xml文件中的SwipeRefreshView内。只要将适配器设置为setFloatingActionButton,我就会调用RecyclerView

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

7 个答案:

答案 0 :(得分:110)

继续上述评论,理想情况下,替换

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()是否被调用。尝试在其中添加日志消息,看看它是否已打印。

答案 1 :(得分:9)

使用smoothScrollToPosition()方法为我使用最新的Android版本。

layoutManager.smoothScrollToPosition(mRecyclerView, null, 0);

答案 2 :(得分:3)

使用以下方法调用'scrollToPosition(0)':

git checkout -- .

答案 3 :(得分:0)

在我的情况下,要求是以相反的顺序填充视图,这使得布局显示滚动视图的底部视图

DbSet并没有真正帮助。

当我将回收者视图显示在最后位置

时,我的问题得到了解决

layoutManager.smoothScrollToPosition(mRecyclerView, null, 'last_index')

答案 4 :(得分:0)

在科特林

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

答案 5 :(得分:0)

上述解决方案对我不起作用,因为我的回收器视图位于 NestedScrollView 内。所以这个解决方案对我有用。

nestedScroll.smoothScrollTo(0,recycler.top)

答案 6 :(得分:0)

如果您不想平滑滚动,这将适用于任何 LayoutManager

myRecyclerView.getLayoutManager().scrollToPosition(0);
相关问题