And*_*ole 2 sqlite android kotlin android-recyclerview
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
}
}
Run Code Online (Sandbox Code Playgroud)
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)
}
Run Code Online (Sandbox Code Playgroud)
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
}
Run Code Online (Sandbox Code Playgroud)
I want a different view type at the end to make it a button.
第一步必须在适配器中声明一个字段,如 blew
private val EMPTY_ITEM = 0
private val NORMAL_ITEM = 1
Run Code Online (Sandbox Code Playgroud)
所以,下一步你必须创建两个类型的实例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
}
Run Code Online (Sandbox Code Playgroud)
并创建一个适合 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
}
}
Run Code Online (Sandbox Code Playgroud)
不要忘记覆盖 getItemViewType
override fun getItemViewType(position: Int): Int {
return when (getItems()[position]) {
is EmptySampleModel -> EMPTY_ITEM
else -> NORMAL_ITEM
}
}
Run Code Online (Sandbox Code Playgroud)
最后一步用合适的数据绑定项目
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()
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2756 次 |
| 最近记录: |