我将recyclerView
与adapter
一起使用。在我的模型层中也具有timestamp
值。但是我为我的商品使用了不同的布局来显示日期,并且想要做这样的事情:
27 february
item1
item2
26 february
item1
我如何在getItemViewType
函数中构造日期等于特征?我了解图书馆,但我想靠自己做。
我的adapter
课:
private inner class BatteryStoryAdapter(private val batteryStoryList : List<Battery>) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
override fun onCreateViewHolder(parent : ViewGroup, viewType : Int) : RecyclerView.ViewHolder {
return when(viewType){
HEADER_VIEW_INDEX -> DateStoryHolder(LayoutInflater.from(activity).inflate(R.layout.battery_story_date_item, parent, false))
else -> BatteryStoryHolder(LayoutInflater.from(activity).inflate(R.layout.battery_story_item, parent, false))
}
}
override fun getItemCount(): Int {
return batteryStoryList.size
}
override fun onBindViewHolder(holder : RecyclerView.ViewHolder, position : Int) {
when(holder){
is BatteryStoryHolder -> holder.bind(batteryStoryList[position])
is DateStoryHolder -> holder.bindDate(batteryStoryList[position].date.toFormatDate(DATE_FORMAT))
}
}
override fun getItemViewType(position: Int): Int {
return BODY_VIEW_INDEX
}
}