我正在尝试使用setSpanSizeLookup
by制作这种类型的布局GridLayoutManager
。
有谁知道如何通过计算项目的宽度来自动调整项目并像图像一样自动调整?
如果还有其他方法,请建议我。
这是一些代码:
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;
}
}
});