創建幫助類獲取尺寸。
public class GetSize {
public static int getColumnCount(Context context, int dp) {
if ((double) (getScreenWidth(context)/getPointOfDp(context, dp)) <= 1) return 1;
else return getScreenWidth(context)/getPointOfDp(context, dp);
}
public static int getScreenWidth(Context context){
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
Point point = new Point();
display.getSize(point);
return point.x; //Screen width
}
private static int getPointOfDp(Context context, int dp) { //Convert from dp to screen dots
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, context.getResources().getDisplayMetrics());
}
}
爲了獲得一個列數需要設置一些最小寬度的項目。
int columnCount = GetSize.getColumnCount(context, 288);
layoutManager = new StaggeredGridLayoutManager(columnCount, StaggeredGridLayoutManager.VERTICAL);
recyclerView.setLayoutManager(layoutManager);
其中thridparty StaggeredGridlayout [Rü使用 – Nithinlal
對不起,我在新的Android和你是什麼意思「thridparty」我從教程這個代碼? – fekra