Listview in Android Scrollview does not display all items

I have a layout containing two listviews, both of which are in the Scrollview, because the content may exceed the height of the device. This is the layout:

<RelativeLayout 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="com.galleri5.android.activities.ImageActivity">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:elevation="4dp"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="#FAC80A">

        <RelativeLayout
            android:id="@+id/relativeLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Image"
                android:textColor="#000000"
                android:textSize="17sp"
                android:layout_centerVertical="true"
                android:id="@+id/textView80" />

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/imageView134"
                android:src="@drawable/gallery_page_ic_notifications"
                android:layout_alignParentEnd="true"
                android:layout_marginRight="30dp"
                android:layout_centerVertical="true" />

        </RelativeLayout>

    </android.support.v7.widget.Toolbar>

    <ScrollView
        android:id="@+id/scrollView2"
        android:layout_below="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/user_profile_bg"
            android:orientation="vertical">

            <ImageView
                android:id="@+id/imageView102"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:layout_marginBottom="25dp"
                android:layout_marginTop="10dp"
                android:adjustViewBounds="true" />

            <ImageView
                android:id="@+id/imageView103"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:layout_marginBottom="25dp"
                android:src="@drawable/user_profile_activity_1_shape_thin_line_separator" />

            <TextView
                android:id="@+id/textView38"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="10dp"
                android:layout_marginLeft="30dp"
                android:layout_marginRight="30dp"
                android:alpha="0.8"
                android:inputType="textMultiLine"
                android:textColor="#FFFFFF" />

            <TextView
                android:id="@+id/textView39"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="25dp"
                android:layout_marginLeft="30dp"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:textColor="#707070" />

            <ImageView
                android:id="@+id/imageView104"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:layout_marginBottom="25dp"
                android:src="@drawable/user_profile_activity_1_shape_thin_line_separator" />

            <LinearLayout
                android:id="@+id/linearLayout3"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="20dp"
                android:layout_marginLeft="30dp"
                android:layout_marginRight="30dp"
                android:orientation="horizontal">

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">

                    <ImageView
                        android:id="@+id/imageView107"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_centerVertical="true"
                        android:paddingRight="5dp" />

                    <LinearLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_centerVertical="true"
                        android:layout_toRightOf="@+id/imageView107"
                        android:orientation="vertical"
                        android:paddingLeft="5dp">

                        <TextView
                            android:id="@+id/textView45"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:alpha="0.8"
                            android:paddingBottom="2dp"
                            android:textColor="#FFFFFF" />

                        <TextView
                            android:id="@+id/textView46"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:paddingTop="2dp"
                            android:textAppearance="?android:attr/textAppearanceSmall"
                            android:textColor="#707070"
                            android:textSize="12sp" />
                    </LinearLayout>

                    <ImageView
                        android:id="@+id/imageView108"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentEnd="true"
                        android:layout_centerVertical="true"
                        android:src="@drawable/image_page_button_bookmark" />
                </RelativeLayout>
            </LinearLayout>

            <ImageView
                android:id="@+id/imageView109"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:layout_marginBottom="10dp"
                android:src="@drawable/user_profile_activity_1_shape_thin_line_separator" />

            <android.support.v7.widget.CardView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="35dp"
                android:layout_marginLeft="30dp"
                android:layout_marginRight="30dp">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="#343031"
                    android:orientation="vertical">

                    <TextView
                        android:id="@+id/textView47"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="25dp"
                        android:layout_marginTop="15dp"
                        android:text="This Image is Part Of..."
                        android:textColor="#707070" />

                    <ImageView
                        android:id="@+id/imageView110"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_horizontal"
                        android:layout_marginBottom="20dp"
                        android:layout_marginTop="10dp"
                        android:src="@drawable/user_profile_activity_1_shape_thin_line_separator" />

                    <ListView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:id="@+id/listView2" />

                    <TextView
                        android:id="@+id/textView57"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginBottom="20dp"
                        android:layout_marginLeft="30dp"
                        android:text="See All Galleries"
                        android:textColor="#707070" />

                </LinearLayout>


            </android.support.v7.widget.CardView>

            <ImageView
                android:id="@+id/imageView125"
                android:layout_width="305dp"
                android:layout_height="wrap_content"
                android:layout_gravity="right"
                android:layout_marginBottom="20dp"
                android:src="@drawable/image_page_belongs_to_shape_line_separator_1" />

            <ListView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/listView6" />

            <EditText
                android:id="@+id/editText6"
                android:layout_width="300dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="40dp"
                android:alpha="0.8"
                android:inputType="text"
                android:elegantTextHeight="true"
                android:enabled="true"
                android:hint="Comment"
                android:textCursorDrawable="@null"
                android:textColor="#FFFFFF"
                android:textColorHint="#FFFFFF"
                android:layout_marginTop="10dp"
                android:backgroundTint="#fac80a" />

        </LinearLayout>
    </ScrollView>

</RelativeLayout>

The IDs of these two list views are listview2 and listview6. I have correctly set the adapters for the two listviews. I am getting data from the back end. For these two listviews, I get two items from the back end, but actually only one item is displayed in each listview. I also tried to add the item to the second listview using notifydatasetchanged(), But it doesn't work properly. I only get one item. What may be the reason and how to solve it?

resolvent:

This is what I do in case others encounter similar problems:

Note: this solution is inefficient. I'm using it because my listview contains at most 2 items. If you have a listview with a large number of items, I won't recommend it to others

public static void setListViewHeightBasedOnChildren(ListView listView) {
              listadapter listadapter = listView.getAdapter();
            if (listadapter == null) {
            // pre-condition
                  return;
            }

            int totalHeight = listView.getPaddingTop() + listView.getPaddingBottom();
            for (int i = 0; i < listadapter.getCount(); i++) {
                 View listItem = listadapter.getView(i, null, listView);
                 if (listItem instanceof ViewGroup) {
                    listItem.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
                 }
                 listItem.measure(0, 0);
                 totalHeight += listItem.getMeasuredHeight();
            }

            ViewGroup.LayoutParams params = listView.getLayoutParams();
            params.height = totalHeight + (listView.getDividerHeight() * (listadapter.getCount() - 1));
                      listView.setLayoutParams(params);
        }

After setting the adapter, I called this function on both listviews. I hope it can help you

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>