Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>
like image 767
Samad Avatar asked Jun 16 '16 05:06

Samad


4 Answers

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

like image 96
Rahul Devanavar Avatar answered Oct 20 '22 02:10

Rahul Devanavar


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)

like image 24
landerlyoung Avatar answered Oct 20 '22 04:10

landerlyoung


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.

like image 3
Todor Kostov Avatar answered Oct 20 '22 02:10

Todor Kostov


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

like image 3
apurv thakkar Avatar answered Oct 20 '22 04:10

apurv thakkar