Skip to content

Instantly share code, notes, and snippets.

@Longwater1234
Created October 29, 2021 09:04
Show Gist options
  • Save Longwater1234/06a4b8b2e06cbd00c3ba4eabcff702c7 to your computer and use it in GitHub Desktop.
Save Longwater1234/06a4b8b2e06cbd00c3ba4eabcff702c7 to your computer and use it in GitHub Desktop.
display two listViews on one activity
/**
* For auto-adjusting ListView height
* @see "https://stackoverflow.com/a/28713754"
*/
public class ListUtils {
public static void setDynamicHeight(ListView mListView) {
ListAdapter mListAdapter = mListView.getAdapter();
if (mListAdapter == null) {
// when adapter is null
return;
}
int height = 0;
int desiredWidth = View.MeasureSpec.makeMeasureSpec(mListView.getWidth(), View.MeasureSpec.UNSPECIFIED);
for (int i = 0; i < mListAdapter.getCount(); i++) {
View listItem = mListAdapter.getView(i, null, mListView);
listItem.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);
height += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = mListView.getLayoutParams();
params.height = height + (mListView.getDividerHeight() * (mListAdapter.getCount() - 1));
mListView.setLayoutParams(params);
mListView.requestLayout();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment