RecyclerView onCreateViewHolder不被调用

时间:2019-11-13 11:51:08

标签: android android-recyclerview

由于某些原因,我的recyclerview适配器的onCreateViewHolder没有被调用,我无法弄清楚原因。 最初,我在所有recyclerview列表中都使用一个适配器,并且遇到了同样的问题,因此我制作了一个单独的适配器以找出原因。我想相信配置正确。 这是我的适配器

class BrowseMediaRecyclerAdapter(
    private val clickListener: (Upload) -> Unit,
    private val moreInfoListener: (Upload) -> Unit) : RecyclerView.Adapter<BrowseMediaRecyclerAdapter.ViewHolder>() {


    lateinit var binding: BrowseMediaItemListBinding


    /**
     *
     * Autonotify msee
     *
     */
    private var uploadList: List<Upload> by Delegates.observable(emptyList()) { _, oldList, newList ->
        autoNotify(oldList, newList) {old, new -> old.id == new.id}
    }


    fun updateList(items: List<Upload>?){
        uploadList = items!!
        Log.e("Items", "Item received: ${items.size}")
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val inflater = LayoutInflater.from(parent.context)

        Log.e("Bind", "View holder created")
        binding = DataBindingUtil.inflate(inflater, R.layout.browse_media_item_list, parent, false)

        return ViewHolder(binding.root)

    }

    override fun getItemViewType(position: Int): Int = position

    override fun getItemCount(): Int = uploadList.size

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {

        val item = uploadList[position]

        Picasso.get()
            .load(item.thumbnailLink)
            .placeholder(R.drawable.ic_local_movies_black_24dp)
            .fit()
            .transform(TransformImage(16f))
            .into(holder.thumb)


        holder.bind(uploadList[position], binding, clickListener, moreInfoListener)
        Log.e("Bind", "Item binded: ${item.id}")
    }

    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

        val thumb: ImageView = itemView.media_thumb
        val authorThumb: ImageView = itemView.authorImage
        private val moreInfo : FrameLayout = itemView.moreDetail


        /**
         *
         * Bind data
         *
         *
         */

        fun bind(item: Upload, binding: BrowseMediaItemListBinding, clickListener: (Upload) -> Unit, moreInfoListener: (Upload) -> Unit) {

            binding.uploadItem = item

            thumb.setOnClickListener{clickListener(item)}

            moreInfo.setOnClickListener{moreInfoListener(item)}
        }

    }

} 

在我的Fragment中,我以这种方式配置

mediaRecyclerAdapter = BrowseMediaRecyclerAdapter(
            { upload: Upload -> onMediaItemClicked(upload) },
            { upload: Upload -> onMediaMoreInfoClicked(upload) })



        binding.subjectUploadsRecyclerView.apply {

            this.adapter = mediaRecyclerAdapter

            this.layoutManager = LinearLayoutManager(activity, RecyclerView.VERTICAL, false)

            this.setHasFixedSize(true)

            this.setItemViewCacheSize(10)

        }

稍后再在我的观察者中更新列表

private val loadUploadItemsObserver = Observer<SubjectUploads> { subject ->

        binding.selectedSubjectText.text = subject.subject_title
        mediaRecyclerAdapter.updateList(subject.uploads)

    }

我的xml文件

<androidx.recyclerview.widget.RecyclerView
                            android:id="@+id/subjectsUploadsRecyclerView"
                            android:layout_width="0dp"
                            android:layout_height="wrap_content"
                            app:layout_constraintEnd_toEndOf="parent"
                            app:layout_constraintStart_toStartOf="parent"
                            app:layout_constraintTop_toBottomOf="@id/selectedSubjectTextFrame"
                            tools:listitem="@layout/media_item_list" /> 

我可能做错了什么?可以帮忙吗

0 个答案:

没有答案