Skip to content

Instantly share code, notes, and snippets.

@johncodeos johncodeos/Constant.kt
Last active Aug 27, 2019

Embed
What would you like to do?
object Constant {
const val VIEW_TYPE_ITEM = 0
const val VIEW_TYPE_LOADING = 1
}
private fun setRVLayoutManager() {
mLayoutManager = GridLayoutManager(this, 3)
items_grid_rv.layoutManager = mLayoutManager
items_grid_rv.setHasFixedSize(true)
items_grid_rv.adapter = adapterGrid
(mLayoutManager as GridLayoutManager).spanSizeLookup = object : GridLayoutManager.SpanSizeLookup() {
override fun getSpanSize(position: Int): Int {
return when (adapterGrid.getItemViewType(position)) {
VIEW_TYPE_ITEM -> 1
VIEW_TYPE_LOADING -> 3 //number of columns of the grid
else -> -1
}
}
}
}
private fun setRVScrollListener() {
scrollListener = RecyclerViewLoadMoreScroll(mLayoutManager as GridLayoutManager)
scrollListener.setOnLoadMoreListener(object :
OnLoadMoreListener {
override fun onLoadMore() {
LoadMoreData()
}
})
items_grid_rv.addOnScrollListener(scrollListener)
}
class Items_RVAdapter(private var itemsCells: ArrayList<String?>) :
RecyclerView.Adapter<RecyclerView.ViewHolder>() {
lateinit var mcontext: Context
class ItemViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView)
class LoadingViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView)
fun addData(dataViews: ArrayList<String?>) {
this.itemsCells.addAll(dataViews)
notifyDataSetChanged()
}
fun getItemAtPosition(position: Int): String? {
return itemsCells[position]
}
fun addLoadingView() {
//Add loading item
Handler().post {
itemsCells.add(null)
notifyItemInserted(itemsCells.size - 1)
}
}
fun removeLoadingView() {
//Remove loading item
if (itemsCells.size != 0) {
itemsCells.removeAt(itemsCells.size - 1)
notifyItemRemoved(itemsCells.size)
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
mcontext = parent.context
return if (viewType == Constant.VIEW_TYPE_ITEM) {
val view = LayoutInflater.from(parent.context).inflate(R.layout.item_row, parent, false)
ItemViewHolder(view)
} else {
val view = LayoutInflater.from(mcontext).inflate(R.layout.progress_loading, parent, false)
LoadingViewHolder(view)
}
}
override fun getItemCount(): Int {
return itemsCells.size
}
override fun getItemViewType(position: Int): Int {
return if (itemsCells[position] == null) {
Constant.VIEW_TYPE_LOADING
} else {
Constant.VIEW_TYPE_ITEM
}
}
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
if (holder.itemViewType == Constant.VIEW_TYPE_ITEM) {
holder.itemView.itemtextview.text = itemsCells[position]
}
}
}
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
if (holder.itemViewType == Constant.VIEW_TYPE_ITEM) {
holder.itemView.itemtextview.text = itemsCells[position]
}else{
val layoutParams = holder.itemView.layoutParams as StaggeredGridLayoutManager.LayoutParams
layoutParams.isFullSpan = true
}
}
private fun setRVLayoutManager() {
mLayoutManager = LinearLayoutManager(this)
items_rv.layoutManager = mLayoutManager
items_rv.setHasFixedSize(true)
}
private fun setRVScrollListener() {
mLayoutManager = LinearLayoutManager(this)
scrollListener = RecyclerViewLoadMoreScroll(mLayoutManager as LinearLayoutManager)
scrollListener.setOnLoadMoreListener(object : OnLoadMoreListener {
override fun onLoadMore() {
LoadMoreData()
}
})
items_rv.addOnScrollListener(scrollListener)
}
interface OnLoadMoreListener {
fun onLoadMore()
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="horizontal">
<ProgressBar
android:id="@+id/progressbar"
android:layout_width="24dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" />
</LinearLayout>
</LinearLayout>
class RecyclerViewActivity : AppCompatActivity() {
lateinit var loadMoreItemsCells: ArrayList<String?>
lateinit var adapter: Items_RVAdapter
lateinit var scrollListener: RecyclerViewLoadMoreScroll
lateinit var mLayoutManager:RecyclerView.LayoutManager
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_recycler_view)
//** Get the data for our ArrayList
//** Set the adapter of the RecyclerView
adapter = Items_RVAdapter(itemsCells)
adapter.notifyDataSetChanged()
items_rv.adapter = adapter
//** Set the Layout Manager of the RecyclerView
setRVLayoutManager()
//** Set the scrollListerner of the RecyclerView
setRVScrollListener()
}
private fun LoadMoreData() {
//Add the Loading View
adapter.addLoadingView()
//Create the loadMoreItemsCells Arraylist
loadMoreItemsCells = ArrayList()
//Get the number of the current Items of the main Arraylist
val start = adapterLinear.itemCount
//Load 16 more items
val end = start + 16
//Use Handler if the items are loading too fast.
//If you remove it, the data will load so fast that you can't even see the LoadingView
Handler().postDelayed({
for (i in start..end) {
//Get data and add them to loadMoreItemsCells ArrayList
loadMoreItemsCells.add("Item $i")
}
//Remove the Loading View
adapterLinear.removeLoadingView()
//We adding the data to our main ArrayList
adapterLinear.addData(loadMoreItemsCells)
//Change the boolean isLoading to false
scrollListener.setLoaded()
//Update the recyclerView in the main thread
items_linear_rv.post {
adapterLinear.notifyDataSetChanged()
}
}, 3000)
}
}
class RecyclerViewLoadMoreScroll : RecyclerView.OnScrollListener {
private var visibleThreshold = 5
private lateinit var mOnLoadMoreListener: OnLoadMoreListener
private var isLoading: Boolean = false
private var lastVisibleItem: Int = 0
private var totalItemCount:Int = 0
private var mLayoutManager: RecyclerView.LayoutManager
fun setLoaded() {
isLoading = false
}
fun getLoaded(): Boolean {
return isLoading
}
fun setOnLoadMoreListener(mOnLoadMoreListener: OnLoadMoreListener) {
this.mOnLoadMoreListener = mOnLoadMoreListener
}
constructor(layoutManager:LinearLayoutManager) {
this.mLayoutManager = layoutManager
}
constructor(layoutManager:GridLayoutManager) {
this.mLayoutManager = layoutManager
visibleThreshold *= layoutManager.spanCount
}
constructor(layoutManager:StaggeredGridLayoutManager) {
this.mLayoutManager = layoutManager
visibleThreshold *= layoutManager.spanCount
}
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
super.onScrolled(recyclerView, dx, dy)
if (dy <= 0) return
totalItemCount = mLayoutManager.itemCount
if (mLayoutManager is StaggeredGridLayoutManager) {
val lastVisibleItemPositions =
(mLayoutManager as StaggeredGridLayoutManager).findLastVisibleItemPositions(null)
// get maximum element within the list
lastVisibleItem = getLastVisibleItem(lastVisibleItemPositions)
} else if (mLayoutManager is GridLayoutManager) {
lastVisibleItem = (mLayoutManager as GridLayoutManager).findLastVisibleItemPosition()
} else if (mLayoutManager is LinearLayoutManager) {
lastVisibleItem = (mLayoutManager as LinearLayoutManager).findLastVisibleItemPosition()
}
if (!isLoading && totalItemCount <= lastVisibleItem + visibleThreshold) {
mOnLoadMoreListener.onLoadMore()
isLoading = true
}
}
private fun getLastVisibleItem(lastVisibleItemPositions: IntArray): Int {
var maxSize = 0
for (i in lastVisibleItemPositions.indices) {
if (i == 0) {
maxSize = lastVisibleItemPositions[i]
} else if (lastVisibleItemPositions[i] > maxSize) {
maxSize = lastVisibleItemPositions[i]
}
}
return maxSize
}
}
private fun setRVLayoutManager() {
mLayoutManager = StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL)
items_rv.layoutManager = mLayoutManager
items_rv.setHasFixedSize(true)
items_rv.adapter = adapterStaggered
}
private fun setRVScrollListener() {
scrollListener = RecyclerViewLoadMoreScroll(mLayoutManager as StaggeredGridLayoutManager)
scrollListener.setOnLoadMoreListener(object :
OnLoadMoreListener {
override fun onLoadMore() {
LoadMoreData()
}
})
items_staggered_rv.addOnScrollListener(scrollListener)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.