2

I have added Button and ListView on a ScrollView. Button is added below to the ListView in ScrollView but Problem is that it shows large space between button and ListView. Here is my code

ReservationDrinkListAdapter adp = new ReservationDrinkListAdapter(this,
            KukumberApplication.getInstance().getBottleService().menuItems);
        list.setAdapter(adp);
        Util.setListViewHeightBasedOnChildren(list);

following method is used to show ListView in ScrollView.

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

       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);
   }

What am I doing wrong? Any Suggestion and sample code Would be appreciated.

AndroidLearner
  • 4,500
  • 4
  • 31
  • 62

1 Answers1

0

Using a listview inside of a scrollview is technically possible but is not at all recommended, doing as you have (expanding the whole list so the scrollview accomodates all the list) goes against the whole point of adapterviews (performance) and will cause you lots of issues such as those you have encountered.

You can see more about this in answers such as: How can I put a ListView into a ScrollView without it collapsing? How to add two listview in scrollview Why ListView cannot be used in a ScrollView? ListView inside ScrollView is not scrolling on Android

And there are even more out there.

Community
  • 1
  • 1
Paul Harris
  • 5,769
  • 1
  • 25
  • 41