在 LinearLayout 中滚动列表视图,该布局包装在 ScrollView 中



我有一个应该可滚动的活动,因此我用ScrollView包围了它。我有两个 listViews,我不想像显示所有项目那样大。

如果我能设置一个 maxHeight 属性,那就太好了,这样如果只有 2 个项目,就不会有空格,但如果有 50 个项目,我一次只想看到其中的 5 个。不幸的是,没有这样的属性,所以我决定将高度设置为固定数字。任何如何更正确地做到这一点的建议将不胜感激。

然而,主要问题是,当我尝试滚动其中一个列表视图时,整个线性布局向下滚动。(如果我在滚动列表时使用另一根手指将 LinearLayout "固定"到位,我可以避免这种情况,但这肯定不是解决方案。

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_settings"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="app.ballmaschine.pages.SettingsPage">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <!-- Some other stuff here  -->
    <TextView
        android:text="Eine Maschine"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAppearance="@android:style/TextAppearance.DeviceDefault.Medium"
        android:layout_marginTop="8dp" />
    <ListView
        android:layout_width="match_parent"
        android:id="@+id/lvCalibsM1"
        android:layout_height="200dp" />
    <TextView
        android:text="Zwei Maschinen"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAppearance="@android:style/TextAppearance.DeviceDefault.Medium"
        android:layout_marginTop="8dp" />
    <ListView
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:id="@+id/lvCalibsM2"/>
    <!-- Some more stuff here  -->
</LinearLayout>

将其添加到列表视图

android:scrollbars = "none"

这在 Java 代码中

listView.setNestedScrollingEnabled(false)

在这些情况下,请始终尝试NestedScrollView,并将此行添加到其中

 app:layout_behavior="@string/appbar_scrolling_view_behavior"

最新更新