1

**我想在下载完成后通知child recycler item改变,而不通知所有dataSetChanged。**
类 ParentAdapter(私有 val 监听器:(slug:String,位置:Int)-> 单元,私有 val childItemDownloadListener:(child:LearningDataResult,parentPosition:Int,childPosition:Int)-> 单元,私有 val childItemClickListener:(child: LearningDataResult, position: Int) -> Unit) : ImmutableRecyclerAdapter() { private var viewPool = RecyclerView.RecycledViewPool() lateinit var childAdapter: ChildAdapter override val itemLayoutId: Int = R.layout.content_learning

override var items: List<ParentModel> by Delegates.observable(emptyList()) { _, old, new ->
    autoNotify(old, new) { o, n -> o == n }
}

override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
    val category = items[position]

    with(holder.itemView) {
        tv_learn_title.text = category.title
        iv_title_image.load(category.image, R.drawable.ic_logo)
        childAdapter = ChildAdapter(
            childDownloadListener = { child, childPosition ->
                childItemDownloadListener.invoke(child, position, childPosition)
            },
            childItemClickListener = { child, position ->
                childItemClickListener.invoke(child, position)
            }
        )
        with(child_recyclerview) {
            addHorizontalLayoutManager(holder.itemView.context)
            adapter = childAdapter
            setRecycledViewPool(viewPool)
        }
        shimmerFrameLayout.visible()
        shimmerFrameLayout.startShimmerAnimation()


        if (category.detailDataStatus != "fetched" && category.detailDataStatus != "fetching" && !category.isEmpty) {
            listener.invoke(category.slug, position)
        } else {
            childAdapter.items = category.learningDataResults
            shimmerFrameLayout.gone()
            shimmerFrameLayout.stopShimmerAnimation()
        }

        if (category.isEmpty) {
            tv_no_data.visible()
            tv_no_data.text = "No content available for ${category.title}"
        } else {
            tv_no_data.invisible()

        }
    }
}

}

类 ChildAdapter( 私有 val childDownloadListener: (child: LearningDataResult, position: Int) -> Unit, private val childItemClickListener: (child: LearningDataResult, position: Int) -> Unit) :

ImmutableRecyclerAdapter<LearningDataResult>() {
override val itemLayoutId: Int = R.layout.content_child_recycler_item

override var items: List<LearningDataResult> by Delegates.observable(emptyList()) { _, old, new ->
    autoNotify(old, new) { o, n -> o == n }
}


override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
    holder.setIsRecyclable(false)
    val child = items[position]
    with(holder.itemView) {

        tv_learn_title.text = child.title
        iv_learn.load(child.skillLogo, R.drawable.ic_grey)

        tv_item_category.text = child.category
        val dir = File("/LevelUp/downloads")
        var fileName = "${child.uuid}"

        val file =
            Environment.getExternalStorageDirectory().path + dir
        val downloadItemData = File(file, fileName)

        if (downloadItemData.exists()) {

            iv_download.invisible()
            iv_downloaded.visible()
        }

        when (child.downloadStatus) {
            IDLE -> {
                download_progress.gone()
                iv_download.visible()
                iv_downloaded.gone()
            }
            DOWNLOADING -> {
                download_progress.visible()
                iv_download.invisible()
                iv_downloaded.invisible()
            }
            DOWNLOADED -> {
                download_progress.gone()
                iv_download.invisible()
                iv_downloaded.visible()
            }
        }
        iv_download.setOnClickListener {
            (holder.itemView.context as AppCompatActivity).askRequiredPermission(
                arrayOf(
                    Manifest.permission.WRITE_EXTERNAL_STORAGE
                )
            ) {
                download_progress.visible()
                iv_download.invisible()
                iv_downloaded.invisible()
                childDownloadListener.invoke(child, position)
            }
        }

        setOnClickListener {
            childItemClickListener.invoke(child, position)
        }
    }

}

}

4

0 回答 0