Skip to content

Commit

Permalink
EndlessScroll class added
Browse files Browse the repository at this point in the history
  • Loading branch information
[email protected] committed Jun 13, 2019
1 parent f87437d commit 954ebfa
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,6 @@ dependencies {
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'com.google.android.material:material:1.0.0'

implementation 'com.facebook.stetho:stetho:1.5.1'
debugImplementation 'com.facebook.stetho:stetho:1.5.1'
debugImplementation 'com.amitshekhar.android:debug-db:1.0.6'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.arabia_it.baserapp.util

/**
* Created by Net6 on 11/12/2017.
*/

import android.widget.AbsListView
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView

abstract class EndlessScrollListener : RecyclerView.OnScrollListener {
// The minimum number of items to have below your current scroll position
// before loading more.
private var visibleThreshold = 5

constructor(visibleThreshold: Int) {
this.visibleThreshold = visibleThreshold
}

override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
super.onScrolled(recyclerView, dx, dy)

recyclerView.layoutManager?.let { layoutManager ->

val totalItemCount = layoutManager.itemCount
val visibleItemCount = layoutManager.childCount
var firstVisibleItem = 0
when (layoutManager) {
is LinearLayoutManager -> firstVisibleItem = layoutManager.findFirstVisibleItemPosition()
is GridLayoutManager -> firstVisibleItem = layoutManager.findFirstVisibleItemPosition()
else -> return
}
if (firstVisibleItem + visibleItemCount + visibleThreshold >= totalItemCount) {
onLoadMore()
}
}

}

abstract fun onLoadMore()


}

0 comments on commit 954ebfa

Please sign in to comment.