android在使用DiffUtil时更改RecyclerView中奇数行的背景颜色

时间:2019-10-15 12:20:05

标签: android android-recyclerview android-diffutils

我有一个RecyclerView,每个奇数行都有不同的背景色。 我尝试添加DiffUtil来加快更新速度,例如:


import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import com.asdf.android.R
import com.asdf.android.network.model.trade.MarketTradeItem
import kotlinx.android.synthetic.main.row_trade_history.view.*

class MarketTradesListAdapter(
    private val context: Context,
    private var list: MutableList<MarketTradeItem>,
    val theme: Int
) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        val inflate =
            LayoutInflater.from(parent.context).inflate(R.layout.row_trade_history, parent, false)
        return ItemHolder(inflate)
    }

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

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        val itemHolder = holder as ItemHolder
        itemHolder.bind(list[position], position)
    }

    override fun onBindViewHolder(
        holder: RecyclerView.ViewHolder,
        position: Int,
        payloads: MutableList<Any>
    ) {
        if (payloads.isEmpty()) {
            super.onBindViewHolder(holder, position, payloads)
        } else {
            val payload = payloads[0] as DiffUtilPayload
            val itemHolder = holder as ItemHolder
            itemHolder.bind(list[position], position, payload)
        }
    }

    fun setList(it: List<MarketTradeItem>) {
        it.forEachIndexed { index, item -> item.position = index }

        DiffUtil.calculateDiff(DiffCallback(list, it),true).dispatchUpdatesTo(this)

        list.clear()
        list.addAll(it)
    }

    inner class ItemHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        fun bind(
            order: MarketTradeItem,
            position: Int,
            payload: DiffUtilPayload = DiffUtilPayload()
        ) {


            var color: Int
            if (theme == R.style.Apptheme) {
                color = R.color.listBackgroundColorLight
            } else {
                color = R.color.listBackgroundColorDark
            }
//            if (payload.isPositionChanged)
            itemView.setBackgroundColor(
                if (position % 2 == 0) ContextCompat.getColor(
                    itemView.context,
                    android.R.color.transparent
                ) else
                    ContextCompat.getColor(itemView.context, color)
            )


            itemView.textViewPrice.setTextColor(
                ContextCompat.getColor(
                    itemView.context,
                    if (order.isRaise) R.color.buy_green else R.color.sell_red
                )
            )
            if (payload.isPriceChanged) {
                itemView.textViewPrice.text = order.price
            }
            if (payload.isAmountChanged) {
                itemView.textViewAmount.text = order.amount
            }
            if (payload.isTimeChanged) {
                itemView.textViewTime.text = order.time
            }


        }
    }

    class DiffCallback(
        private val oldList: List<MarketTradeItem>,
        private val newList: List<MarketTradeItem>
    ) : DiffUtil.Callback() {
        override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
            return oldList[oldItemPosition] == newList[newItemPosition]
        }

        override fun getOldListSize(): Int {
            return oldList.size
        }

        override fun getNewListSize(): Int {
            return newList.size
        }

        override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
            return oldList[oldItemPosition] == newList[newItemPosition]
        }

        override fun getChangePayload(oldItemPosition: Int, newItemPosition: Int): Any? {
            val oldItem = oldList[oldItemPosition]
            val newItem = newList[oldItemPosition]
            return DiffUtilPayload(
                oldItem.price != newItem.price,
                oldItem.amount != newItem.amount,
                oldItem.createdAt != newItem.createdAt,
                oldItem.time != newItem.time,
                oldItem.position != newItem.position
            )
        }

    }

    data class DiffUtilPayload(
        val isPriceChanged: Boolean = true,
        val isAmountChanged: Boolean = true,
        val isCreatedAtChanged: Boolean = true,
        val isTimeChanged: Boolean = true,
        val isPositionChanged: Boolean = true
    )

}

问题是,将新项目插入列表时,偶数行和奇数行的背景色显示不正确,并且显示如下:

enter image description here 背景颜色是随机设置的,不会显示为交替的偶数/奇数。 我该怎么做才能解决此问题?

1 个答案:

答案 0 :(得分:2)

我认为您需要在DiffUtil中更改letters="abcdefghi" numbers="123456789" for l in letters: for n in numbers: globals()[l+n]=0 方法。如果内容相同,则不会计算不会向适配器分派areContentsTheSame的更改。而且,该位置/项目不会调用notifyItemChanged(position)

您应该尝试将方法更改为

onBindViewHolder