Skip to content

Load More In RecyclerAdapter

kevadiya krunal edited this page Apr 30, 2019 · 4 revisions

Install

class LoadMoreActivity : AppCompatActivity() {
    private lateinit var noPaginate: NoPaginate

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_loadmore)
        setupLoadMore()
    }

    private fun setupLoadMore() {
        noPaginate = NoPaginate {
           loadingTriggerThreshold = 0
            recyclerView = recyclerViews
            loadingItem = LoadingItem.DEFAULT
            errorItem = ErrorItem.DEFAULT
            direction = Direction.UP
            onLoadMore = {
                noPaginate.showError(false)
                noPaginate.showLoading(true)
                Handler(Looper.getMainLooper()).postDelayed({
                    if (Random(25).nextInt() % 2 == 0) {
                        count++
                        noPaginate.showLoading(false)
                        noPaginate.setNoMoreItems(count > 3)
                        recyclerView.post {
                            val list = MutableList(10) { index -> "LoadMore -> ${index + (adapter.itemCount + 1)}" }
                            adapter + list
                        }
                    } else {
                        noPaginate.showLoading(false)
                        noPaginate.showError(true)
                    }
                }, 5000)
            }
        }
    }

    override fun onDestroy() {
        noPaginate.unbind()
        super.onDestroy()
    }
}

Actions

   noPaginate.showLoading(false)
   noPaginate.showError(false)
   noPaginate.setNoMoreItems(false); //Method onLoadMore will not to call
   noPaginate.unbind(); //Don't forget call it on onDestroy();

Custom Loading and Error

For custom error and loaging item just implement the interfaces ErrorItem or LoadingItem

Custom error:

    class CustomErrorItem : ErrorItem {
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
            val view = LayoutInflater.from(parent.context).inflate(R.layout.item_error, parent, false)
            return object : RecyclerView.ViewHolder(view) {}
        }

        override fun onBindViewHolder(
            holder: RecyclerView.ViewHolder,
            position: Int,
            repeatListener: OnRepeatListener?
        ) {
            val btnRepeat = holder.itemView.findViewById<Button>(R.id.btnRepeat)
            btnRepeat.setOnClickListener {
                repeatListener?.onClickRepeat()
            }
        }
    }

Custom loading:

    class CustomLoadingItem : LoadingItem {
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
            val view = LayoutInflater.from(parent.context).inflate(R.layout.item_loading, parent, false)
            return object : RecyclerView.ViewHolder(view) {}
        }

        override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {}
    }

Install with custom items and trigger threshold

noPaginate = NoPaginate {
            loadingTriggerThreshold = 0
            recyclerView = recyclerViews
            loadingItem = CustomLoadingItem()
            errorItem = CustomErrorItem()
            direction = Direction.DOWN
            onLoadMore = {}
}

Roadmap

  1. Double-sided pagination
  2. Delegate for Presenter or Interactor, with implementation Limit/Offset and Page pagination

Clone this wiki locally