gpt4 book ai didi

android - 如何使用 Android 分页库向 PagedList 添加 header

转载 作者:行者123 更新时间:2023-12-02 12:00:55 25 4
gpt4 key购买 nike

我尝试以正常方式添加标题(不同的 View 类型、将数据计数增加 1 等),但分页列表滚动条跳到列表底部。

此后我遇到了the AsyncPagedListDiffer并尝试实现它,但现在除了标题之外我的所有项目都消失了。谁能看出我做错了什么吗?

    class MerchantHistoryAdapter : PagedListAdapter<MerchantHistoryResponse.Transaction, BaseViewHolder>(MerchantHistoryDiffUtilCallback()) {

lateinit var listener: HistoryItemListAdapterListener
lateinit var headerListener: HistoryItemHeaderListAdapterListener

lateinit var networkStateListener: NetworkStateListAdapterListener
private var networkState: NetworkState = NetworkState.LOADING

val adapterCallback = AdapterListUpdateCallback(this)

val listUpdateCallback = object : ListUpdateCallback {
override fun onInserted(position: Int, count: Int) {
adapterCallback.onInserted(position + 1, count)
}

override fun onRemoved(position: Int, count: Int) {
adapterCallback.onRemoved(position + 1, count)
}

override fun onMoved(fromPosition: Int, toPosition: Int) {
adapterCallback.onMoved(fromPosition + 1, toPosition + 1)
}

override fun onChanged(position: Int, count: Int, payload: Any?) {
adapterCallback.onChanged(position + 1, count, payload)
}
}

val differ = AsyncPagedListDiffer<MerchantHistoryResponse.Transaction>(listUpdateCallback,
AsyncDifferConfig.Builder<MerchantHistoryResponse.Transaction>(MerchantHistoryDiffUtilCallback()).build())

override fun getItem(position: Int): MerchantHistoryResponse.Transaction? {
return differ.getItem(position - 1)
}

override fun submitList(pagedList: PagedList<MerchantHistoryResponse.Transaction>?) {
differ.submitList(pagedList)
}

override fun getCurrentList(): PagedList<MerchantHistoryResponse.Transaction>? {
return differ.currentList
}

override fun onBindViewHolder(holder: BaseViewHolder, position: Int) {
holder.onBind(position)
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder {
return when (viewType) {
R.layout.item_merchant_history_header -> {
val view = ItemMerchantHistoryHeaderBinding.inflate(LayoutInflater.from(parent.context))
MerchantHistoryHeaderViewHolder(view)
}
R.layout.item_merchant_history -> {
val view = ItemMerchantHistoryBinding.inflate(LayoutInflater.from(parent.context))
MerchantHistoryViewHolder(view)
}
R.layout.item_network_state -> {
val view = ItemNetworkStateBinding.inflate(LayoutInflater.from(parent.context))
NetworkStateViewHolder(view)
}
else -> throw IllegalArgumentException("unknown view type $viewType")
}
}

override fun getItemViewType(position: Int): Int {
return if(position == 0){
R.layout.item_merchant_history_header
}
else if (hasExtraRow() && position == itemCount - 1) {
R.layout.item_network_state
} else {
R.layout.item_merchant_history
}
}

private fun hasExtraRow() = networkState != null && networkState != NetworkState.LOADED

override fun getItemCount(): Int {
//Header item, plus the extra row
return super.getItemCount() + if (hasExtraRow()) 2 else 1
}

fun setNetworkState(newNetworkState: NetworkState) {
Timber.d("Setting network state....." + newNetworkState.status)
val previousState = this.networkState
val hadExtraRow = hasExtraRow()
this.networkState = newNetworkState
val hasExtraRow = hasExtraRow()
if (hadExtraRow != hasExtraRow) {
if (hadExtraRow) {
notifyItemRemoved(differ.itemCount + 1)
} else {
notifyItemInserted(differ.itemCount + 1)
}
} else if (hasExtraRow && previousState != newNetworkState) {
notifyItemChanged(itemCount - 1)
}
}

inner class MerchantHistoryViewHolder(private val binding: ItemMerchantHistoryBinding)
: BaseViewHolder(binding.root), MerchantHistoryItemViewModel.ListItemViewModelListener {

private var itemViewModel: MerchantHistoryItemViewModel? = null

override fun onBind(position: Int) {
val transaction = getItem(position)

transaction?.let {
itemViewModel = MerchantHistoryItemViewModel(binding.root.context, it, this)
binding.viewModel = itemViewModel
binding.executePendingBindings()
}
}

override fun onItemClick(item: MerchantHistoryResponse.Transaction) {
listener.onItemClick(item)
}
}

interface HistoryItemListAdapterListener {
fun onItemClick(item: MerchantHistoryResponse.Transaction)
}

inner class NetworkStateViewHolder(private val binding: ItemNetworkStateBinding)
: BaseViewHolder(binding.root), NetworkStateItemViewModel.ListItemViewModelListener {

private var itemViewModel: NetworkStateItemViewModel? = null

override fun onBind(position: Int) {
Timber.d("binding position " + position + " with network state of " + networkState.status)
itemViewModel = NetworkStateItemViewModel(binding.root.context, networkState,this)
binding.viewModel = itemViewModel
binding.executePendingBindings()
}

override fun onRetryClick() {
networkStateListener.onRetryClick()
}
}

interface NetworkStateListAdapterListener {
fun onRetryClick()
}

inner class MerchantHistoryHeaderViewHolder(private val binding: ItemMerchantHistoryHeaderBinding)
: BaseViewHolder(binding.root), MerchantHistoryHeaderItemViewModel.ListItemViewModelListener {

private var itemViewModel: MerchantHistoryHeaderItemViewModel? = null

override fun onBind(position: Int) {
itemViewModel = MerchantHistoryHeaderItemViewModel(binding.root.context, this)
binding.viewModel = itemViewModel
binding.executePendingBindings()
}

override fun onItemClick() {
headerListener.onItemClick()
}
}

interface HistoryItemHeaderListAdapterListener {
fun onItemClick()
}

class MerchantHistoryDiffUtilCallback : DiffUtil.ItemCallback<MerchantHistoryResponse.Transaction>() {

override fun areItemsTheSame(oldItem: MerchantHistoryResponse.Transaction, newItem: MerchantHistoryResponse.Transaction): Boolean {
return oldItem.uid == newItem.uid
}

override fun areContentsTheSame(oldItem: MerchantHistoryResponse.Transaction, newItem: MerchantHistoryResponse.Transaction): Boolean {
return oldItem.uid == newItem.uid
}
}
}

最佳答案

哈哈,好吧,我在发布后很快就明白了:

这个:

override fun getItemCount(): Int {
//Header item, plus the extra row
return super.getItemCount() + if (hasExtraRow()) 2 else 1
}

应该是:

override fun getItemCount(): Int {
//Header item, plus the extra row
return differ.itemCount + if (hasExtraRow()) 2 else 1
}

希望这对 future 的 Google 用户有所帮助。

关于android - 如何使用 Android 分页库向 PagedList 添加 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57853321/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com