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.
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()
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With