RecyclerView-我只想更新1个视图时更新2个视图

时间:2018-08-20 17:59:43

标签: android android-recyclerview kotlin

我创建了一个RecyclerView,用户可以在其中按下“ Now”按钮,它将使用当前日期和时间更新Recycler视图的TextView。

奇怪的是,如果我在位置0的回收站视图上按“现在”按钮,它也会在位置9处更新RecyclerView。类似地,在位置1处按它也会在位置10处对其进行更新。

当前,日期和时间未保存在任何地方;该部分尚未设置。我只是想弄清楚为什么单击按钮会更新相距10个位置的两个视图。

这是我的适配器的代码;我正在使用Kotlin:

class TimeStampListAdapter(private val labelList: ArrayList<TimeStampLabels>,
                       private val context: Context) : RecyclerView.Adapter<TimeStampListAdapter.ViewHolder>() {

override fun onCreateViewHolder(parent: ViewGroup, position: Int): ViewHolder {
    val view: View = LayoutInflater.from(context).inflate(R.layout.list_time_category, parent, false)
    return ViewHolder(view, context, labelList)
}

override fun getItemCount(): Int {
    return labelList.size
}

override fun onBindViewHolder(viewHolder: ViewHolder, position: Int) {
    viewHolder?.bindItem(labelList[position])

}

 inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView), View.OnClickListener {

    var tsDate = itemView.findViewById(R.id.txtEDate) as EditText
    var tsTime = itemView.findViewById(R.id.txtETime) as EditText

    var categoryLabel = itemView.findViewById(R.id.txtVCategory) as TextView

    var nowButton = itemView.findViewById(R.id.btnNow) as Button

    val calendar = Calendar.getInstance()

    // Inserts the time stamp labels into each RecyclerView instance
    fun bindItem(label: TimeStampLabels) {

        categoryLabel.text = label.tsLabel
        nowButton.setOnClickListener(this)

    }



     override fun onClick(v: View?) {
         var dateFormat = SimpleDateFormat("MM / dd / yyyy")
         var timeFormat = SimpleDateFormat("HH:mm")
         var date = dateFormat.format(calendar.timeInMillis)
         var time = timeFormat.format(calendar.getTime())

         // Get the position of relevant row
         var position: Int = adapterPosition
         Log.d("POSITION:  ", position.toString())
         var mList = labelList[position]
         if (v!!.id == nowButton.id) {
             Log.d("POSITION in conditional", position.toString())
             tsDate.setText(date)
             tsTime.setText(time)


             //notifyItemChanged(adapterPosition) - This isn't needed, 
             // but if uncommented doesn't help the main issue I'm    
             // having.
         }

     }

问题是在触发覆盖的onClick函数时发生的。

活动:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_time_stamp)



    labelListItems = ArrayList()
    adapter = TimeStampListAdapter(labelListItems!!, this )
    layoutManager = LinearLayoutManager(this)


    timeStampRecyclerView.layoutManager = layoutManager
    timeStampRecyclerView.adapter = adapter

    /**
     * Create the recycler views out of the TimeStampLabels table
     */
    for (i in 0..labels.timeStampLabels!!.size - 1) {
        val label = TimeStampLabels()
        label.tsLabel = label.timeStampLabels[i]
        labelListItems!!.add(label)
    }
    adapter!!.notifyDataSetChanged()
}

1 个答案:

答案 0 :(得分:0)

之所以发生这种情况,是因为回收站视图的名称表明回收(重用)该视图以便节省内存并为用户提供流畅的滚动体验,请参阅此链接以获取更多详细信息https://developer.android.com/guide/topics/ui/layout/recyclerview

因此,您在模型类TimeStampLabels中创建了另外两个字段,这些字段将存储日期和时间,然后您必须按如下所示更改onClick实施方式

override fun onClick(v: View?) {
    var dateFormat = SimpleDateFormat("MM / dd / yyyy")
    var timeFormat = SimpleDateFormat("HH:mm")
    var date = dateFormat.format(calendar.timeInMillis)
    var time = timeFormat.format(calendar.getTime())

    // Get the position of relevant row
    var position: Int = adapterPosition
    Log.d("POSITION:  ", position.toString())
    var mList = labelList[position]
    // What ever name you give for date and time field in your model class
    mList.date = date
    mList.time = time
    labelList[position] = mList
    notifyDataSetChanged()
}

并按如下所示更改您的bindItem方法

fun bindItem(label: TimeStampLabels) {
    // What ever name you give for date and time field in your model class
    tsDate.setText(label.date)
    tsTime.setText(label.time)
    categoryLabel.text = label.tsLabel
    nowButton.setOnClickListener(this)

}