I have a complex xml file and I really need a ScrollView. Please Don\'t mind the Ids as I have changed them. The ScrollView here doesn\'t work.
It can be done, although you should not put a listview inside a scrollview but sometimes this is the easier way: https://stackoverflow.com/a/3495908/1117338
Answer from a Google employee that works in Android:
Do not use ListView inside ScrollView
https://plus.google.com/107708120842840792570/posts/T5AgJ7jiK88
I would recommend to use listview with section headers ...i think that is what you want to implement...
This may be helpful https://learnlinky.com/2016/11/30/listview-section-header-android/
the answer given by reddy raz work only if we set fixed height of list items
You can set it by engulfing ListView in a parent layout like an 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/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">
<TextView
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">
<TextView
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 listview is populated -
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 is, we set the ScrollView height to match_parent
. And after the ListView is populated we defined each parent layout of ListView to its ListView item count. And, remember to change the value of eachitemSize
according to your each cell size.
Another thing to care about is after the LayoutParams is assigned its margin becomes invalid due to the new LayoutParams. So, use <Space/>
instead of margin
like the example above.
Hope it helps!!
Use Like this: THIS WORKED LIKE CHARM
Remove Linear layout. use relative layout and inside that place your two list view like this.
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scrollojt"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#f00"></ListView>
<ListView
android:id="@+id/listView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/listView1"
android:background="#0f0"></ListView>
</RelativeLayout>
</ScrollView>
add Utility.java
public class Utility {
public static void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
// pre-condition
return;
}
int totalHeight = 0;
int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.AT_MOST);
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, listView);
listItem.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
listView.requestLayout();
}
}
In your Activity:
lv1.setAdapter(adapter);
lv2.setAdapter(adapter);
Utility.setListViewHeightBasedOnChildren(lv1);
Utility.setListViewHeightBasedOnChildren(lv2);
Thanks to #Nirmal for the actual answer https://stackoverflow.com/a/17693628/1403112
ScrollView
and ListView
both use scrolling. So in my opinion they shouldn't be used together. Try using RelativeLayout
or some other layout instead of ScrollView