1

我有一个ConcatAdapter使用一个(通过调用和一个 normalPagingDataAdapter由它自己包装):ConcatAdapterwithLoadStateFooterListAdapter

val concatAdapter = ConcatAdapter(
   newChatMessagesAdapter, // this is a ListAdapter
   chatMessagesHistoryAdapter.withLoadStateFooter(
       ListLoadStateAdapter(chatMessagesHistoryAdapter::retry)
   ) // this is another ConcatAdapter (that contains a PagingDataAdapter)
)

thenewChatMessageAdapter和 thechatMessageHistoryAdapter都具有多种视图类型,但两个适配器具有完全相同的视图类型:

// newChatMessagesAdapter and chatMessagesHistoryAdapter both contain this code:

override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        val message = getItem(position)
        if (message != null) {
            when (holder.itemViewType) {
                R.layout.item_chat_message_outgoing_first -> {
                    (holder as ChatMessageOutgoingFirstViewHolder).bind(message)
                }
                R.layout.item_chat_message_outgoing_following -> {
                    (holder as ChatMessageOutgoingFollowingViewHolder).bind(message)
                }
                R.layout.item_chat_message_incoming_first -> {
                    (holder as ChatMessageIncomingFirstViewHolder).bind(message, isGroupChat)
                }
                R.layout.item_chat_message_incoming_following -> {
                    (holder as ChatMessageIncomingFollowingViewHolder).bind(message)
                }
            }
        }
    }

    override fun getItemViewType(position: Int): Int {
        val currentItem = getItem(position)
        val previousItem = if (position != itemCount - 1) getItem(position + 1) else null

        return if (currentItem?.senderUid == currentUserUid) {
            if (previousItem == null || previousItem.senderUid != currentItem.senderUid) {
                R.layout.item_chat_message_outgoing_first
            } else {
                R.layout.item_chat_message_outgoing_following
            }
        } else {
            if (previousItem == null || previousItem.senderUid != currentItem?.senderUid) {
                R.layout.item_chat_message_incoming_first
            } else {
                R.layout.item_chat_message_incoming_following
            }
        }
    }

itemViewType当我启动我的应用程序时,所有视图都是空的,因为onBindViewHolder. 如果我删除withLoadStateFooter(将第二个 ConcatAdapter 变成我自己的PagingDataAdapter)并添加它,它可能会起作用ConcatAdapter.Config.Builder().setIsolateViewTypes(false).build(),可能是因为两个适配器具有完全相同的viewTypes. 但是,一旦我添加withLoadStateFooter它也停止工作,我又回到了空视图持有者。

所以简而言之,我的问题是:如何viewTypes在我的适配器中使用不同的组合在 a 中ConcatAdapter

4

0 回答 0