Created
October 29, 2021 09:04
-
-
Save Longwater1234/06a4b8b2e06cbd00c3ba4eabcff702c7 to your computer and use it in GitHub Desktop.
display two listViews on one activity
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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