Skip to content

Instantly share code, notes, and snippets.

@Sirelon
Last active October 31, 2018 15:50
Show Gist options
  • Save Sirelon/82022445081e8a4c80ad144bafea0ee7 to your computer and use it in GitHub Desktop.
Save Sirelon/82022445081e8a4c80ad144bafea0ee7 to your computer and use it in GitHub Desktop.
PagedBaseAdapter created for using BaseAdapter with android.arch.paging library. In example this can be useful, when you need to support pagination on AdapterView. For using you need to extend this class, and simply implement method getView
package com.sirelon
import android.arch.paging.PagedList
import android.arch.paging.PagedListAdapterHelper
import android.support.v7.recyclerview.extensions.ListAdapterConfig
import android.support.v7.util.ListUpdateCallback
import android.widget.BaseAdapter
/**
* Created on 2/7/18 12:35
*/
abstract class PagedBaseAdapter<T>(listConfig: ListAdapterConfig<T>) : BaseAdapter(), ListUpdateCallback {
private val pagedHelper: PagedListAdapterHelper<T>
init {
pagedHelper = PagedListAdapterHelper(this, listConfig)
}
fun setPagedList(pagedList: PagedList<T>) = pagedHelper.setList(pagedList)
/* Methods from Base Adapter */
override fun getCount() = pagedHelper.itemCount
override fun getItem(position: Int) = pagedHelper.getItem(position)
override fun getItemId(position: Int) = position.toLong()
/* Methods from ListUpdateCallback */
override fun onChanged(position: Int, count: Int, payload: Any?) {
notifyDataSetChanged()
}
override fun onMoved(fromPosition: Int, toPosition: Int) {
notifyDataSetChanged()
}
override fun onInserted(position: Int, count: Int) {
notifyDataSetChanged()
}
override fun onRemoved(position: Int, count: Int) {
notifyDataSetChanged()
}
}
@antTony00
Copy link

I couldn't find android.arch.paging.PagedListAdapterHelper. Can you please share this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment