Samad
Samad

Reputation: 1842

How to remove focus from RecyclerView inside ScrollView?

I have a Scrollview which contains an ImageView and RecyclerView. if navigation drawer opened then closed the RecyclerView auto scrolling to top, How to stop this?

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/scrollViewMain"
    >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

       <ImageView
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:id="@+id/imageView_main_icons_line"
           android:src="@drawable/main_line" />

              ...


       <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerView_activity_main_passenger_log"
            android:paddingBottom="2dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    </LinearLayout>
</ScrollView>
</RelativeLayout>

Upvotes: 23

Views: 28554

Answers (5)

Apurv Thakkar
Apurv Thakkar

Reputation: 10218

Adding android:descendantFocusability="blocksDescendants" can restrict scroll to recylerview but if you have edittext inside your layout, this property can block the focus of that particular edittext too. So if you are using this property please make sure you are removing this property in your kotlin/java class once the layout loaded.

parentLayout?.descendantFocusability = FOCUS_BEFORE_DESCENDANTS (view as ViewGroup).descendantFocusability = FOCUS_BEFORE_DESCENDANTS

Upvotes: 3

Ashraf Hussain
Ashraf Hussain

Reputation: 1

Just add the following code in your linear layout, works 100% ` android:descendantFocusability="blocksDescendants"

<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

    <LinearLayout
        android:descendantFocusability="blocksDescendants"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

Upvotes: 0

landerlyoung
landerlyoung

Reputation: 1810

That is because RecyclerView ALWAYS set:

setFocusableInTouchMode(true);

this is hard coded in its constructor.

so if you apply those attribute in your XML for a RecyclerView

android:focusable="false"
android:focudeableInTouchMode="false"

that would be useless, you end up with recycleView.isFocusable() == true...

so the most elegant solution is to disable foucusable for RecyclerView's Parent.

<LinearLayout
    android:focusable="false"
    android:focusableInTouchMode="false"
    android:descendantFocusability="blocksDescendants"
    ...
    >
    <android.support.v7.widget.RecyclerView
        ...
    />
/>

or you can just simply setFocusable(false)

Upvotes: 30

Rahul Devanavar
Rahul Devanavar

Reputation: 4175

This problem because of recyclerView has default focus.

Solution

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:descendantFocusability="blocksDescendants"
    android:orientation="vertical">

Add android:descendantFocusability="blocksDescendants" to your immediate layout of scrollView

Upvotes: 84

Todor Kostov
Todor Kostov

Reputation: 1839

Check out clearFocus() from here Android Dev Doc.

You can set a DrawerListener to your navigation drawer and use the onDrawerStateChanged() or some of the other options from here to call clearFocus() on your RecyclerView.

Upvotes: 3

Related Questions