RecyclerView item height and width change dynamically Android

后端 未结 2 403
渐次进展
渐次进展 2021-02-01 23:28

I am new to android development and I am developing an app where the app downloads pictures from server and show them in a gridView. But some extra spaces are coming up when the

2条回答
  •  逝去的感伤
    2021-02-02 00:00

    Set height and width dynamically according to the screen size. this is the best way to get perfect result.

    DisplayMetrics displaymetrics = new DisplayMetrics();
            ((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
            //if you need three fix imageview in width
            int devicewidth = displaymetrics.widthPixels / 3;
    
            //if you need 4-5-6 anything fix imageview in height
            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;
    

    try this in adapter in getview hope you get better result in all device

    EDIT: Or to set same height same as width you can set devicewidth in height also like below:

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

提交回复
热议问题