Android-如何通过单击禁用RecyclerView自动滚动 [英] Android - How to disable RecyclerView auto scroll from clicking

查看:778
本文介绍了Android-如何通过单击禁用RecyclerView自动滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

自API 24开始,RecyclerView将自动滚动到某个项目,用户单击该项目将部分显示该项目. 如何禁用此功能?

Since API 24, the RecyclerView will auto scroll to a item which is displayed in partial by the user click the item. How to disable this feature?

以下代码在support-library 25.0.1之前有效.

@Override
    public boolean requestChildRectangleOnScreen(View child, Rect rect, boolean immediate) {
        Object tag = child.getTag();
        if( tag != null && tag.toString().equalsIgnoreCase("preventAutoScroll") ){
            return false;
        }
        return super.requestChildRectangleOnScreen(child, rect, immediate);
    }

它必须是可聚焦且可单击的,因为它是TextView并且文本必须是可选的.

It has to be focusable and clickable, because it's a TextView and the text needs to be selectable.

推荐答案

在回收站视图上设置布局管理器的替代版本.就我而言,我想针对某个特定的子视图进行禁用,例如

Set an overriden version of the layout manager on recycler view. In my case I wanted to disable for when a certain child view was focused e.g.

LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context) {
        @Override
        public boolean requestChildRectangleOnScreen(RecyclerView parent, View child, Rect rect, boolean immediate, boolean focusedChildVisible) {

            if (((ViewGroup) child).getFocusedChild() instanceof YourFocusableChildViewClass) {                    
                return false;
            }

            return super.requestChildRectangleOnScreen(parent, child, rect, immediate, focusedChildVisible);
        }
    };

这篇关于Android-如何通过单击禁用RecyclerView自动滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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