title: How to implement pagination with kotlin
body: <p>Hello there recently I switch my project from java to kotlin  and I'm a very beginner in kotlin, and I want to implement pagination in my recycler view</p>
  <p>the recycler view contain images that are stored in firebase, what I want</p>
  <p><strong>Option 1</strong> is that first when the fragment is created it loads 20 images and when the user reaches the last image (20) it loads the next 20 images in the recycler view</p>
  <p><strong>Option 2</strong></p>
  <p>(optional but wanted this kind of pagination in my app )</p>
  <p>have you seen Pinterest and Instagram, if you just scroll normally you probably don't see the images are loading until if you scroll very fast, and then it shows the progress bar?</p>
  <p>for eg: Pinterest</p>
  <p>1: scroll normally or slow <a href="https://drive.google.com/file/d/1QZbuf_iTsVsAXfjINM5_Hpjl4rZ2H18x/view?usp=sharing" rel="nofollow noreferrer">Link</a></p>
  <p>2: scroll very fast <a href="https://drive.google.com/file/d/1QTHoN1R8u169YJhbu52CxCN2hCWqH7_4/view?usp=sharing" rel="nofollow noreferrer">Link</a></p>
  <p>I want this kind of functionality if possible</p>
  <p>what I think is going on in Instagram or Pinterest is for eg</p>
  <p>They load probably 20 images at the start and when the user reaches the 10th image (still 10 images are remaining to be seen by the user) they just load the next 10 images  so it just maintains the adapter with 20 images instead of reaching the end of the list to load the next images ( hence this would be an exception if user scrolls very fast)</p>
  <hr />
  <blockquote>
  <p>Note: I just don't know what it is said pagination or something else
  and also not sure that what I said up are the technique used I'm just
  speculating</p>
  </blockquote>
  <p><strong>Code</strong></p>
  <p><strong>Home_Fragment.kt</strong></p>
  <blockquote>
  <p>I only included the required code but if you want more references
  please tell me I will update the question btw the implemented pagination (don't know is it pagination or something else but it's not working)</p>
  </blockquote>
  <pre><code>val staggeredGridLayoutManager =
              StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL)
          postRecyclerView.layoutManager = staggeredGridLayoutManager
          postRecyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() {
              override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
                  visibleItemCount = staggeredGridLayoutManager.childCount
                  totalItemCount = staggeredGridLayoutManager.itemCount
                  val firstVisibleItems: IntArray? = null
                  staggeredGridLayoutManager.findFirstVisibleItemPositions(firstVisibleItems)
                  if (loading) {
                      if (totalItemCount &gt; previousTotal) {
                          loading = false
                          previousTotal = totalItemCount
                      }
                  }
                  if (!loading &amp;&amp; totalItemCount - visibleItemCount
                      &lt;= firstVisibleItem + visibleThreshold
                  ) {
                      data
                      loading = true
                  }
              }
          })
          data
          //        setupFirebaseAuth();
          shimmerFrameLayout!!.startShimmer()
          mUploads = ArrayList()
          postsAdapter = PostAdapter_Home(requireContext(), mUploads)
          postRecyclerView.adapter = postsAdapter
          postRecyclerView.scrollToPosition(saved_position)
          //        postsAdapter.stateRestorationPolicy = PREVENT_WHEN_EMPTY;
          return view
      }
   private val data: Unit
          get() {
              databaseReference.addValueEventListener(object : ValueEventListener {
                  @SuppressLint(&quot;NotifyDataSetChanged&quot;)
                  override fun onDataChange(snapshot: DataSnapshot) {
                      if (snapshot.exists()) {
                          shimmerFrameLayout!!.stopShimmer()
                          shimmerFrameLayout!!.visibility = View.GONE
                          postRecyclerView.visibility = View.VISIBLE
                          mUploads!!.clear()
                          for (dataSnapshot in snapshot.children) {
                              val upload = dataSnapshot.getValue(Upload::class.java)!!
                              upload.setmKey(dataSnapshot.key)
                              mUploads!!.add(upload)
                          }
                      }
                      postsAdapter!!.setUploads(mUploads)
                      //notify the adapter
                      postsAdapter!!.notifyItemRangeInserted(0, mUploads!!.size)
                      loading = true
                  }
  
                  override fun onCancelled(error: DatabaseError) {
                      loading = true
                  }
              })
          }
  </code></pre>
  <p><strong>PostAdapter_Home.kt</strong></p>
  <pre><code>class PostAdapter_Home(var mcontext: Context, var mUploads: MutableList&lt;Upload?&gt;?) :
      RecyclerView.Adapter&lt;PostAdapter_Home.PostViewHolder&gt;() {
      override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PostViewHolder {
          val view: View
          view = LayoutInflater.from(mcontext)
              .inflate(R.layout.post_item_container_home_ex, parent, false)
          return PostViewHolder(view)
      }
  
      override fun onBindViewHolder(holder: PostViewHolder, position: Int) {
          val shimmer = ColorHighlightBuilder()
              .setBaseColor(Color.parseColor(&quot;#F3F3F3&quot;))
              .setBaseAlpha(1f)
              .setHighlightColor(Color.parseColor(&quot;#E7E7E7&quot;))
              .setHighlightAlpha(1f)
              .setDropoff(50f)
              .build()
          val shimmerDrawable = ShimmerDrawable()
          shimmerDrawable.setShimmer(shimmer)
          val uploadCurrent = mUploads?.get(position)
          Glide.with(mcontext)
              .load(uploadCurrent?.getmImageUrl())
              .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
              .placeholder(shimmerDrawable)
              .fitCenter()
              .into(holder.imageView)
  
      }
  
      
      override fun getItemCount(): Int {
          return mUploads?.size!!
      }
  
      //    public long getId() {
      //        return this.id;
      //    }
      //
      //    @Override
      //    public long getItemId(int position) {
      //        return mUploads.get(position).Id;
      //    }
      fun setUploads(uploads: MutableList&lt;Upload?&gt;?) {
          mUploads = uploads
      }
  
      class PostViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
          val imageView: ShapeableImageView
  
          init {
              imageView = itemView.findViewById(R.id.imagePostHome)
          }
      }
  }
  </code></pre>
  <p><strong>Upload.kt</strong></p>
  <pre><code>package com.example.myappnotfinal.AdaptersAndMore
  
  import com.google.firebase.database.Exclude
  
  class Upload {
      private var mImageUrl: String? = null
      private var mKey: String? = null
  
      constructor() {}
      constructor(imageUrl: String?) {
          mImageUrl = imageUrl
      }
  
      fun getmImageUrl(): String? {
          return mImageUrl
      }
  
      fun setmImageUrl(mImageUrl: String?) {
          this.mImageUrl = mImageUrl
      }
  
      @Exclude
      fun getmKey(): String? {
          return mKey
      }
  
      @Exclude
      fun setmKey(Key: String?) {
          mKey = Key
      }
  }
  </code></pre>