Android - How to disable RecyclerView auto scroll from clicking

人走茶凉 提交于 2019-12-11 07:18:24

问题


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?

The codes below works before 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);
    }

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


回答1:


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


来源:https://stackoverflow.com/questions/43182601/android-how-to-disable-recyclerview-auto-scroll-from-clicking

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!