PaginationRecyclerView

Additional

Language
Kotlin
Version
v1.0.0 (Mar 5, 2019)
Created
Mar 5, 2019
Updated
Mar 5, 2019
Owner
JDroidCoder
Contributors
JDroidCoder
AnonUnfeeling
2
Activity
Badge
Generate
Download
Source code
APK file

Promotion

PaginationRecyclerView

This library based on RecyclerView, and has a purpose easy work with lazy loading (pagination) data.

You can use this library with our other library for work with Network API: https://github.com/JDroidCoder/apiservice

Install:

  • Add jitpack repository in gradle(project level)
  • allprojects { repositories { ... maven { url 'https://jitpack.io' } } }
  • Add dependency in gradle(app level)
  • dependencies { implementation 'com.github.JDroidCoder:PaginationRecyclerView:v1.0.0' }
  • enjoy ;)

Initialize library:

  • Init in the xml
  • <jdroidcoder.ua.paginationrecyclerview.PaginationRecyclerView
      android:id="@+id/paginationRecyclerView"
      android:layout_width="match_parent"
      android:layout_height="match_parent" />
    
  • Add OnPageChangeListener
  •   
     paginationRecyclerView?.setOnPageChangeListener(object : OnPageChangeListener {
        override fun onPageChange(page: Int) {
           // Here you will receive next page
        }
    })
    

    Code example: Xml (activity_main):

    <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
         xmlns:tools="http://schemas.android.com/tools"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         tools:context=".MainActivity">
         
         <jdroidcoder.ua.paginationrecyclerview.PaginationRecyclerView
      android:id="@+id/paginationRecyclerView"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      tools:context=".MainActivity" />
    </android.support.constraint.ConstraintLayout>
    

    Activity class:

    class MainActivity : AppCompatActivity() {
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)
            val adapter =  RecyclerViewAdapter(generateList(1))
            paginationRecyclerView.adapter =adapter
            paginationRecyclerView?.layoutManager = LinearLayoutManager(this)
            paginationRecyclerView?.setOnPageChangeListener(object : OnPageChangeListener {
                override fun onPageChange(page: Int) {
                    adapter.addItems(generateList(page))
                }
            })
        }
    
        private fun generateList(page: Int): ArrayList {
            val arrayList = ArrayList()
            for (i in 0..10) {
                arrayList.add("Item $i page $page")
            }
            return arrayList
        }
    }
    

    item_style.xml:

    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/itemTextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="20dp"
        android:textSize="20sp" />
    

    RecyclerViewAdapter:

    class RecyclerViewAdapter(private val items: ArrayList) : RecyclerView.Adapter() {
    
        override fun onCreateViewHolder(parent: ViewGroup, p1: Int): ViewHolder {
            return ViewHolder(LayoutInflater.from(parent?.context).inflate(R.layout.item_style, parent, false))
        }
    
        override fun onBindViewHolder(holder: ViewHolder, position: Int) {
            holder.itemTextView.text = items?.get(position)
        }
    
        override fun getItemCount(): Int {
            return items.size
        }
        
        fun addItems(items: ArrayList){
            this.items.addAll(items)
            notifyDataSetChanged()
        }
    }
    
    class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val itemTextView: TextView = view.itemTextView
    }
    

    If you have any question ask us here or write to email: jdroidcoder@gmail.com