-2
  <androidx.recyclerview.widget.RecyclerView
                                    android:id="@+id/rv46"
                                    android:layout_width="match_parent"
                                    android:layout_height="wrap_content"
                                    app:spanCount="3"
                                    tools:listitem="@layout/wallet_items"

                                    />

I am implementing recyclerview with N number of items but i want to show only 3 item in list.Also i have set spancount for recyclerview but its not working

Vishali
  • 1,307
  • 4
  • 23
  • 41
  • Does this answer your question? [Simple Android grid example using RecyclerView with GridLayoutManager (like the old GridView)](https://stackoverflow.com/questions/40587168/simple-android-grid-example-using-recyclerview-with-gridlayoutmanager-like-the) – Ricky Mo Oct 21 '21 at 06:59
  • i am not using gridlayout – Vishali Oct 21 '21 at 07:05
  • The question is ambiguous. Span count is used as part of a GridLayout to determine the number of items horizontally per row, hence the comment about GridLayoutManager. – Chris Oct 21 '21 at 07:07
  • It is not `GridLayout`. It is `RecyclerView` using `GridLayoutManager`. `RecyclerView` has a `layoutManager`. There are different types of `layoutManager` to choose from. `GridLayoutManager` with a column count set to 3 suit your use case – Ricky Mo Oct 21 '21 at 07:08
  • I am using linearlayoutmanager – Vishali Oct 21 '21 at 07:11
  • Sorry for misunderstanding. You may want to set the `height` of `itemView` to 1/3 of the parent view. You may refer to [this post](https://stackoverflow.com/questions/35221566/how-to-set-the-height-of-an-item-row-in-gridlayoutmanager) . This also applies to `LinearLayoutManager`. – Ricky Mo Oct 21 '21 at 07:16
  • You have to dynamically set the height/width also after undertaking the margin . thats the only way . – ADM Oct 21 '21 at 07:27

3 Answers3

0

instead of app:spanCount="3" use tools:itemCount="3"

0

In your adapter class, assign a return value 3 of `getItemCount(), it shows only 3 list items

override fun getItemCount(): Int {
   return 3
}
Mohak Shah
  • 518
  • 3
  • 12
0

Add this condition in Your adapter getItemCount() method:

return Math.min(yourlist.size(), 3);

Like this:

@Override
    public int getItemCount() {
        return Math.min(memeberList.size(), 3);
    }
ouflak
  • 2,458
  • 10
  • 44
  • 49