I am trying to make this type of layout using setSpanSizeLookup
by GridLayoutManager
.
Does anyone know how to autofit an item by calculating width of the item and auto adjust like image?
If there is some another way then please suggest me.
Here is some code:
int maxWidth = 1040; // device width
mLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
LayoutInflater vi = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
View v = vi.inflate(R.layout.row_favorite, null);
final TextView textView = (TextView) v.findViewById(R.id.tv_name);
int itemWidth = (int) (textView.getPaint().measureText(favoriteList.get(position).getName()));
if ((maxWidth / 2) / 3 > itemWidth) {
return 1;
} else if ((maxWidth / 2) / 3 < itemWidth && maxWidth / 3 > itemWidth) {
return 2;
} else if (maxWidth / 2 > itemWidth && maxWidth / 3 < itemWidth) {
return 3;
}
}
});