Skip to content Skip to sidebar Skip to footer

Android Nested Listview Adapter Adding Only First Element

My purpose is a list in a list like this: -alllist-------- ----row--------- ----row--------- -----(list)----- -------textview -------textview ---row--------- -----(list)---- .....

Solution 1:

I've a similar problem when i put a ListView inside a ScrollView.

The problem was: setting my ListView to wrap_content inside a scrollable view (ScrollView in my case) will only make it span around the first child while the others would be in the overflow.

As workaround: after updating adapter data (e.g adding new item), i also update the ListView's height (based on children) from code.

This is an example:

publicclassUtils {

    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);
        listView.requestLayout();
    }     
}

Post a Comment for "Android Nested Listview Adapter Adding Only First Element"