2

I am having a hard time to add a different view type to my working recycler view loaded from sqlite database. I want this to be a button in the end of the list since adding a button to the end of the layout is buggy and in the future I pretend to add more view types.

I saw multiple examples with multiple solutions but im a beginner and my java is not very good since im learning android studio with kotlin.

I tried :

Extending recycler view holder

-I could not continue because I didnt understand the java code.

using BaseViewHolder

-I got problems with the return type of the onCreateViewHolder unit and with the declaration of the viewholders, so I could not do it like the examples I found

I think my getItemViewType is working where I add one 1 line with id =-10 after the cursor in the dbhandler to get the different view type in the end of the list.

override fun getItemViewType(position: Int) :Int  {
        return if (list[position].id.toInt() == -10 ){
            VIEW_TYPE_FOLLOWER
        }else{
            VIEW_TYPE_HEADER
        }
    }

I have many doubts about the onCreateViewHolder when I try to return ViewHolder and ViewHolder2 I get errors so this is my working viewholder.

override fun onCreateViewHolder(p0: ViewGroup, p1: Int): ViewHolder {
        val v = LayoutInflater.from(p0.context).inflate(R.layout.list_layout, p0 , false)
        return ViewHolder(v)
    }

view holder

class ViewHolder(itemView: View) :RecyclerView.ViewHolder(itemView){
        val textViewName = itemView.findViewById(R.id.textViewName)as TextView
        val textViewNameEdit = itemView.findViewById(R.id.textViewNameEdit)as EditText
        val textViewAddress = itemView.findViewById(R.id.priorityLevel) as TextView

        val notas = itemView.findViewById(R.id.notas) as TextView
    }

I want a different view type at the end to make it a button.

Andre Andriole
  • 335
  • 6
  • 11
  • Possible duplicated of https://stackoverflow.com/questions/26245139/how-to-create-recyclerview-with-multiple-view-type – Gustavo Pagani Jun 30 '19 at 16:09
  • It is in java to start with, I dont understand how his OnCreateViewHolder doest specify the return type, the other aswer extends recycler view holder and I added that to my question etc etc... – Andre Andriole Jun 30 '19 at 16:31

1 Answers1

8

the first step must be declared a field in the adapter like blew

private val EMPTY_ITEM = 0
private val NORMAL_ITEM = 1

so, the next step you must create two type instance viewHolder

inner class MyViewHolder(itemView: View) : ViewHolder(itemView) {
    var title: TextView = itemView.item_text

}

inner class EmptyMyViewHolder(itemView: View) : ViewHolder(itemView) {
    var titleEmpty: TextView = itemView.item_empty_text
}

and create a new instance suitable viewHolder

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
    val inflater = LayoutInflater.from(parent.context)
    return if (viewType == NORMAL_ITEM) {
        val v = inflater.inflate(R.layout.item, parent, false)
        vh = MyViewHolder(v)
        vh as MyViewHolder
    } else {
        val v = inflater.inflate(R.layout.item_empty, parent, false)
        vh = EmptyMyViewHolder(v)
        vh as EmptyMyViewHolder
    }
}

don't forget override getItemViewType

override fun getItemViewType(position: Int): Int {
    return when (getItems()[position]) {
        is EmptySampleModel -> EMPTY_ITEM
        else -> NORMAL_ITEM
    }
}

last step bind item with suitable data

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
    when (holder) {
        is MyViewHolder -> {
            vh = holder
            val model = getItems()[position] as SampleModel
            (vh as MyViewHolder).title.text = model.getId().toString()
        }
        else -> {
            vh = holder
            val model = getItems()[position] as EmptySampleModel
            (vh as EmptyMyViewHolder).titleEmpty.text = model.getText()
        }
    }
}
  • Thank you ! So I just needed to use an inner class ! But im still having a problem, in the return of OnCreateViewHolder it says that this cast "vh as ViewHolder" can never succed. Where and how should I declare the vh ? – Andre Andriole Jul 01 '19 at 14:44
  • your welcome, why never succeed? your child viewHolder must be instance of ViewHolder class, maybe useful lookup this class https://github.com/alirezat775/carousel-view/blob/development/app/src/main/java/alirezat775/carouselview/SampleAdapter.kt I am implemented multi-view in own library Note: carousel-view is advanced recycler-view, a sampleAdapter class is instance of recyclerViewAdapter – Alireza Tizfahm Fard Jul 01 '19 at 15:22