How to make span count and icon size automatic

不打扰是莪最后的温柔 提交于 2019-12-01 01:42:30

You can calculate number of columns. Define a static function to calculate the number of columns as:

public class Utility {
    public static int calculateNoOfColumns(Context context) {
        DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
        float dpWidth = displayMetrics.widthPixels / displayMetrics.density;
        int noOfColumns = (int) (dpWidth / 180);
        // Where 180 is the width of your grid item. You can change it as per your convention.
        return noOfColumns;
    }
}

And then, when using the GridLayoutManager in the activity or fragment you can do like this:

int mNoOfColumns = Utility.calculateNoOfColumns(getApplicationContext());

// Some code...

mGridLayoutManager = new GridLayoutManager(this, mNoOfColumns);
mRecyclerView.setLayoutManager(mGridLayoutManager);

// Some code...

I hope, this helps you.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!