Scrollview With A Listview Doesn't Scroll - Android
Solution 1:
I spent days trying to figure out how to achieve this and couldn't find any solution. You should not put a ListView inside a ScrollView
was the common saying everywhere I searched. I didn't want to use LinearLayout or ViewGroup because I had already created the whole UI using ListView and it looked awesome to me and everyone else. It worked well except that the page didn't scroll.
Recently I stumbled upon a question here and thought to give this answer a try. It works flawlessly!
Here's the solution:
publicclassUtility {
publicstaticvoidsetListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
// pre-conditionreturn;
}
int totalHeight = 0;
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, listView);
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
}
}
Just call Utility.setListViewHeightBasedOnChildren(yourListView)
after you have assigned the adapter to your listview and you're done!
A big thanks to DougW
for coming up with the answer. Here is the original link How can I put a ListView into a ScrollView without it collapsing?
Solution 2:
You shouldn't put listview in to scrollview, it's not a good idea. If you don't want it to scroll, maybe you shouldn't use listviews. Have you tried with LinearLayouts?
Solution 3:
Instead of putting two listview you can use one listview and you can add both data or inflate both xml file in the adapter of that listview. This will show both data in one listview and scroll the data with one scroll bar.
Solution 4:
Before calling the below method,
publicstaticvoidsetListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
// pre-conditionreturn;
}
int totalHeight = 0;
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, listView);
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
}
You must remove the property 'android:fillViewport="true" from scrollview. If not, scrolling not works.
Solution 5:
Try this way:
You removed scroll view from your layout because that is not working correctly for list view..
And also you put the height "android:layout_height="fill_parent"
for listview so that is occupy full height of parent layout.
so please fix some height manually for both listview.
Post a Comment for "Scrollview With A Listview Doesn't Scroll - Android"