Android StaggedGridLayoutManager使自动适配列

Android StaggedGridLayoutManager使自动适配列,android,gridview,android-recyclerview,Android,Gridview,Android Recyclerview,如何使StaggedGridLayoutManager自动适应所有屏幕大小,自动设置列数 recyclerViewadapter = new RecyclerViewAdapter(GetDataAdapter1, this); StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL); recyclerView.setLayo

如何使StaggedGridLayoutManager自动适应所有屏幕大小,自动设置列数

recyclerViewadapter = new RecyclerViewAdapter(GetDataAdapter1, this);
StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(recyclerViewadapter);

如果项目的宽度相同,则可以执行以下操作:

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();
            }
        });
试试这个
StaggedGridLayoutManager layoutManager=新的StaggedGridLayoutManager(getSpan(),staggedGridLayoutManager.VERTICAL)

public int getSpan(){
int-orientation=getScreenOrientation(getActivity());
if(isTV(getActivity())返回4;
如果(isTablet(getActivity()))
返回方向==Configuration.orientation\u纵向?2:3;
返回方向==Configuration.orientation\u纵向?2:3;
}
公共静态布尔isTV(上下文){
返回Build.VERSION.SDK\u INT>=Build.VERSION\u CODES.HONEYCOMB\u MR2&((UiModeManager)上下文
.getSystemService(Context.UI\u MODE\u SERVICE))
.getCurrentModeType()==Configuration.UI\u MODE\u TYPE\u电视;
}
公共静态int getScreenOrientation(上下文){
返回context.getResources().getDisplayMetrics().widthPixels<
context.getResources().getDisplayMetrics().heightPixels?
Configuration.ORIENTATION\u纵向:Configuration.ORIENTATION\u横向;
}
公共静态布尔isTablet(上下文){
返回(context.getResources().getConfiguration().screenLayout&Configuration.screenLayout\u SIZE\u MASK)>=Configuration.screenLayout\u SIZE\u LARGE;
}

创建用于获取尺寸的帮助类

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());
    }
}

抱歉,我是android新手,我从教程中得到了这段代码,你说的“第三方”是什么意思?这在普通视图中是2张,在横向视图中是3张,我不希望我想自动设置它,这取决于屏幕的大小好的。如果你找到任何解决办法,请告诉我。这样我就可以把它用在未来
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);