添加setTextColor后,OnItemClickListener在recyclerView中无法正常工作吗?

时间:2019-01-14 15:08:06

标签: android kotlin android-recyclerview

我有一个片段,其中包含一个recyclerview,该视图在cardviews中显示了时隙,如下所示

enter image description here

我想要的内容:如果用户单击卡片,则“可用”一词将变为“已满”,并且文本颜色将变为红色,如下图所示,当您单击7:30插槽时

enter image description here

问题:在将setTextColor()添加到onbindViewHolder中的textview之后, 更改(将文本更改为“ Full”,将颜色更改为红色)应用于单击的卡,但是当我滚动recyclerview时,我发现另一张卡将文本颜色更改为红色,而没有将文本更改为“ Full”!

enter image description here

我的适配器代码:     class TimeSlotAdapter(priv val timeSlotsList:ArrayList):RecyclerView.Adapter(){

private var mListener: OnItemClickListener? = null

interface OnItemClickListener {
          fun onItemClick(position: Int)
}

fun setOnItemClickListener (listener: OnItemClickListener) {
    mListener = listener
}

class TimeSlotViewHolder(card: View, listener: OnItemClickListener?): RecyclerView.ViewHolder(card){

    var mCardTime: TextView = card.findViewById(R.id.timeTV)
    var mAvailability: TextView = card.findViewById(R.id.availabilityTV)

    init {

        card.setOnClickListener {
            if (listener != null) {
                val position = adapterPosition
                if (position != RecyclerView.NO_POSITION) {
                    listener.onItemClick(position)
                }
            }
        }
    }
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TimeSlotViewHolder {
    val v = LayoutInflater.from(parent.context).inflate(R.layout.time_slot, parent, false)
    return TimeSlotViewHolder(v, mListener)
}

override fun getItemCount(): Int = timeSlotsList.size

override fun onBindViewHolder(holder: TimeSlotViewHolder, position: Int) {
    val currentItem = timeSlotsList[position]

    holder.mCardTime.text = currentItem.cardTime
    holder.mAvailability.text = currentItem.availability

    if (currentItem.isSelected) {

    // the problem when I add the below
        holder.mAvailability.setTextColor(Color.RED)
    }
}

活动片段代码:     class ReserveTimeFragment:Fragment(){

private var timeSlotsList: ArrayList<TimeSlot>? = null

private var mRecyclerView: RecyclerView? = null
private var mAdapter: TimeSlotAdapter? = null
private var mLayoutManager: RecyclerView.LayoutManager? = null

override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {

    val view = inflater.inflate(R.layout.fragment_reserve_time, container, false)

    mRecyclerView = view.findViewById(R.id.recyclerViewLO)

    createTimeSlotsList()
    buildRecyclerView()

    return view
}

fun buildRecyclerView() {
    mRecyclerView!!.setHasFixedSize(true)
    mLayoutManager = LinearLayoutManager(activity)
    mAdapter = TimeSlotAdapter(timeSlotsList!!)

    mRecyclerView!!.layoutManager = mLayoutManager
    mRecyclerView!!.adapter = mAdapter

    mAdapter!!.setOnItemClickListener(object : TimeSlotAdapter.OnItemClickListener {
        override fun onItemClick(position: Int) {
            selectedItem(position, "Full", false)
        }
    })
}

fun selectedItem(position: Int, text: String, selected: Boolean) {
    timeSlotsList!![position].changeAvailability(text)
    timeSlotsList!![position].selecteditem(selected)
    mAdapter!!.notifyDataSetChanged()
}

private fun createTimeSlotsList() {
    timeSlotsList = ArrayList()
    timeSlotsList!!.add(TimeSlot("07:30", "Available", false))
    timeSlotsList!!.add(TimeSlot("08:00", "Available", false))
    timeSlotsList!!.add(TimeSlot("08:30", "Available", false))
    timeSlotsList!!.add(TimeSlot("09:00", "Available", false))
    timeSlotsList!!.add(TimeSlot("09:30", "Available", false))
    timeSlotsList!!.add(TimeSlot("10:00", "Available", false))
    timeSlotsList!!.add(TimeSlot("10:30", "Available", false))
    timeSlotsList!!.add(TimeSlot("11:00", "Available", false))
    timeSlotsList!!.add(TimeSlot("11:30", "Available", false))
    timeSlotsList!!.add(TimeSlot("12:00", "Available", false))
    timeSlotsList!!.add(TimeSlot("12:30", "Available", false))
    timeSlotsList!!.add(TimeSlot("13:00", "Available", false))
    timeSlotsList!!.add(TimeSlot("13:30", "Available", false))
    timeSlotsList!!.add(TimeSlot("14:00", "Available", false))
    timeSlotsList!!.add(TimeSlot("14:30", "Available", false))
    timeSlotsList!!.add(TimeSlot("15:00", "Available", false))
    timeSlotsList!!.add(TimeSlot("15:30", "Available", false))
    timeSlotsList!!.add(TimeSlot("16:00", "Available", false))
    timeSlotsList!!.add(TimeSlot("16:30", "Available", false))
    timeSlotsList!!.add(TimeSlot("17:00", "Available", false))
    timeSlotsList!!.add(TimeSlot("17:30", "Available", false))
    timeSlotsList!!.add(TimeSlot("18:00", "Available", false))
    timeSlotsList!!.add(TimeSlot("18:30", "Available", false))
    timeSlotsList!!.add(TimeSlot("19:00", "Available", false))
    timeSlotsList!!.add(TimeSlot("19:30", "Available", false))
    timeSlotsList!!.add(TimeSlot("20:00", "Available", false))
    timeSlotsList!!.add(TimeSlot("20:30", "Available", false))
    timeSlotsList!!.add(TimeSlot("21:00", "Available", false))
    timeSlotsList!!.add(TimeSlot("21:30", "Available", false))
    timeSlotsList!!.add(TimeSlot("22:00", "Available", false))

}

}

请如何解决此问题?

2 个答案:

答案 0 :(得分:1)

我相信您只是遇到了与回收相关的问题。

override fun onBindViewHolder(holder: TimeSlotViewHolder, position: Int) {
    val currentItem = timeSlotsList[position]

    holder.mCardTime.text = currentItem.cardTime
    holder.mAvailability.text = currentItem.availability

    if (currentItem.isSelected) {

    // the problem when I add the below
        holder.mAvailability.setTextColor(Color.RED)
    }
}

简单地说,在这里考虑一个回收站视图:检查它是否被选中,然后将其文本颜色设置为红色。如果没有呢?

您应该考虑onBindViewHolder中的所有情况,这意味着要实现一个else语句来处理回收者视图:

holder.mAvailability.setTextColor(if (currentItem.isSelected) Color.RED else Color.GREEN)

答案 1 :(得分:1)

    if (currentItem.isSelected) {

    // the problem when I add the below
        holder.mAvailability.setTextColor(Color.RED)
    }

我尚未测试此代码,但根据我的经验,应该是:

    if (currentItem.isSelected) {
        holder.mAvailability.setTextColor(Color.RED)
    } else {
        holder.mAvailability.setTextColor(Color.GREEN)
    }