如何根据屏幕高度android设置recyclerview网格?

时间:2018-02-06 11:02:30

标签: android

我有一个应用程序,其中我有一个带有自定义布局的回收视图我想要根据设备高度显示项目。如果设备很大,那么我必须显示1个网格或设备高度为中等的项目,然后想要显示2个网格。

代码: -

DisplayMetrics displayMetrics = mContext.getResources().getDisplayMetrics();
    width = displayMetrics.widthPixels;
    height = displayMetrics.heightPixels;

回收者视图代码

rightPaneAdapter = new SidePanelListAdapter(UserListingMainActivity.this, models, this);
    RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(getApplicationContext(), 1);
    mOptionList.setLayoutManager(mLayoutManager);
    mOptionList.setItemAnimator(new DefaultItemAnimator());
    mOptionList.setAdapter(rightPaneAdapter);

1 个答案:

答案 0 :(得分:-1)

使用

DisplayMetrics displaymetrics = new DisplayMetrics();
    ((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);

    int devicewidth = displaymetrics.widthPixels / 3;

    int deviceheight = displaymetrics.heightPixels / 4;

    holder.image_view.getLayoutParams().width = devicewidth;

    //if you need same height as width you can set devicewidth in holder.image_view.getLayoutParams().height
    holder.image_view.getLayoutParams().height = deviceheight;