3

是否可以在ConcatAdapter的不同适配器之间设置空格(边距/填充)?我只有一个简单的代码示例,如下所示,但根据设计,我必须在不同适配器之间准确添加边距(不是一个适配器中的支架项目)。实现这一目标的最佳方法是什么?

val firstAdapter: FirstAdapter = …
val secondAdapter: SecondAdapter = …
val thirdAdapter: ThirdAdapter = …
val concatAdapter = ConcatAdapter(firstAdapter, secondAdapter, thirdAdapter)

recyclerView.adapter = concatAdapter

连接适配器

4

1 回答 1

1

我设法做这样的事情:

class EmptySpaceAdapter(private val context: Context, private val height: Int) : RecyclerView.Adapter<BaseViewHolder<*>>() {

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder<*> {
    val view = View(context)
    view.layoutParams = ViewGroup.LayoutParams(RecyclerView.LayoutParams.MATCH_PARENT, height)
    return EmptySpaceViewHolder(view)
}

override fun getItemCount() = 1

class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView)

override fun onBindViewHolder(holder: BaseViewHolder<*>, position: Int) {
    when (holder) {
        is EmptySpaceViewHolder -> holder.bind(true, position)
        else -> throw IllegalArgumentException("No view holder to show this data, did you forgot to add it to the onBindViewHolder?")
    }
}

inner class EmptySpaceViewHolder(itemView: View) : BaseViewHolder<Boolean>(itemView) {
    override fun bind(item: Boolean, position: Int) {
        Timber.d("onBind empty space")
    }
  }
}

视图持有者:

abstract class BaseViewHolder<T>(itemView: View) : RecyclerView.ViewHolder(itemView) {
    abstract fun bind(item: T, position: Int)
}
于 2021-07-29T13:54:50.613 回答