多数の要素を含む を含む Android レイアウトがありますscrollView
。 の下部にはscrollView
があり、listView
そこにアダプタが設定されます。
私が経験している問題は、にはすでにスクロール可能な機能があるため、 Android がlistView
を から除外していることです。 をコンテンツと同じ長さにして、マスター スクロール ビューをスクロール可能にしたいと考えています。scrollView
scrollView
listView
この動作を実現するにはどうすればよいですか?
これが私のメインレイアウトです:
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:fillViewport="true"
android:gravity="top" >
<LinearLayout
android:id="@+id/foodItemActvity_linearLayout_fragments"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
次に、プログラムでコンポーネントを id: の linearlayout に追加しますfoodItemActvity_linearLayout_fragments
。以下は、その linearlayout に読み込まれるビューの 1 つです。これが、スクロールで問題が発生するビューです。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/fragment_dds_review_textView_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Reviews:"
android:textAppearance="?android:attr/textAppearanceMedium" />
<ListView
android:id="@+id/fragment_dds_review_listView"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
次に、アダプタがこのリスト ビューを埋めます。
以下は、マスター スクロール ビューをクリックしたときの Android 階層ビューアからの画像です。
ご覧のとおり、レビューのリストビューは除外されています。
ページを下にスクロールして8件のレビューを見ることができるはずですが、実際には3件しか表示されず、レビューがある小さな部分でスクロールできます。ページ全体をスクロールできるようにしたいです。
ベストアンサー1
ScrollView内でスクロールする子ビュー。ListView、RecyclerViewなど。ScrollViewをネストされたスクロールビュー現在の xml にこれを追加すると、魔法が起こります。
以下はサンプルの XML コードです。
<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16dp"
android:paddingBottom="20dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Recycler View inside a Scroll View"
android:textColor="@color/black"
android:textSize="@dimen/_20sp"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="Below is a Recycler View as an example."
android:textSize="16sp" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
app:layout_constraintTop_toBottomOf="@id/et_damaged_qty" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="This textview automatically goes below the Recycler View."
android:textSize="16sp" />
</androidx.appcompat.widget.LinearLayoutCompat>
</androidx.core.widget.NestedScrollView>
これで、ネストされたスクロールを回避するために行った醜いハックをすべて取り除くことができます。