You can install it by including the ListView in the parent layout, as in the example below -
<?xml version="1.0" encoding="utf-8"?> <androidx.core.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <androidx.cardview.widget.CardView android:id="@+id/cardRange" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="@dimen/fab_margin" android:layout_marginBottom="6dp" app:cardElevation="3dp"> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <in.indilabz.fillip_ecommerce.customs.light.TextViewRobotoLight android:id="@+id/priceRangeTitle" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Price Range" android:layout_marginTop="@dimen/fab_margin" android:layout_marginLeft="@dimen/fab_margin" android:layout_marginRight="@dimen/fab_margin" android:textAppearance="@style/TextAppearance.AppCompat.Small" android:textColor="@color/md_black_1000"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:weightSum="3" android:layout_marginTop="8dp" android:layout_marginLeft="8dp" android:layout_marginRight="8dp" android:layout_marginBottom="@dimen/fab_margin" android:layout_below="@+id/priceRangeTitle"> <EditText android:layout_width="0dp" android:layout_height="50dp" android:layout_weight="1" android:layout_marginLeft="4dp" android:maxLines="1" android:maxLength="6" android:inputType="number" android:padding="2dp" android:gravity="center" android:background="@drawable/edit_text_rect"/> <in.indilabz.fillip_ecommerce.customs.bold.TextViewRobotoBold android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:textColor="@color/md_black_1000" android:text="- to -"/> <EditText android:layout_width="0dp" android:layout_height="50dp" android:layout_weight="1" android:layout_marginRight="4dp" android:maxLines="1" android:maxLength="6" android:inputType="number" android:padding="2dp" android:gravity="center" android:background="@drawable/edit_text_rect"/> </LinearLayout> </RelativeLayout> </androidx.cardview.widget.CardView> <androidx.cardview.widget.CardView android:id="@+id/cardBrand" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="@dimen/fab_margin" android:layout_below="@+id/cardRange" app:cardElevation="3dp"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <in.indilabz.fillip_ecommerce.customs.light.TextViewRobotoLight android:id="@+id/brandTitle" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Brands" android:layout_marginTop="@dimen/fab_margin" android:layout_marginLeft="@dimen/fab_margin" android:layout_marginRight="@dimen/fab_margin" android:textAppearance="@style/TextAppearance.AppCompat.Small" android:textColor="@color/md_black_1000"/> <ListView android:id="@+id/brandList" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginTop="8dp" android:layout_marginLeft="8dp" android:layout_marginRight="8dp" android:layout_marginBottom="@dimen/fab_margin" /> </LinearLayout> </androidx.cardview.widget.CardView> <Space android:layout_width="match_parent" android:layout_height="6dp" /> <androidx.cardview.widget.CardView android:id="@+id/cardRating" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="@dimen/fab_margin" android:layout_below="@+id/cardBrand" app:cardElevation="3dp"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <in.indilabz.fillip_ecommerce.customs.light.TextViewRobotoLight android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Ratings" android:layout_marginTop="@dimen/fab_margin" android:layout_marginLeft="@dimen/fab_margin" android:layout_marginRight="@dimen/fab_margin" android:textAppearance="@style/TextAppearance.AppCompat.Small" android:textColor="@color/md_black_1000"/> <ListView android:id="@+id/ratingList" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginTop="8dp" android:layout_marginLeft="8dp" android:layout_marginRight="8dp" android:layout_marginBottom="@dimen/fab_margin" /> </LinearLayout> </androidx.cardview.widget.CardView> <Space android:layout_width="match_parent" android:layout_height="6dp" /> </LinearLayout> </androidx.core.widget.NestedScrollView>
And, after the list is full -
eachitemSize = 180; cardBrand.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, eachitemSize * brandsAdapter.getCount())); cardRating.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, eachitemSize * ratingAdapter.getCount()));
What we did above, we set match_parent height. And after the ListView is populated, we defined each parent ListView layout with its number of ListView elements. And don't forget to change eachitemSize fit the size of each cell.
Another thing to take care of is that after LayoutParams is assigned, its field becomes invalid due to new LayoutParams. So use <Space/> instead of margin as in the example above.
Hope it helps !!
source share