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

时间:2022-10-02 23:31:23

Stuck in this problem about RecyclerView Visible item count Requirement :- RecyclerView has 3+ items but ,need to show 3 items (other items can be reached through scrolling) RecyclerView has <=3 items , need to show the full items

关于RecyclerView Visible项目计数的问题要求: - RecyclerView有3个以上项目,但需要显示3个项目(其他项目可以通过滚动到达)RecyclerView有<= 3项,需要显示完整项目

Recyclerview set adapter code

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 file

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"

        >

Thanks in advance

提前致谢

1 个解决方案

#1


0  

this can be help to you

这可以对你有所帮助

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

#1


0  

this can be help to you

这可以对你有所帮助

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