1

I have a layout defined as the following.

<ScrollView
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="10dp">

        <LinearLayout
            android:id="@+id/section1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="20dp"
                android:text="@string/anomaly_products_title"
                android:textColor="@color/title"
                android:textSize="16sp"
                android:textStyle="bold" />

            <ListView
                android:id="@+id/section1_list"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="40dp"
                android:divider="@android:color/transparent"
                android:dividerHeight="20dp" />
        </LinearLayout>

        .... Some Other section

    </LinearLayout>
</ScrollView>

I also defined the item of the list as follows.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="13dp"
    android:background="@drawable/semi_rounded">

    <CheckBox
        android:id="@+id/checkbox_item"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="16sp" />
</LinearLayout>

I would like to set the height of the ListView to the height of all his elements and make it non-scrollable. The scroll must only occur on the parent ScrollView. For that, I have found the function setListViewHeightBasedOnChildren here.

The problem is that my checkbox label can be displayed on multiple lines and thus increase the size of the item slightly. With many checkboxes in this situation, the ListView becomes scrollable.

Is There any way to handle that case?

Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98
Xiidref
  • 1,456
  • 8
  • 20

1 Answers1

0

I would like to suggest adding headers and footers to your ListView instead of a ScrollView and ListView combination. This is simple and easier to implement. Here's a nice tutorial on how you might add headers/footers in a ListView.

Handling the height of the ListView based on the items count and item size is difficult and hard to optimize the overall process. You can also consider using a RecyclerView instead of ListView. It has the option to add headers and footers as well. Here's an implementation.

For adding multiple lists (multiple sections in your case), you might consider looking into this sample Github Project which might give you a lead on how you might achieve the behavior that you want using a RecyclerView.

Hence, remove the ScrollView. Put the contents which are above of your ListView in a separate layout file and do the same for the contents below your ListView. Then add them as a header and footer accordingly.

Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98
  • what if there is more content in the scrollview that is not part of a list footer or header? – Tim Jun 24 '19 at 09:07
  • Can you please give me an example? I am actually confused about the more contents. The `ListView` is somewhere in the middle of the whole layout as I could understand from the question. Hence, everything which is above the `ListView` can be set as a header and everything below it can be set as a footer, which I was suggesting. – Reaz Murshed Jun 24 '19 at 09:09
  • I understand now. Thank you for the clarification. You can still achieve that using a `RecyclerView` as well. I am sharing a sample [Github Project](https://github.com/masudias/dynamic-recyclerview) which I think is applicable here. – Reaz Murshed Jun 24 '19 at 09:19