LoginSignup
4
3

More than 5 years have passed since last update.

NestedScrollView内にRecycleViewを置くとスクロールがずれる問題について

Posted at

この記事について

NestedScrollView内にあるViewを置いてからRecyclerViewを置いた時に、RecyclerViewの上端にスクロールされてしまい、最初に置いたViewが隠れてしまう問題に遭遇したので、対処法をメモする。

問題の詳細

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

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

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="このViewが隠れてしまう" />

        <android.support.v7.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    </LinearLayout>
</android.support.v4.widget.NestedScrollView>

上記のようにレイアウトを作って、RecyclerViewにAdapterをセットした場合、RecylcerViewの上端にスクロールされた状態で表示され、TextViewは上にスクロールしないと表示されなかった。

解決策

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

        ~ 省略 ~ 

    </LinearLayout>

NestedScrollView直下のViewGroup(今回はLinearLayout)に android:focusableInTouchMode="true" をつける

参考

4
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
3