web-dev-qa-db-ja.com

ScrollView内のRecyclerViewからフォーカスを削除する方法は?

ImageViewとRecyclerViewを含むScrollviewがあります。ナビゲーションドロワーを開いてからRecyclerViewの自動スクロールを閉じた場合、これを停止するにはどうすればよいですか?

<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>
14
Samad

この問題は、recyclerViewにデフォルトのフォーカスがあるためです。

解決

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

追加 Android:descendantFocusability="blocksDescendants" scrollViewの即時レイアウトに

61
Rahul Devanavar

これは、RecyclerViewが常に設定されているためです。

setFocusableInTouchMode(true);

これはコンストラクタでハードコードされています。

したがって、RecyclerViewのXMLでこれらの属性を適用する場合

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

それは役に立たないでしょう、あなたはrecycleView.isFocusable()== true ...

したがって、最もエレガントな解決策は、RecyclerViewの親に対してfoucusableを無効にすることです。

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

または単にsetFocusable(false)

20
landerlyoung

こちらからチェックアウトclearFocus()Android Dev Doc.

DrawerListenerをナビゲーションドロワーに設定し、onDrawerStateChanged()または hereclearFocus()RecyclerViewで呼び出す.

2
Todor Kostov

_Android:descendantFocusability="blocksDescendants"_を追加すると、スクロールがrecylerviewに制限されますが、レイアウト内にedittextがある場合、このプロパティはその特定のedittextのフォーカスもブロックする可能性があります。したがって、このプロパティを使用している場合は、レイアウトが読み込まれたら、kotlin/Javaクラスでこのプロパティを削除していることを確認してください。

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

0
apurv thakkar

線形レイアウトに次のコードを追加するだけで、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