i am working on a project with a staggered gridview that can support varying height for each nested view,
I had a similar problem and I resolved it by overriding onMeasure
method of RelativeLayout
.
package com.etsy.android.grid.util;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.RelativeLayout;
public class DynamicHeightRelativeLayout extends RelativeLayout {
public DynamicHeightRelativeLayout(Context context) {
super(context);
}
public DynamicHeightRelativeLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public DynamicHeightRelativeLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// Hack - This causes the height of RelativeLayout to match
// it's content when RelativeLayout is shown in StaggeredGridView.
heightMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
Then I used this class in XML layout file: