Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fit recyclerview items on android screen

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:

enter image description here

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.

like image 857
guisantogui Avatar asked Mar 09 '17 04:03

guisantogui


People also ask

What is recyclerview in Android?

To help you build apps with lists, Android provides the RecyclerView. RecyclerView is designed to be very efficient, even with large lists, by reusing, or recycling, the views that have scrolled off the screen. When a list item is scrolled off the screen, RecyclerView reuses that view for the next list item about to be displayed.

How to animate recyclerview items when they appear on the screen?

This example demonstrates how to animate RecyclerView items when they appear on the screen . Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.xml. Step 3 − Add the following code to res/anim/down_to_up.xml.

What is a recyclerview widget?

The Recyclerview widget is usually used when one has data collections whose elements change at runtime based on user action or network events. A recyclerview accesses its data using an adapter and positions its items using a layout manager 1. Add Dependencies

Should I use recyclerview or not?

If you need to fix views to Screen than you don't need to take recyclerView. You can play with Weight and make items fit to screen.


1 Answers

Have a look at this OnBindViewHolder code and change it as per your requirement :D

 @Override
    public void onBindViewHolder(ViewHolder viewHolder, final int position) {

        final int pos = position;
        try {
//
            Resources r = activity.getResources();
            int px = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 150, r.getDisplayMetrics()); // i have bottom tabbar so yf you dont have any thing like this just leave 150 to 0.I think in your case height of image view an your top(Pifer)
            //this change height of rcv
            DisplayMetrics displaymetrics = new DisplayMetrics();
            activity.getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
            int height = displaymetrics.heightPixels;
            int width = displaymetrics.widthPixels;
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            params.height = (height - px) / 5; //height recycleviewer (there are 5 rows so divide by 5 but i think in your case there are 4 rows so divide by 4)
            viewHolder.itemView.setLayoutParams(params);
            viewHolder.nameTxt.setText(totalList.get(position).getName());
            viewHolder.icon.setImageResource(totalList.get(position).getIcon());
//           viewHolder.background.setBackground(ContextCompat.getDrawable(context, totalList.get(position).getBackground()));
        } catch (Exception e) {
            e.printStackTrace();
        }


    }

Just posting this viewHolder to see what all items.

public static class ViewHolder extends RecyclerView.ViewHolder {

        public TextView nameTxt;
        public RelativeLayout background;
        public ImageView icon;

        public ViewHolder(View itemLayoutView) {
            super(itemLayoutView);

            nameTxt = (TextView) itemLayoutView.findViewById(R.id.menu_label);
            background = (RelativeLayout) itemLayoutView.findViewById(R.id.menu_background);
            icon = (ImageView) itemLayoutView.findViewById(R.id.menu_icon);
        }
like image 58
taman neupane Avatar answered Oct 30 '22 10:10

taman neupane