Android 分页库在列表为空时显示空视图

Android paging library show empty viewholder when list is empty

我正在使用带有 recyclerView 的分页库,我的任务是在我从服务器接收到空的 arrayList 时显示空的 viewHolder。

我试图检查列表是否为空,但是当我得到响应时它调用 Observer 方法 2 次(第一次是空列表,第二次是真实列表),在我使用 swipeRefresh 之后调用 DataSource class 发送空列表,因为它在缓存中已经有相同的列表。我也有分页,检查数据集大小很困难,因为有很多情况。

/**
 * Simple Adapter used to show list of Appeals with pagination
 */
class MyAppealsAdapter : PagedListAdapter<Appeal, RecyclerView.ViewHolder>(diffCallBack) {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return AppealsViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.item_my_appeal, parent, false))
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        when (holder) {
            is AppealsViewHolder -> holder.bind(position)
        }
    }

    inner class AppealsViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        @SuppressLint("SimpleDateFormat")
        fun bind(position: Int) {
            getItem(position)?.let { item ->
                item.fields?.get(AppealKeyObj.K_SUB_ISSUE)?.let {
                    itemView.mTypeOfAppeal.text = it
                }
            }
        }
    }

    companion object {
        open var diffCallBack: DiffUtil.ItemCallback<Appeal> = object : DiffUtil.ItemCallback<Appeal>() {
            override fun areItemsTheSame(oldItem: Appeal, newItem: Appeal): Boolean {
                return oldItem.id === oldItem.id
            }

            override fun areContentsTheSame(oldItem: Appeal, newItem: Appeal): Boolean {
                return oldItem == newItem
            }
        }
    }
}

Please help to show empty viewHolder when I receive empty arrayList

我是这样做的

override fun getItemViewType(position: Int): Int {
        return if (super.getItemCount() == 0) EMPTY else FILLED
    }

    override fun getItemCount(): Int {
        return if (super.getItemCount() == 0) 1 else super.getItemCount()
    }

并且还将 linearlayoutmanager 更改为具有覆盖方法的自定义

override fun onLayoutChildren(recycler: Recycler?, state: RecyclerView.State?) {
        try {
            super.onLayoutChildren(recycler, state)
        } catch (e: IndexOutOfBoundsException) {
            Timber.e(e)
        }
    }