你好,我是一个新的编程我刚刚创建了一个应用程序,从服务器上获得的数据,每秒钟我只是想改变我的回收站持有人的出价背景,并询问的立场,如果数据更高或更低,如果以前。例如,我的初始数据在出价是4000后一秒钟,如果我的出价的项目是增加,然后在recyclerview的位置改变它的背景色。
我只是实现了这段代码,但它随机改变背景也当价格不变。
class Adapter(private val product: ArrayList<Products>) :
RecyclerView.Adapter<McxAdapter.CustomViewHolder>() {
var olddatabid: ArrayList<String> = ArrayList()
var newdatabid: ArrayList<String> = ArrayList()
var olddataask: ArrayList<String> = ArrayList()
var newdataask: ArrayList<String> = ArrayList()
fun addNewStatutes(items: ArrayList<Products>) {
product.clear()
this.product.addAll(items)
if (product.isNotEmpty()) {
notifyDataSetChanged()
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CustomViewHolder {
val itemView = LayoutInflater.from(parent.context)
.inflate(R.layout.item_mcx, parent, false)
return CustomViewHolder(itemView)
}
override fun onBindViewHolder(holder: CustomViewHolder, position: Int) {
try {
val datum = product[position]
holder.txtSymbol.text = datum.symbol
holder.txtdate.text = datum.serExp
holder.txtBuy.text = datum.buyPrice
holder.txtSell.text = datum.sellPrice
holder.txtLtp.text = datum.lastTradedPrice
holder.txtHigh.text = datum.high
holder.txtLow.text = datum.low
holder.txtOpen.text = datum.open
holder.txtClose.text = datum.close
holder.txtChange.text = datum.netChangeInRs
if (newdatabid.size < product.size) {
newdatabid.add(datum.buyPrice.toString())
}
if (olddatabid.size < product.size) {
olddatabid.add(datum.buyPrice.toString())
}
if (newdataask.size < product.size) {
newdataask.add(datum.sellPrice.toString())
}
if (olddataask.size < product.size) {
olddataask.add(datum.sellPrice.toString())
}
newdatabid[position] = datum.buyPrice.toString()
newdataask[position] = datum.sellPrice.toString()
if (newdatabid[position].toFloat() > olddatabid[position].toFloat()) {
holder.txtBuy.setBackgroundColor(Color.BLUE)
}
if (newdatabid[position].toFloat() < olddatabid[position].toFloat()) {
holder.txtBuy.setBackgroundColor(Color.RED)
}
if (newdataask[position].toFloat() > olddataask[position].toFloat()) {
holder.txtSell.setBackgroundColor(Color.BLUE)
}
if (newdataask[position].toFloat() < olddataask[position].toFloat()) {
holder.txtSell.setBackgroundColor(Color.RED)
}
olddatabid[position] = newdatabid[position]
olddataask[position] = newdataask[position]
} catch (e: Exception) {
}
}
override fun getItemCount(): Int {
return if (product.size > 0 && product.isNotEmpty()) {
product.size
} else {
0
}
}
inner class CustomViewHolder(view: View) : RecyclerView.ViewHolder(view) {
internal var txtSymbol: TextView = itemView.findViewById(R.id.scriptname)
internal var txtdate: TextView = itemView.findViewById(R.id.date)
internal var txtBuy: TextView = itemView.findViewById(R.id.buy)
internal var txtSell: TextView = itemView.findViewById(R.id.sell)
internal var txtLtp: TextView = itemView.findViewById(R.id.currentvalue)
internal var txtHigh: TextView = itemView.findViewById(R.id.high)
internal var txtLow: TextView = itemView.findViewById(R.id.low)
internal var txtOpen: TextView = itemView.findViewById(R.id.open)
internal var txtClose: TextView = itemView.findViewById(R.id.close)
internal var txtChange: TextView = itemView.findViewById(R.id.change)
internal var txtRupp: TextView = itemView.findViewById(R.id.rupp)
}
}
2条答案
按热度按时间e5njpo681#
使类成为数据类,并使用AsyncListDifference.submitlist更新适配器
下面是一篇关于实现diffutil的好文章https://medium.com/@iammert/using-diffutil-in-android-recyclerview-bdca8e4fbb00
inn6fuwd2#
尝试删除if-else或when语句中的if语句,然后检查结果