我已经从Java迁移到Kotlin并创建了这个recyclerViewAdapter:
class MisPedidosAdapter(private val context: Context, private var pedidos: List<ParseObject>?, private val onMisPedidosClickListener: OnMisPedidosClickListener) : CustomRecyclerViewAdapter() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
when(getItemViewType(viewType)){
STATIC ->return NoMisPedidosHolder(LayoutInflater.from(parent.context).inflate(R.layout.no_pedidos_recibidos_item, parent, false))
else -> return MisPedidosHolder(LayoutInflater.from(parent.context).inflate(R.layout.mis_pedidos_item, parent, false), onMisPedidosClickListener, pedidos)
}
}
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
if (getItemViewType(position) == STATIC)
{
val mHolder = holder as NoMisPedidosHolder
}
else
{
val mHolder = holder as MisPedidosHolder
val pedido = pedidos!![position]
val menuShop = pedido.getParseObject("menu_shop")
mHolder.numPlati.text = pedido.getNumber("quantity")?.toInt().toString()
mHolder.horario.text = pedido.getString("horario")
mHolder.dateOrder.text = StringUtils.getDataFormatted("dd MMM yyyy", pedido.createdAt!!)
}
}
override fun getItemViewType(position: Int): Int = when{
pedidos!!.isEmpty() -> STATIC
else -> DINAMIC
}
override fun getItemCount(): Int = when{
pedidos!!.isEmpty() -> 1
else -> pedidos!!.size
}
fun updatePedidos(pedidos: List<ParseObject>) {
this.pedidos = pedidos
notifyDataSetChanged()
}
companion object {
private val STATIC = 0
private val DINAMIC = 1
}
}
问题在于,当我在Kotlin中list.size
时,它还包括大小索引(例如,如果大小为4,则考虑{0,1,2,3,4}) 。当我用于循环时,我可以使用until
锁定{0,1,2,3},但在这种情况下,我不知道该怎么做。
有人知道解决方法吗?感谢
答案 0 :(得分:0)
使用此
override fun getItemCount(): Int = when{
pedidos!!.isEmpty() -> 0
else -> pedidos!!.size
}
这是
的内容override fun getItemCount(): Int = when{
pedidos!!.isEmpty() -> 1
else -> pedidos!!.size
}
答案 1 :(得分:0)
您必须更换
when(getItemViewType(viewType)){
与
when(viewType){