GridLayoutManager - how to auto fit columns?

后端 未结 8 2033
野性不改
野性不改 2020-11-30 18:30

I have a RecyclerView with a GridLayoutManager that displays Card Views. I want the cards to rearrange according to the screen size (the Google Play app does this kind of th

8条回答
  •  Happy的楠姐
    2020-11-30 19:30

    Use this function, and put margins for cell layout in XML instead using decoration.

    public int getNumberOfColumns() {
            View view = View.inflate(this, R.layout.row_layout, null);
            view.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
            int width = view.getMeasuredWidth();
            int count = getResources().getDisplayMetrics().widthPixels / width;
            int remaining = getResources().getDisplayMetrics().widthPixels - width * count;
            if (remaining > width - 15)
                count++;
            return count;
        }
    

提交回复
热议问题