web-dev-qa-db-fra.com

Comment supprimer le focus de RecyclerView dans ScrollView?

J'ai un Scrollview qui contient un ImageView et RecyclerView . Si le tiroir de navigation est ouvert, puis fermé le défilement automatique RecyclerView, Comment arrêter cela?

<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>
8
El Pr0grammer

Ce problème à cause de recyclerView a le focus par défaut.

Solution

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

Ajoutez Android:descendantFocusability="blocksDescendants" à votre mise en page immédiate de scrollView 

37
Rahul Devanavar

En effet, RecyclerView a TOUJOURS défini:

setFocusableInTouchMode(true);

c'est codé en dur dans son constructeur.

donc, si vous appliquez ces attributs dans votre XML pour un RecyclerView

Android:focusable="false"
Android:focudeableInTouchMode="false"

ce serait inutile, vous vous retrouvez avec recycleView.isFocusable () == true ...

la solution la plus élégante consiste donc à désactiver foucusable pour Parent de RecyclerView.

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

ou vous pouvez simplement définir setfocusable (false)

14
landerlyoung

Découvrez clearFocus () à partir d'ici Android Dev Doc.

Vous pouvez définir un DrawerListener dans votre tiroir de navigation et utiliser le onDrawerStateChanged () ou certaines des autres options de ici pour appeler clearFocus () sur votre RecyclerView.

2
Todor Kostov

Ajoutez simplement le code suivant dans votre mise en page linéaire, fonctionne à 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">
0
Ashraf Hussain