列表为空时,Android分页库显示空的Viewholder

时间:2019-09-27 09:10:27

标签: android kotlin paging

我正在将页面库与recyclerView一起使用,当我从服务器接收到空的arrayList时,我的任务是显示空的viewHolder。

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

/**
 * 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
            }
        }
    }
}
  

当我收到空的arrayList时,请帮助显示空的viewHolder

enter image description here

1 个答案:

答案 0 :(得分:1)

我是这样做的

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
LocalDateTime dateTime = LocalDateTime.of(1986, Month.APRIL, 8, 12, 30);
String formattedDateTime = dateTime.format(formatter); // "1986-04-08 12:30"

并且还更改了linearlayoutmanager以使用覆盖方法自定义一个

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()
    }