如何在 RecyclerView 中显示确切的项目数量? [英] How to show exact number of items in RecyclerView?
本文介绍了如何在 RecyclerView 中显示确切的项目数量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图在 RecyclerView 中显示精确的(例如 3 个项目).我们的网络应用程序显示如下:
I am trying to show exact (eg. 3 items) in RecyclerView. Our web application shows like this:
如果我移动项目:
然后松开鼠标,它会自动出现:
and release the mouse, it automatically comes to this:
在 Android 中,我使用 RecyclerView 显示图像:
In Android, I am showing images using RecyclerView:
如何在 RecyclerView 的可见区域恰好显示 3 个项目?如何避免显示一半"项目?这可能吗?
RecyclerView:
RecyclerView:
<android.support.v7.widget.RecyclerView
android:layout_width="wrap_content"
android:layout_height="130dp"
android:id="@+id/rvProducts" />
回收商视图中的一项:
<?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>
代码:
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);
推荐答案
一种解决方案是以编程方式设置每个孩子的width
.
One solution is to set width
of each child programmatically like.
view.getLayoutParams().width = getScreenWidth() / VIEWS_COUNT_TO_DISPLAY;
屏幕尺寸如下:
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics).widthPixels;
这篇关于如何在 RecyclerView 中显示确切的项目数量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文