0

我正在尝试使用setSpanSizeLookupby制作这种类型的布局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;
        }
    }
});

请检查图像

4

2 回答 2

1

我们FlexboxLayout现在可以选择。

对于布局,

<com .google.android.flexbox.flexboxlayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     app:flexwrap="wrap">

我们还有用于动态句柄的布局管理器recyclerView

FlexboxLayoutManager layoutManager = new FlexboxLayoutManager();

享受..!!

于 2017-09-07T10:37:26.323 回答
0

添加弹性盒依赖

编译'com.google.android:flexbox:0.3.1'

并设置FlexboxLayoutManager

    FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(context);
    layoutManager.setFlexDirection(FlexDirection.ROW);
    layoutManager.setJustifyContent(JustifyContent.CENTER);
    recyclerview.setLayoutManager(layoutManager);
于 2017-10-05T11:13:27.497 回答