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
Android Background Drawable Not Working in Button Since Android Studio 4.1
How to Replicate Android:Editable="False" in Code
How to Set Text to View from Drawer Header Layout in Navigation Drawer Without Inflating View
Google Maps Fragment Returning Null Inside a Fragment
How to Write a Drawable Resource to a File
How to Programmatically Get the List of Registered Users in Firebase
Get Nfc Tag with Ndef Android Application Record (Aar)
How to Change Line Color in Edittext
How to Get a Jacoco Coverage Report Using Android Gradle Plugin 0.10.0 or Higher
Httpurlconnection Worked Fine in Android 2.X But Not in 4.1: No Authentication Challenges Found
How to Check If "Android.Permission.Package_Usage_Stats" Permission Is Given
Android Studio: Duplicate Files Copied in APK Meta-Inf/Dependencies When Compile
Image Share Intent Works for Gmail But Crashes Fb and Twitter
Android: Programmatically Adding Buttons to a Layout
Jpeg Images Have Different Pixel Values Across Multiple Devices