当包含 ListViews 时,ScrollView 不可滚动
ScrollView not scrollable when contains ListViews
我有一个包含一些元素的 ScrollView,在其余元素之间,有两个 ListView。每个 ListView 中的项目数量是 有限,我 不 希望它们滚动。但是,我确实希望 ScrollView 本身是可滚动的,但出于某种原因,它不是。这个问题有什么好的解决方案?
几乎所有我读到的地方都说我不应该将 ListViews 放在 ScrollView 中,但我需要一些看起来类似于此布局的东西,并且实际起作用(包括标题,如果相应的列表是空,反之亦然)-
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@color/light_gray_real">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:elevation="15dp">
<EditText
android:id="@+id/etSearch"
android:layout_width="match_parent"
android:layout_height="45dp"
android:hint="Search values here"
android:textCursorDrawable="@null"
android:paddingStart="10dp"
android:background="@android:color/transparent"/>
<ImageButton
android:id="@+id/btnClear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|center"
android:layout_margin="5dp"
android:src="@drawable/places_ic_clear"
android:background="@android:color/transparent"/>
</FrameLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:isScrollContainer="true">
<TextView
android:id="@+id/tvTitle1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Title 1"
android:paddingStart="10dp"
android:paddingTop="2dp"
android:paddingBottom="2dp"
android:visibility="gone"/>
<ListView
android:id="@+id/listview1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white"/>
<TextView
android:id="@+id/tvTitle2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Title 2"
android:paddingStart="10dp"
android:paddingTop="2dp"
android:paddingBottom="2dp"
android:visibility="gone"/>
<ListView
android:id="@+id/listview2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white"/>
</LinearLayout>
</ScrollView>
由于 scrollview 和 listview 不支持嵌套滚动,因此您不能使用它们。您可以为此自定义滚动视图或
您可以使用 nestedscrollview 代替 scrollview,使用 Recyclerview 代替 listview。
我有一个包含一些元素的 ScrollView,在其余元素之间,有两个 ListView。每个 ListView 中的项目数量是 有限,我 不 希望它们滚动。但是,我确实希望 ScrollView 本身是可滚动的,但出于某种原因,它不是。这个问题有什么好的解决方案?
几乎所有我读到的地方都说我不应该将 ListViews 放在 ScrollView 中,但我需要一些看起来类似于此布局的东西,并且实际起作用(包括标题,如果相应的列表是空,反之亦然)-
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@color/light_gray_real">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:elevation="15dp">
<EditText
android:id="@+id/etSearch"
android:layout_width="match_parent"
android:layout_height="45dp"
android:hint="Search values here"
android:textCursorDrawable="@null"
android:paddingStart="10dp"
android:background="@android:color/transparent"/>
<ImageButton
android:id="@+id/btnClear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|center"
android:layout_margin="5dp"
android:src="@drawable/places_ic_clear"
android:background="@android:color/transparent"/>
</FrameLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:isScrollContainer="true">
<TextView
android:id="@+id/tvTitle1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Title 1"
android:paddingStart="10dp"
android:paddingTop="2dp"
android:paddingBottom="2dp"
android:visibility="gone"/>
<ListView
android:id="@+id/listview1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white"/>
<TextView
android:id="@+id/tvTitle2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Title 2"
android:paddingStart="10dp"
android:paddingTop="2dp"
android:paddingBottom="2dp"
android:visibility="gone"/>
<ListView
android:id="@+id/listview2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white"/>
</LinearLayout>
</ScrollView>
由于 scrollview 和 listview 不支持嵌套滚动,因此您不能使用它们。您可以为此自定义滚动视图或
您可以使用 nestedscrollview 代替 scrollview,使用 Recyclerview 代替 listview。