如何在Android的RecyclerView末尾添加一个加号图像作为按钮?

时间:2017-11-20 12:06:42

标签: android android-recyclerview kotlin

我希望在所有运动项目之后都有一个 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)

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

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

1 个答案:

答案 0 :(得分:0)

您需要在 <?php $array = array( array( array( 'blog_title' => 'sooraj bloging', 'blog_id' => 2 ), array( 'blog_title' => 'What are Mobile App Testing Challenges?', 'blog_id' => 4 ), array( 'blog_title' => 'sooraj blog', 'blog_id' => 8 ) ), array( array( 'title' => 'sooraj casestudy', ) ), array( array( 'career_id' => 14, 'title' => 'Software Engineer .NET sooraj', 'location' => 'Kochi, India.', 'description' => 'Developing .NET applications.', 'qualification' => 'B.Tech. in CSE, MCA', 'status' => 0, 'created_at' => '2017-11-20 13:14:29', 'updated_at' => '0000-00-00 00:00:00' ) ), array(), array( array( 'tst_id' => 146, 'tst_name' => 'John Kasha', 'tst_quote' => 'Gadgeon was extremely professional and was easy to commun sooraj icate and work with on a day-to-day basis. I also liked the fact that they were willing to do the research for specific tasks and present a viable solution or workaround to keep the project on schedule. I would recommend them for any task for any industry software or hardware. Bottom line, they get it done and you get results, not excuses. VP of Engineering.', 'tst_desig' => 'Vice President,Product Development and Engineering', 'tst_image' => '91c09ac9ee6234fdfcc523a393800bd5.jpg', 'url' => '', 'crop_name' => '668959f965ab28815dc97bbc1f8718d8.jpg', 'sysDate' => '2017-11-20 15:42:34' ) ) ); foreach ($array as $value){ foreach ($value as $row){ foreach ($row as $key=> $row1){ echo $key.' - '. $row1; } echo '<br>'; } } ?> 中启用viewType。

onBindViewHolder

旁注:

您使用ViewHolder是错误的方式,每次更新视图时,ViewHolders的好处之一都不会被强制执行override fun onBindViewHolder(holder: CustomAdapterExercise.ViewHolder, position: Int) { if (holder.itemViewType == typeAdd) { // Bind the add button else { holder.bindItems(exerciseList[position]) } }