Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show exact number of items in RecyclerView?

Tags:

I am trying to show exact (eg. 3 items) in RecyclerView. Our web application shows like this:

enter image description here

If I move items:

enter image description here

and release the mouse, it automatically comes to this: enter image description here

In Android, I am showing images using RecyclerView:

enter image description here

How to show exactly 3 items in the visible area of RecyclerView? How to avoid to show "half" items? Is this possible?

RecyclerView:

        <android.support.v7.widget.RecyclerView
            android:layout_width="wrap_content"
            android:layout_height="130dp"
            android:id="@+id/rvProducts" />

One item in recycler view:

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

    <ImageView
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:id="@+id/ivProduct"
        android:src="@drawable/noimage"/>

</LinearLayout>

Code:

    LinearLayoutManager layoutManager
            = new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false);
    RecyclerView productImageList = (RecyclerView) view.findViewById(R.id.rvProducts);
    productImageList.setLayoutManager(layoutManager);
    GalleryRecyclerAdapter adapter = new GalleryRecyclerAdapter(images);
    productImageList.setAdapter(adapter);
like image 670
Joe Richard Avatar asked Feb 02 '16 12:02

Joe Richard


People also ask

How do I find the recycler item count?

episodeList. size() it's your total item in recyclerview.

How does recycler view is different than list view?

The RecyclerView's adaptor forces us to use the ViewHolder pattern. The views are split into onCreateViewholder() and onBindViewholder() methods. The ListView doesn't give that kind of protection by default, so without implementing the ViewHolder pattern inside the getView().


1 Answers

One solution is to set width of each child programmatically like.

view.getLayoutParams().width = getScreenWidth() / VIEWS_COUNT_TO_DISPLAY;

And the screen size like this:

DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics).widthPixels;
like image 65
Prokky Avatar answered Sep 28 '22 06:09

Prokky