I have a Gridview that I don't want a constant row height. I want the row height to vary to accommodate the tallest content in each row. I am currently using https://stackoverflow.com/a/7568226/1149456 (modified so i can resume with multiple adapters) as my solution but I have ran into a problem. The rows are all re-sized correctly until I open in a second fragment.
For example, I open a list of achievement categories in a fragment fragment 1 and when you select a category a new fragment is opened with a list of sub-categories in fragment 2. The first item is not re-sized if the second is larger such as here in this screenshot.
If i open fragment 2 first it re-sizes correctly, or if i scroll up and down it resizes correctly like this screenshot.
AutoMeasureGridView.java
public class AutoMeasureGridView extends GridView {
private ListAdapter mAdapter;
public AutoMeasureGridView(Context context) {
super(context);
}
public AutoMeasureGridView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public AutoMeasureGridView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public void setAdapter(ListAdapter adapter) {
this.mAdapter = adapter;
super.setAdapter(adapter);
super.setAdapter(this.mAdapter);
}
@Override
public ListAdapter getAdapter() {
return this.mAdapter;
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
if(changed) {
//TutorialAdapter adapter = (TutorialAdapter)getAdapter();
int numColumns = getNumColumns();
if(getAdapter() != null) {
GridViewItemLayout.initItemLayout(numColumns, getAdapter().getCount());
}
/*if(numColumns > 1) {
int columnWidth = getMeasuredWidth() / numColumns;
adapter.measureItems(columnWidth);
}*/
}
super.onLayout(changed, l, t, r, b);
}
}
The original code calls adapter.measureItems(columnWidth); but i found the code for the most part worked without it and I would be unable to reuse the code with multiple adapters by calling the custom function.
GridViewItemLayout.java public class GridViewItemLayout extends LinearLayout {
// Array of max cell heights for each row
private static int[] mMaxRowHeight;
// The number of columns in the grid view
private static int mNumColumns;
// The position of the view cell
private int mPosition;
// Public constructor
public GridViewItemLayout(Context context) {
super(context);
}
// Public constructor
public GridViewItemLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
/**
* Set the position of the view cell
* @param position
*/
public void setPosition(int position) {
mPosition = position;
}
/**
* Set the number of columns and item count in order to accurately store the
* max height for each row. This must be called whenever there is a change to the layout
* or content data.
*
* @param numColumns
* @param itemCount
*/
public static void initItemLayout(int numColumns, int itemCount) {
mNumColumns = numColumns;
mMaxRowHeight = new int[itemCount];
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
// Do not calculate max height if column count is only one
if(mNumColumns <= 1 || mMaxRowHeight == null) {
return;
}
// Get the current view cell index for the grid row
int rowIndex = mPosition / mNumColumns;
// Get the measured height for this layout
int measuredHeight = getMeasuredHeight();
// If the current height is larger than previous measurements, update the array
if(measuredHeight > mMaxRowHeight[rowIndex]) {
mMaxRowHeight[rowIndex] = measuredHeight;
}
// Update the dimensions of the layout to reflect the max height
setMeasuredDimension(getMeasuredWidth(), mMaxRowHeight[rowIndex]);
}
}
I have tried both achievementAdapter.notifyDataSetChanged(); and achievementListView.invalidateViews(); but neither forced it to resize.
AchievementAdapter.java public class AchievementAdapter extends ArrayAdapter {
private final Context context;
private final ArrayList<AchievementsItem> swtorAchievements;
int AdvancedPos1;
int AdvancedPos2;
String type;
public AchievementAdapter(Context context, ArrayList<AchievementsItem> swtorAchievements, String type) {
super(context, R.layout.achievement_row, swtorAchievements);
this.context = context;
this.swtorAchievements = swtorAchievements;
this.type = type;
}
@Override
public View getView(int position, View convertView, final ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = null;
final AchievementsItem item = swtorAchievements.get(position);
if(item.isGroupHeader()){
rowView = inflater.inflate(R.layout.advanced_class_tab3_header, parent, false);
TextView txtHeader = (TextView) rowView.findViewById(R.id.txtAbilityTitle);
txtHeader.setText(item.getTitle());
} else {
rowView = inflater.inflate(R.layout.achievement_row, parent, false);
TextView txtViewCategory1 = (TextView) rowView.findViewById(R.id.txtCategory1);
TextProgressBar txtViewProgress = (TextProgressBar) rowView.findViewById(R.id.progressBarWithText);
if (item != null) {
if (type.equals("")) {
txtViewCategory1.setText(item.getCategory1());
} else if (type.equals("category1")) {
txtViewCategory1.setText(item.getCategory1());
} else if (type.equals("category2")) {
txtViewCategory1.setText(item.getCategory2());
} else if (type.equals("category3")) {
txtViewCategory1.setText(item.getCategory3());
}
txtViewProgress.setText("0 / " + item.getCount());
}
}
return rowView;
}
}