I'm in trouble! I have this RecyclerView where I use a GridLayoutManager to achieve two columns and several rows. But here goes my issue: I have at most 8 items in this RecyclerView, and I would like to fit them according to screen size
So far I've got this:
using this piece of code:
Rect rectangle = new Rect();
Window window = ((Activity)context).getWindow();
window.getDecorView().getWindowVisibleDisplayFrame(rectangle);
int statusBarHeight = rectangle.top;
int contentViewTop =
window.findViewById(Window.ID_ANDROID_CONTENT).getTop();
int titleBarHeight= contentViewTop - statusBarHeight;
final TypedArray styledAttributes = getContext().getTheme().obtainStyledAttributes(
new int[] { android.R.attr.actionBarSize });
int mActionBarSize = (int) styledAttributes.getDimension(0, 0);
styledAttributes.recycle();
int softButtonsHeight = 0;
DisplayMetrics metrics = new DisplayMetrics();
((Activity)context).getWindowManager().getDefaultDisplay().getMetrics(metrics);
DisplayMetrics realMetrics = new DisplayMetrics();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
((Activity)context).getWindowManager().getDefaultDisplay().getRealMetrics(realMetrics);
if(realMetrics.heightPixels > metrics.heightPixels){
softButtonsHeight = realMetrics.heightPixels - metrics.heightPixels;
}
}
ImageView img_Logo = (ImageView)rootView.findViewById(R.id.img_logo_detalhe);
float logoHeight = 0;
//convertendo na mão tamanho do sponsor
if(img_Logo.getVisibility() != GONE) {
logoHeight = 100 * context.getResources().getDisplayMetrics().density;
}
double sizeInPx = (metrics.heightPixels - titleBarHeight - softButtonsHeight - mActionBarSize - logoHeight) / Math.round(list.size() / 2D);
itensAdapter = new OptionItensAdapter(context, list, (int)sizeInPx);
rvOptions.setAdapter(itensAdapter);
and inside OptionItensAdapter constructor at my onBindViewHolder
:
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, sizeInPx);
holder.imageButton.setLayoutParams(params);
Do you have any idea that would make me achieve this? Thanks in advance.