在多视图类型场景中滚动时,Recyclerview 项目会发生变化

问题描述 投票:0回答:1

嗨,我正在使用 recyclerview 来处理列表的多视图类型

但是当我滚动时,单元格有时会发生变化

我参考了很多SO的答案,但没有成功

也覆盖 getitemid 但没有工作需要以下代码的帮助

这是我的适配器代码:-

class Adapter(val onItemClick: (Data) -> Unit) :
BaseRecyclerViewAdapter<Data, BaseRecyclerViewHolder<Data>>() {

companion object {
    const val VIEW_LINK = 1
    const val VIEW_POST = 2
    const val VIEW_LOADER = 3
}

override fun createItemViewHolder(
    parent: ViewGroup,
    viewType: Int
): BaseRecyclerViewHolder<Data> {
    val layoutInflater = LayoutInflater.from(parent.context)
    when (viewType) {
        VIEW_LINK -> {
            return LinkViewHolder(
                LayoutLinkPreviewBinding.inflate(
                    layoutInflater,
                    parent,
                    false
                )
            )
        }

        VIEW_POST -> {
            return PostViewHolder(
                LayoutPostPreviewBinding.inflate(
                    layoutInflater,
                    parent,
                    false
                )
            )
        }

        else -> {
            return LoadMoreViewHolder(
                ItemProgressLoadMoreBinding.inflate(
                    layoutInflater,
                    parent,
                    false
                ).root
            )
        }
    }
}

override fun bindItemViewHolder(holder: BaseRecyclerViewHolder<Data>, position: Int) {
    when (holder.itemViewType) {
        VIEW_LINK -> {
            items.let {
                (holder as LinkViewHolder).bind(it[position])
            }
        }

        VIEW_POST -> {
            items.let {
                (holder as PostViewHolder).bind(it[position])
            }
        }

        VIEW_LOADER -> {
            (holder as LoadMoreViewHolder).pdLoadMore.show()

        }

    }

}

override fun getItemViewType(position: Int): Int {
    return when (items[position].type) {
        "link" -> {
            VIEW_LINK
        }

        "post" -> {
            VIEW_POST
        }

        "media" -> {
            VIEW_POST
        }

        else -> {
            VIEW_LOADER
        }
    }
}

inner class LinkViewHolder(private val layoutLinkPreviewBinding: LayoutLinkPreviewBinding) :
    BaseRecyclerViewHolder<Data>(layoutLinkPreviewBinding.root) {
    private var coroutineScope: CoroutineScope? = null
    var link: String? = null


    override fun bind(item: Data) {
        ensureCoroutineScope()
        fetchLinkPreview(item)
    }

}

inner class PostViewHolder(private val postPreviewBinding: LayoutPostPreviewBinding) :
    BaseRecyclerViewHolder<Data>(postPreviewBinding.root) {

    override fun bind(item: Data) {

        setUpUI(item)
    }
}

inner class LoadMoreViewHolder(itemView: View) :
    BaseRecyclerViewHolder<Data>(itemView) {
    var pdLoadMore: loaderView

    init {
        pdLoadMore = itemView.findViewById(R.id.pdLoadMore)
    }

    override fun bind(item: ShowCaseData) {}
}

override fun getItemId(position: Int): Long {
    return items[position].hashCode().toLong()
}

}

请帮忙提前致谢

java android kotlin android-recyclerview android-support-library
1个回答
0
投票

重写适配器类中的 getItemViewType, 默认情况下,它看起来像,

override fun getItemViewType(position: Int): Int {
    return super.getItemViewType(position)
}

将其更改为以下,

override fun getItemViewType(position: Int): Int {
    return position
}

这将返回 itemview 的当前位置。

© www.soinside.com 2019 - 2024. All rights reserved.