Android-如何在firestore recyclerview适配器中为型号布局实现另一个视图持有人



我正在尝试使2视图支架在位置0始终处于位置0,这将是选择图像的标题视图,另一个用于显示模型的图像,我可以为普通的回收库适配器做到这一点,但是在Firestore回收器适配器中我做不 我尝试的是将项目数量增加1,但我得到了

java.lang.IndexOutOfBoundsException: Index: 0, Size: 0

这是我的适配器类代码:

val TYPE_SELECT_IMAGE = 0
val TYPE_ITEMS = 1
class UsersImageAdapter(options: FirestoreRecyclerOptions<UserImage>)
    : FirestoreRecyclerAdapter<UserImage, RecyclerView.ViewHolder>(options) , AnkoLogger{
    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int, model: UserImage) {
        when(holder){
            is SelectImageViewHolder -> {
            holder.bindModel()
        }
            is UserImageViewHolder -> {
                holder.bindImages(model)
            }
        }
    }
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return when (viewType) {
            TYPE_SELECT_IMAGE -> {
                val view = LayoutInflater.from(parent.context).inflate(R.layout.item_add_image, parent, false)
                SelectImageViewHolder(view)

            }
            TYPE_ITEMS -> {
                val view = LayoutInflater.from(parent.context).inflate(R.layout.item_image,parent,false)
                UserImageViewHolder(view)
            }
            else -> throw RuntimeException()
        }
    }
    override fun getItemViewType(position: Int): Int {
        return if (isPositionHeader(position)) {
            TYPE_SELECT_IMAGE
        } else {
            TYPE_ITEMS
        }
    }
    fun isPositionHeader(position: Int): Boolean {
        return position == 0
    }
    override fun getItemCount(): Int {
        return super.getItemCount() + 1 
    }
}

这是两个视图支架一个用于标头视图另一个用于显示模型的视图

class SelectImageViewHolder(val view: View) : RecyclerView.ViewHolder(view) , AnkoLogger{
        fun bindModel() { 
            view.setOnClickListener {
                info {
                    "clicked !"
                }
            }
        }
    }
class UserImageViewHolder(val view: View) : RecyclerView.ViewHolder(view) {
    fun bindImages(userImage: UserImage) {
        val imageView = view.findViewById<ImageView>(R.id.image_user_post)
        Glide.with(view.context).load(userImage.imageUrl).into(imageView)
    }
}

我的目标是将SelectImageViewHolder保持在位置0始终

firebaseui维护器。 onBindViewHolder(RecyclerView.ViewHolder, Int, Model)在内部进行getItem(position)调用以无缝填充您的模型。由于您正在手动注入一个项目,因此RecyClerview会认为数据库中有一些项目,并且会如您所见。为了解决这个问题,您需要覆盖本机onBindViewHolder(RecyclerView.ViewHolder, Int)方法而不致电Super:

override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
    when(holder) {
        is SelectImageViewHolder -> holder.bindModel()
        is UserImageViewHolder -> {
            // Manually get the model item
            holder.bindImages(getItem(position - 1))
        }
    }
}

相关内容

  • 没有找到相关文章

最新更新