【问题标题】:How to add a plus image as a button at the end of my RecyclerView in Android? [duplicate]如何在 Android 中的 RecyclerView 末尾添加一个加号图像作为按钮? [复制]
【发布时间】:2018-05-03 15:38:14
【问题描述】:

我想要一个 add 按钮,我的所有锻炼项目如下:

Image of the RecyclerView I want

我尝试在我的CustomAdapter 中像这样实现它:

class CustomAdapterExercise(val exerciseList: ArrayList<Exercise>) : RecyclerView.Adapter<CustomAdapterExercise.ViewHolder>() {

val typeAdd = 0
val typeExercise = 1

//this method is returning the view for each item in the list
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CustomAdapterExercise.ViewHolder {

    if (viewType == typeAdd) {
        val itemView = LayoutInflater.from(parent.context).inflate(R.layout.exercise_layout, parent, false)
        return ViewHolder(itemView)
    } else if (viewType == typeExercise) {
        val itemView = LayoutInflater.from(parent.context).inflate(R.layout.add_layout, parent, false)
        return ViewHolder(itemView)
    }
    else{
        val itemView = LayoutInflater.from(parent.context).inflate(R.layout.add_layout, parent, false)
        return ViewHolder(itemView)
    }

}

//this method is binding the data on the list
override fun onBindViewHolder(holder: CustomAdapterExercise.ViewHolder, position: Int) {
    holder.bindItems(exerciseList[position])
}

override fun getItemViewType(position: Int): Int {
    if (position <= exerciseList.size) {
        return typeExercise
    } else if (position == exerciseList.size + 1) {
        return typeAdd
    }
    else{
        return typeAdd
    }
}

//this method is giving the size of the list
override fun getItemCount(): Int {
    return exerciseList.size + 1
}

//the class is hodling the list view
class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

    fun bindItems(Exercise: Exercise) {

        val exerciseAmount = itemView.findViewById<TextView>(R.id.exerciseAmount)
        val exerciseName  = itemView.findViewById<TextView>(R.id.exerciseName)
        val exerciseWeight = itemView.findViewById<TextView>(R.id.exerciseWeight)
        val exerciseSets  = itemView.findViewById<TextView>(R.id.exerciseSets)
        exerciseAmount.text = Exercise.exAmount
        exerciseName.text = Exercise.exName
        exerciseWeight.text = Exercise.weight
        exerciseSets.text = Exercise.sets
    }
}
}

但是当我进入该视图时它会崩溃,并给我这个错误:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
                      at abyte.fitness.fitnessbyte.CustomAdapterExercise$ViewHolder.bindItems(CustomAdapterExercise.kt:67)

我知道这是因为我正在为我的添加按钮分配文本,但我不知道如何以不同的方式执行此操作并使其正常工作。

当我整天搜索时,我已经看到了“重复”的问题,但对于像我这样的初学者来说,它并没有得到很好的解释......

【问题讨论】:

  • “它不适合初学者” - SO 是为热心的程序员准备的,而不是为正在寻找有关基本内容的深入教程的初学者准备的。如果您无法理解链接帖子中的答案,您可能需要先花一些时间在 android developer docs 上,以对 android 有一个基本的了解
  • @TimCastelijns 好吧,我是一个狂热的程序员,我只是还没有 android 编程的技能。另外,我将查看文档,谢谢。我不知道 StackOverflow 只适用于各个领域的专业人士。
  • 我不知道 StackOverflow 只适用于各个领域的专业人士。 - 不是。我没这么说。它只是不适合想要被灌输信息的初学者
  • @TimCastelijns 你是完全正确的,但是在尝试了大约 4 个小时并且自己做了一半的事情并且不理解后半部分之后,我认为完全可以问......不要你这么认为?

标签: android android-recyclerview kotlin


【解决方案1】:

你需要在onBindViewHolder中开启viewType。

override fun onBindViewHolder(holder: CustomAdapterExercise.ViewHolder, position: Int) {
    if (holder.itemViewType == typeAdd) {
        // Bind the add button
    else {
        holder.bindItems(exerciseList[position])
    }
}

旁注:

您以错误的方式使用 ViewHolder,ViewHolders 的好处之一是不会在每次更新 View 时都强制执行findByView

【讨论】:

  • 你的意思是我必须把持有人改成正确的viewType?
  • 由于您对视图使用不同的布局,我怀疑您不能使用相同的 ViewHolder(除非它们实际上包含相同的视图 ID 和类型)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-15
相关资源
最近更新 更多