Tuesday, 20 August 2013

Including the list view into the scroll view

Although the Google guy doesn't want us to include the list view into the scroll view (See the pic below), there is a good way to do it.

Source: Google+

The best way to do it is set the height of the ListView such way that it doesn't need any scroll inside, so the unique scroll will be provided by the ScrollView.

here is the source code found in StackOverflow:

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

Source:
StackOverflow - How can I put a ListView into a ScrollView without it collapsing?

No comments:

Post a Comment