Search code examples
androidandroid-listviewandroid-linearlayoutandroid-nestedscrollview

ListView displays only the first item when the screen is rotated


I am developing an Android application and I currently have a problem with a layout of a fragment.

This layout is composed of a NestedScrollView, which includes a LinearLayout with TextViews and ListViews.

When my screen is in vertical position, everything works. My problem is that when I rotate the screen, only the first item of each ListView is displayed, instead of the whole content.

Here is my code:

<?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>

Solution

  • First of all, I want to say that based on the doc for ScrollView (NestedScrollView too) they say:

    Never add a RecyclerView or ListView to a scroll view. Doing so results in poor user interface performance and a poor user experience.

    So you should avoid doing this.

    Second, when the content in your NestedScrollView larger than the screen, your list view will only show 1 item. The successful behavior you received in vertical mode was fake because the content didn't overflow.

    If you really want a ListView inside NestedScrollView, you have to set the ListView's height, ex: 200dp, that leads to you will have to scroll to see whole content of it.This make sense cause if your list view can show all item at once, there is no need to include it in the NestedScrollView from the beginning.