Home > Back-end >  How to create a ReyclerView which has extra CardView for adding new items
How to create a ReyclerView which has extra CardView for adding new items

Time:04-06

Consider an ArrayList which contains 4 element. The RecylcerView shows 4 CardViews. I know how to do that. For this particular RecyclerView, I want a different behaviour: there's a 5th cardview for adding new items. After new item is succesfully added to the ArrayList, then the "add item" CardView will be the 6th, etc.

enter image description here

Here's what I already have:

override fun getItemViewType(position: Int): Int {
    if (position < itemCount) return VIEW_DISPLAY_ITEM
    else return VIEW_ADD_ITEM
}

 override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) : RecyclerView.ViewHolder{
        if (viewType == VIEW_DISPLAY_ITEM){
            return ViewHolderDisplayItem(
                LayoutInflater.from(context).inflate(R.layout.display_item, parent, false)
            )
        }
        else {
            return ViewHolderAddItem(
                LayoutInflater.from(context).inflate(R.layout.add_new_item, parent, false)
            )
        }
    }


override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        when (holder.itemViewType){
            VIEW_DISPLAY_ITEM -> {
                (holder as ViewHolderDisplayItem).bind(position)
                holder.itemView.setOnClickListener {
                    val theData = theList.get(position)
                    txtItemName.text = theData.name
                    txtItemID.text = theData.id
                    txtItemLocation.text = theData.location
                }
            }

            VIEW_ADD_ITEM -> {
                (holder as ViewHolderAddItem).bind(position)
                holder.itemView.setOnClickListener {
                    
                }
            }
        }
    }

The result is not what I expected. The "add new item" cardview is not shown. I guess that's because when getItemType() is invoked, position is always lesser than itemCount.

No idea what's the simple solution for this. Hint/sample code is appreciated.

CodePudding user response:

Not included in your question is the code for getItemCount(). Make sure you are returning list.size 1 in order to include that final "add new item" element.

CodePudding user response:

You try

override fun getItemViewType(position: Int): Int {
    if (position <= itemCount) return VIEW_DISPLAY_ITEM
     else return VIEW_ADD_ITEM       }

I think you have a little misunderstanding in overriding the getItemViewType() function, position start from 0.

Hope it

CodePudding user response:

If u need simple solution
you can just use this CardView outside the recycler view but make sure that recycler view is wrap content so that it can be expandable

ScrollView >> LinearLayout >> RecyclerView >> CardView

Just like that

<androidx.recyclerview.widget.RecyclerView
        android:id="@ id/course_names_rv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:nestedScrollingEnabled="false"/>

So u can easily add new item to your adapter without using view types or something else

I’ve already made it and it works fine with me

  • Related