使用LinearLayoutManager滚动到RecyclerView的顶部 [英] Scroll to top in RecyclerView with LinearLayoutManager

查看:456
本文介绍了使用LinearLayoutManager滚动到RecyclerView的顶部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个片段,其中有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();
        }
    });
}

解决方案

从上述注释继续,理想情况下,替换

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

I have a fragment in which there is RecyclerView with LinearLayoutManager in which there are CardView items. 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. 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. I am calling setFloatingActionButton as soon as set adapter to 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();
        }
    });
}

解决方案

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.

这篇关于使用LinearLayoutManager滚动到RecyclerView的顶部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆