如何使StaggeredGridLayoutManager auto_fit具有所有屏幕大小,自动设置列数
recyclerViewadapter = new RecyclerViewAdapter(GetDataAdapter1, this);
StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(recyclerViewadapter);发布于 2016-11-26 07:35:20
尝尝这个
StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(getSpan(), StaggeredGridLayoutManager.VERTICAL);public int getSpan() {
int orientation = getScreenOrientation(getActivity());
if (isTV(getActivity())) return 4;
if (isTablet(getActivity()))
return orientation == Configuration.ORIENTATION_PORTRAIT ? 2 : 3;
return orientation == Configuration.ORIENTATION_PORTRAIT ? 2 : 3;
}
public static boolean isTV(Context context) {
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2 && ((UiModeManager) context
.getSystemService(Context.UI_MODE_SERVICE))
.getCurrentModeType() == Configuration.UI_MODE_TYPE_TELEVISION;
}
public static int getScreenOrientation(Context context) {
return context.getResources().getDisplayMetrics().widthPixels <
context.getResources().getDisplayMetrics().heightPixels ?
Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
}
public static boolean isTablet(Context context) {
return (context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_LARGE;
}发布于 2016-11-26 07:35:19
如果项目宽度相同,则可以这样做:
mRecyclerView.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
mRecyclerView.getViewTreeObserver().removeOnGLobalLayoutListener(this);
int viewWidth = mRecyclerView.getMeasuredWidth();
float cardViewWidth = getActivity().getResources().getDimension(R.dimen.cardview_layout_width);
int newSpanCount = (int) Math.floor(viewWidth / cardViewWidth);
mLayoutManager.setSpanCount(newSpanCount);
mLayoutManager.requestLayout();
}
});发布于 2017-10-05 19:14:03
创建用于获取大小的帮助类。
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);https://stackoverflow.com/questions/40815956
复制相似问题