Kimi Chiu
Kimi Chiu

Reputation: 2173

Android - How to disable RecyclerView auto scroll from clicking

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.

Upvotes: 4

Views: 5588

Answers (2)

GerbenDev
GerbenDev

Reputation: 28

A solution which works across the whole app

In AndroidManifest.xml, set activity's windowSoftInputMode to adjustPan:

<activity
    android:name=".YourActivity"
    android:windowSoftInputMode="adjustPan">
</activity>

This prevents the layout from being resized when the soft keyboard appears, stopping the automatic scrolling.

adjustpan documentation :

The activity's main window is not resized to make room for the soft keyboard. Rather, the contents of the window are automatically panned so that the current focus is never obscured by the keyboard and users can always see what they are typing. This is generally less desirable than resizing, because the user may need to close the soft keyboard to get at and interact with obscured parts of the window.

Upvotes: 0

user7768828
user7768828

Reputation: 96

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

Upvotes: 6

Related Questions