如何在RecyclerView中显示确切的项目数?

时间:2016-02-02 12:31:14

标签: android android-layout android-recyclerview

我试图在RecyclerView中显示确切的(例如3项)。我们的网络应用程序显示如下:

enter image description here

如果我移动物品:

enter image description here

并释放鼠标,它会自动出现: enter image description here

在Android中,我使用RecyclerView显示图像:

enter image description here

如何准确显示RecyclerView可见区域中的3个项目?如何避免显示"一半"项目?这可能吗?

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);

4 个答案:

答案 0 :(得分:14)

一种解决方案是以编程方式为每个孩子设置width

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

屏幕尺寸如下:

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

答案 1 :(得分:8)

我已按以下方法完成上述任务,请参考以下代码

onCreateViewHolder() RecycleView Adapter 方法中,我找到了屏幕的宽度,而不是分为 3 < / strong> sections.Please检查一次。

@Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.MY_ITEM_TO_INFALTE, parent, false);

        itemView.getLayoutParams().width = (int) (getScreenWidth() / NUMBERS_OF_ITEM_TO_DISPLAY); /// THIS LINE WILL DIVIDE OUR VIEW INTO NUMBERS OF PARTS

        return new MyCreationItemAdapter.MyViewHolder(itemView);
    }

从上面我们使用的代码 getScreenWidth() 方法如下,请检查。

public int getScreenWidth() {

        WindowManager wm = (WindowManager) activity.getSystemService(Context.WINDOW_SERVICE);
        Display display = wm.getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);

        return size.x;
    }

答案 2 :(得分:2)

在实现这一目标的一般方法中,没有回收者视图附带的直接属性。

这可以实现的方法是将父宽度发送给适配器..

以suedo方式......

    Parent width = parent container width;

    InitialiseRecyclerAdapter(parent width) {
         on create view holder {
                 Set the width of ur item layout with a width that divides ur parent width in equal parts;
          }
}

答案 3 :(得分:0)

此解决方案对我有用:

 private lateinit var productImageList: RecyclerView

 override fun onAttachedToRecyclerView(recyclerView: RecyclerView) 
  {
       super.onAttachedToRecyclerView(recyclerView)

        productImageList = recyclerView
  }

 override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ProductViewHolder {
        val viewHolder = LayoutInflater.from(parent.context).inflate(R.layout.ITEM_TO_INFALTE, parent, false)

        viewHolder.layoutParams.width = productImageList.measuredWidth / NUMBERS_OF_ITEM_TO_DISPLAY

        return ProductViewHolder(viewHolder)
    }

代码在kotlin中,希望对您有所帮助。