enable scrioll viewer if the list length is greater than 3 code example

Example 1: list view in scrollview

//Must use method whenever updating listview data
//Sets listview height to total of all individual views

public static void getListViewSize(ListView myListView) {
        ListAdapter myListAdapter = myListView.getAdapter();
        if (myListAdapter==null) {
            //do nothing return null
            return;
        }
        //set listAdapter in loop for getting final size
        int totalHeight = 0;
        for (int size = 0; size < myListAdapter.getCount(); size++) {
            View listItem = myListAdapter.getView(size, null, myListView);
            listItem.measure(0, 0);
            totalHeight += listItem.getMeasuredHeight();
        }
        //setting listview item in adapter
        ViewGroup.LayoutParams params=myListView.getLayoutParams();
        params.height=totalHeight + (myListView.getDividerHeight() * (myListAdapter.getCount() - 1));
        myListView.setLayoutParams(params);
    }

Example 2: list view in scrollview

//This method needs to be run each time the list view data changes
//It sets the height to the height of all the individual views

public static void setListViewHeightBasedOnChildren
                                              (ListView listView) {ListAdapter listAdapter = listView.getAdapter();if (listAdapter == null) return;int desiredWidth = easureSpec.makeMeasureSpec(listView.getWidth(),
                                           MeasureSpec.UNSPECIFIED);int totalHeight = 0; 
  View view = null;for (int i = 0; i < listAdapter.getCount(); i++) { 
    view = listAdapter.getView(i, view, listView);if (i == 0) view.setLayoutParams(new   
                                ViewGroup.LayoutParams(desiredWidth, 
                                        LayoutParams.WRAP_CONTENT));
   
    view.measure(desiredWidth, MeasureSpec.UNSPECIFIED); 
    totalHeight += view.getMeasuredHeight(); 
  }
 
  ViewGroup.LayoutParams params = listView.getLayoutParams();   
   
  params.height = totalHeight + (listView.getDividerHeight() * 
                                     (listAdapter.getCount() — 1));  
   
  listView.setLayoutParams(params); 
  listView.requestLayout(); 
}                                            (ListView listView) {ListAdapter listAdapter = listView.getAdapter();if (listAdapter == null) return;int desiredWidth = easureSpec.makeMeasureSpec(listView.getWidth(),                                           MeasureSpec.UNSPECIFIED);int totalHeight = 0;   View view = null;for (int i = 0; i < listAdapter.getCount(); i++) {     view = listAdapter.getView(i, view, listView);if (i == 0) view.setLayoutParams(new                                   ViewGroup.LayoutParams(desiredWidth,                                         LayoutParams.WRAP_CONTENT));       view.measure(desiredWidth, MeasureSpec.UNSPECIFIED);     totalHeight += view.getMeasuredHeight();   }   ViewGroup.LayoutParams params = listView.getLayoutParams();        params.height = totalHeight + (listView.getDividerHeight() *                                      (listAdapter.getCount() — 1));       listView.setLayoutParams(params);   listView.requestLayout(); }

Tags:

Misc Example