первый шаг должен быть объявлен как поле в адаптере, например blew
private val ADS_ITEM = 0
private val NORMAL_ITEM = 1
, поэтому на следующем шаге вы должны создать экземпляр типа viewHolder
inner class MyViewHolder(itemView: View) : ViewHolder(itemView) {
var title: TextView = itemView.item_text
}
inner class AdsMyViewHolder(itemView: View) : ViewHolder(itemView) {
var title: TextView = itemView.item_text
}
и создать новыйподходящий экземпляр viewHolder
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val inflater = LayoutInflater.from(parent.context)
return if (viewType == NORMAL_ITEM) {
val v = inflater.inflate(R.layout.item, parent, false)
vh = MyViewHolder(v)
vh as MyViewHolder
} else {
val v = inflater.inflate(R.layout.item_ads, parent, false)
vh = AdsMyViewHolder(v)
vh as AdsMyViewHolder
}
}
не забудьте переопределить getItemViewType
override fun getItemViewType(position: Int): Int {
return when (getItems()[position]) {
is AdsSampleModel -> ADS_ITEM
else -> NORMAL_ITEM
}
}
последний шаг: привязать элемент с подходящими данными
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
when (holder) {
is MyViewHolder -> {
vh = holder
val model = getItems()[position] as SampleModel
(vh as MyViewHolder).title.text = model.getId().toString()
}
else -> {
vh = holder
val model = getItems()[position] as AdsSampleModel
(vh as AdsMyViewHolder).title.text = model.getText()
}
}
}
добавить новый элемент после подкачки
fun view () {
endless = object : EndlessRecyclerViewScrollListener(layoutManager) {
fun onLoadMore(page: Int, totalItemsCount: Int, view: RecyclerView) {
getNext()
}
}
recyclerView.addOnScrollListener(endless)
}
fun getNext() {
if (hasNextPage) {
page++
val callBack = object : YourCallback {
override fun onSuccess(response: ResponseValue?) {
if (response!!.getList().size() == 0)
hasNextPage = false
else
addData(response!!.getList())
}
override fun onError() {
// error handling
}
}
}
}
private fun addData(models: List<YourModel>?) {
for (i in models?.indices) {
if (i % 10 == 0) {
list.add(ADS_TYPE)
}
}
// notify adapter must be doing by main thread
runOnUiThread { adapter.notifyDataSetChanged() }
}