I am trying to show exact (eg. 3 items) in RecyclerView. Our web application shows like this:
If I move items:
and release the mouse, it automatically comes to this:
In Android, I am showing images using RecyclerView:
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);
episodeList. size() it's your total item in recyclerview.
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().
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With