GithubHelp home page GithubHelp logo

samsul-arip / paginationrecyclerview Goto Github PK

View Code? Open in Web Editor NEW

This project forked from jdroidcoder/paginationrecyclerview

0.0 1.0 0.0 198 KB

License: GNU General Public License v3.0

Kotlin 86.66% Java 13.34%

paginationrecyclerview's Introduction

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: [email protected]

    paginationrecyclerview's People

    Contributors

    anonunfeeling avatar jdroidcoder avatar

    Watchers

     avatar

    Recommend Projects

    • React photo React

      A declarative, efficient, and flexible JavaScript library for building user interfaces.

    • Vue.js photo Vue.js

      ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

    • Typescript photo Typescript

      TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

    • TensorFlow photo TensorFlow

      An Open Source Machine Learning Framework for Everyone

    • Django photo Django

      The Web framework for perfectionists with deadlines.

    • D3 photo D3

      Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

    Recommend Topics

    • javascript

      JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

    • web

      Some thing interesting about web. New door for the world.

    • server

      A server is a program made to process requests and deliver data to clients.

    • Machine learning

      Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

    • Game

      Some thing interesting about game, make everyone happy.

    Recommend Org

    • Facebook photo Facebook

      We are working to build community through open source technology. NB: members must have two-factor auth.

    • Microsoft photo Microsoft

      Open source projects and samples from Microsoft.

    • Google photo Google

      Google โค๏ธ Open Source for everyone.

    • D3 photo D3

      Data-Driven Documents codes.