Android Nested Listview

android nested listview

I had the same problem today, so this is what I did to solve it:

I have a ListView, with a CustomAdapter, and on the getView of the customAdapter, I have something like this:

LinearLayout list = (LinearLayout) myView.findViewById(R.id.list_musics);
list.removeAllViews();

for (Music music : albums.get(position).musics) {
View line = li.inflate(R.layout.inside_row, null);

/* nested list's stuff */

list.addView(line);
}

So, resuming, It's not possible to nest to ListViews, but you can create a list inside a row using LinearLayout and populating it with code.

Dynamic nested listviews in android

You shouldn't try to nest two list views – both are scrolling views and the UX becomes horrible. What you should do instead is use ExpandableListAdapter, which lets you make a list of parent (group) and child views. It's possible to have multiple children per group, so in practice, you have nested lists, but in reality, it's a single ListView.

Nested Listview only shows first item

Its not a good idea to put a scrollable view inside another scrollable view. Try to solve your problem using getItemViewType method.

If you don't have any other option, then you have to measure inner ListView height. This way all item will be inflated at the same time, you can't take advantage of recycle property of ListView.

To measure inner ListView use this method

/**** Method for Setting the Height of the ListView dynamically. 
**** Hack to fix the issue of not showing all the items of the ListView
**** when placed inside a ScrollView ****/
public static void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null)
return;

int desiredWidth = MeasureSpec.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();
}

Usage:

setListViewHeightBasedOnChildren(listView)

This way you inner LisView will lose recycle property.

There is other questions about this, You can check those too,

Check this and this

How to use nested Listview in android

You can use Cards Lib

Examples:

  • Card With
    List
  • Cardexpand-And-CardListView

And you can customize the card's internal layout to use according to what you need.



Related Topics



Leave a reply



Submit