Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v4版本BaseMultiItemAdapter增加diff支持(临时解决) #3873

Open
luke-li-n opened this issue Jul 8, 2024 · 3 comments
Open

v4版本BaseMultiItemAdapter增加diff支持(临时解决) #3873

luke-li-n opened this issue Jul 8, 2024 · 3 comments

Comments

@luke-li-n
Copy link

v4版本BaseMultiItemAdapter如何支持Differ?

@Suguiqiang
Copy link

+1

@xiangsss
Copy link

同问

@luke-li-n
Copy link
Author

luke-li-n commented Oct 16, 2024

临时解决方案:
使用现有的BaseDifferAdapter 自己实现多类型逻辑

MultiItemProvider

abstract class MultiItemProvider<T, VH : RecyclerView.ViewHolder> {
    abstract fun onBind(holder: VH, position: Int, item: T?)
    abstract fun onCreate(context: Context, parent: ViewGroup, viewType: Int): VH
}

Adapter

class DifferAdapter() :BaseDifferAdapter<UiData, RecyclerView.ViewHolder>(DiffCallBack()) {
    private val textMessageItemProvider =
        object : MultiItemProvider<UiData, TextViewHolder>() {
            override fun onBind(holder: TextViewHolder, position: Int, item: UiData?) {

            }

            override fun onCreate(
                context: Context, parent: ViewGroup, viewType: Int
            ): TextViewHolder{
                return TextViewHolder(parent)
            }
        }
    private val messageItemProviders = mapOf(
        SupportType.Text.type to textItemProvider,
        SupportType.Unknown.type to unknownItemProvider
    )

    override fun getItemViewType(position: Int, list: List<UiData>): Int {
        val item = getItem(position)
        return item?.messageType?.type ?: SupportType.Unknown.type
    }

    override fun onCreateViewHolder(
        context: Context,
        parent: ViewGroup,
        viewType: Int
    ): RecyclerView.ViewHolder {
        val provider: MultiItemProvider<UiMessage, out RecyclerView.ViewHolder> =
            messageItemProviders[viewType]
                ?: throw IllegalArgumentException("No provider found for message type: ${viewType}")
        return provider.onCreate(parent.context, parent, viewType)
    }

    override fun onBindViewHolder(
        holder: RecyclerView.ViewHolder,
        position: Int,
        item: UiData?
    ) {
        val provider: MultiItemProvider<UiMessage, out RecyclerView.ViewHolder> =
            messageItemProviders[item?.messageType?.type]
                ?: throw IllegalArgumentException("No provider found for message type: ${item?.messageType?.type}")
        @Suppress("UNCHECKED_CAST")
        (provider as MultiItemProvider<UiMessage, RecyclerView.ViewHolder>).onBind(
            holder,
            position,
            item
        )
    }
}

@luke-li-n luke-li-n changed the title BaseMultiItemAdapter增加Differ支持 v4版本BaseMultiItemAdapter增加Differ支持(临时解决) Oct 16, 2024
@luke-li-n luke-li-n changed the title v4版本BaseMultiItemAdapter增加Differ支持(临时解决) v4版本BaseMultiItemAdapter增加diff支持(临时解决) Oct 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants