Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save vincent1086/4c3ac59e0a8ac9b260448c4d75e85e07 to your computer and use it in GitHub Desktop.
Save vincent1086/4c3ac59e0a8ac9b260448c4d75e85e07 to your computer and use it in GitHub Desktop.
Android - ListAdapter Children Measure
private int measureContentWidth(ListAdapter adapter) {
// Menus don't tend to be long, so this is more sane than it looks.
int width = 0;
View itemView = null;
int itemType = 0;
final int widthMeasureSpec =
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
final int heightMeasureSpec =
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
final int count = adapter.getCount();
for (int i = 0; i < count; i++) {
final int positionType = adapter.getItemViewType(i);
if (positionType != itemType) {
itemType = positionType;
itemView = null;
}
if (mMeasureParent == null) {
mMeasureParent = new FrameLayout(mContext);
}
itemView = adapter.getView(i, itemView, mMeasureParent);
itemView.measure(widthMeasureSpec, heightMeasureSpec);
width = Math.max(width, itemView.getMeasuredWidth());
}
return width;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment