简体   繁体   中英

How I can show the number of items in recyclerview?

In recyclerview, I want only 3 items to show,

so I set the width and height with recyclerview but,

another test device it show 3.2 items to show.

in main activity,

<android.support.v7.widget.RecyclerView
                        android:id="@+id/main_activity_recyclerview"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent">

                    </android.support.v7.widget.RecyclerView>

and in main_activity_recyclerviews view.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" **android:layout_width="90dp"**
    android:layout_height="match_parent"
    android:layout_gravity="center_horizontal"
    android:gravity="center_horizontal"
    >

<TextView> ...


</LinearLayout>

In main_activity_recyclerviews view, I change the width and test in A device, it show just 3 items, but test in B device, it shows 3.2items.

It occurs looks like device display size.

How I can show the recyclerview items of appoint size?

----------------------update---------------------------

@Override
        public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_main,parent,false);

            view.getLayoutParams().height = changeX;

            return new MyViewHolder(view);
        }

oncreateviewholder

One solution is to set height of each child problematically like

view.getLayoutParams().height = getScreenHeight() / VIEWS_COUNT_TO_DISPLAY;

And the screen size like this:

DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics).heightPixels;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM