【问题标题】:ListView displays only the first item when the screen is rotatedListView 在屏幕旋转时只显示第一项
【发布时间】:2021-08-22 14:27:26
【问题描述】:

我正在开发一个 Android 应用程序,但我目前遇到了片段布局问题。

此布局由NestedScrollView 组成,其中包括LinearLayoutTextViews 和ListViews。

当我的屏幕处于垂直位置时,一切正常。我的问题是,当我旋转屏幕时,只显示每个ListView 的第一项,而不是全部内容。

这是我的代码:

<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ui.memory.MemoryFragment"
    android:gravity="center"
    android:background="@color/white"
    android:fillViewport="true">

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

        <TextView
            android:id="@+id/tv_icmanuf_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/grey"
            android:padding="8dp"
            android:text="@string/ic_manufacturer"
            android:textAlignment="textStart"
            android:textColor="@color/white"
            android:textSize="16sp" />

        <TextView
            android:id="@+id/tv_icmanuf_value"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="8dp"
            android:text=""
            android:textAlignment="textStart"
            android:textSize="16sp" />

        <TextView
            android:id="@+id/tv_ids_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/grey"
            android:padding="8dp"
            android:text="@string/uid"
            android:textAlignment="textStart"
            android:textColor="@color/white"
            android:textSize="16sp" />

        <ListView
            android:id="@+id/lv_ids"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:divider="@null"
            android:dividerHeight="0dp" />

        <TextView
            android:id="@+id/tv_suptech_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/grey"
            android:padding="8dp"
            android:text="@string/supported_technologies"
            android:textAlignment="textStart"
            android:textColor="@color/white"
            android:textSize="16sp" />

        <TextView
            android:id="@+id/tv_suptech_value"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="8dp"
            android:text=""
            android:textAlignment="textStart"
            android:textSize="16sp" />

        <TextView
            android:id="@+id/tv_andtech_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/grey"
            android:padding="8dp"
            android:text="@string/android_technologies"
            android:textAlignment="textStart"
            android:textColor="@color/white"
            android:textSize="16sp" />

        <ListView
            android:id="@+id/lv_andtech"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:divider="@null"
            android:dividerHeight="0dp" />

        <TextView
            android:id="@+id/tv_originsign_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/grey"
            android:padding="8dp"
            android:text="@string/originality_check"
            android:textAlignment="textStart"
            android:textColor="@color/white"
            android:textSize="16sp" />

        <TextView
            android:id="@+id/tv_originsign_value"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="8dp"
            android:text=""
            android:textAlignment="textStart"
            android:textSize="16sp" />

    </LinearLayout>
</androidx.core.widget.NestedScrollView>

【问题讨论】:

标签: android android-listview android-linearlayout android-nestedscrollview


【解决方案1】:

首先,我想说的是,基于 ScrollView (NestedScrollView) 的文档,他们说:

永远不要将 RecyclerView 或 ListView 添加到滚动视图。这样做会导致糟糕的用户界面性能和糟糕的用户体验。

所以你应该避免这样做。

其次,当您的 NestedScrollView 中的内容大于屏幕时,您的列表视图将只显示 1 个项目。您在垂直模式下收到的成功行为是假的,因为内容没有溢出。

如果您真的想要在 NestedScrollView 中使用 ListView,则必须设置 ListView 的高度,例如:200dp,这导致您必须滚动才能看到它的全部内容。如果您的列表视图可以显示所有内容,这是有道理的item 一次,不需要从一开始就包含在 NestedScrollView 中。

【讨论】:

    猜你喜欢
    • 2016-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多