设置Recycler View可见行数最多3个项目,即使它有更多项目

时间:2017-10-24 09:35:09

标签: android android-recyclerview recycler-adapter

关于RecyclerView可见项目计数的问题 要求: - RecyclerView3+项,但需要显示3个项目(其他项目可以通过滚动来覆盖) RecyclerView<=3件商品,需要显示完整商品

Recyclerview设置适配器代码

     cardoptionlist.clear();
    cardoptionlist.addAll(savedcards);
    cardoptionlist.add(new CardModel(true));

    if (cardOptionAdapter == null) {
        cardOptionAdapter = new CardOptionAdapter(getActivity(), cardoptionlist, this);
    }


    if (rcv_card_options.getLayoutManager() == null) {

        rcv_card_options.setLayoutManager(new LinearLayoutManager(getActivity()));

    }

    rcv_card_options.addItemDecoration(new DividerItemDecoration(getActivity(), LinearLayoutManager.VERTICAL, 0, false));
    rcv_card_options.setItemAnimator(new DefaultItemAnimator());

    rcv_card_options.setHasFixedSize(true);
    rcv_card_options.setItemViewCacheSize(20);
    rcv_card_options.setDrawingCacheEnabled(true);
    rcv_card_options.setAdapter(cardOptionAdapter);

Recyclerview Xml文件

      <android.support.v7.widget.RecyclerView
        android:id="@+id/rcv_card_options"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/rounded_white_app_color_border_8dp"
        android:paddingBottom="2dp"

        android:paddingLeft="8dp"
        android:paddingRight="8dp"
        android:paddingTop="2dp"

        >

提前致谢

1 个答案:

答案 0 :(得分:0)

这可以对你有所帮助

 public void setHeight(final View convertView) {
    final int height = mRecyclerView.getMeasuredHeight() / 3;
    convertView.setLayoutParams(newAbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, height));
}