首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >StaggeredGridLayoutManager制作auto_fit列

StaggeredGridLayoutManager制作auto_fit列
EN

Stack Overflow用户
提问于 2016-11-26 07:17:51
回答 3查看 1.2K关注 0票数 1

如何使StaggeredGridLayoutManager auto_fit具有所有屏幕大小,自动设置列数

代码语言:javascript
复制
recyclerViewadapter = new RecyclerViewAdapter(GetDataAdapter1, this);
StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(recyclerViewadapter);
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2016-11-26 07:35:20

尝尝这个

代码语言:javascript
复制
 StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(getSpan(), StaggeredGridLayoutManager.VERTICAL);
代码语言:javascript
复制
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;
}
票数 -1
EN

Stack Overflow用户

发布于 2016-11-26 07:35:19

如果项目宽度相同,则可以这样做:

代码语言:javascript
复制
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();
            }
        });
票数 0
EN

Stack Overflow用户

发布于 2017-10-05 19:14:03

创建用于获取大小的帮助类。

代码语言:javascript
复制
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());
    }
}

要获得列计数,需要设置项目的最小宽度。

代码语言:javascript
复制
int columnCount = GetSize.getColumnCount(context, 288);

layoutManager = new StaggeredGridLayoutManager(columnCount, StaggeredGridLayoutManager.VERTICAL);
recyclerView.setLayoutManager(layoutManager);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40815956

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档