Android RecyclerView Load More
I think RecyclerView is very common in android development. It is a useful component in android application.It is necessary to know how to implement loading more function with RecyclerView in android application.How is this implemented?
Typically, we load elements to the adapter from a data source.
In order to detect that the user has scrolled to the end of RecyclerView, we need to implement OnScrollListener()
.
In order to show the loading view at the bottom of RecyclerView.We could add a NULL element to the end of the data source.After adding a NULL element, we notify the adapter using notifyItemInserted()
method.So that the loading view will be showing. And Then to fetch the next set of elements.
Once the next set of elements is obtained, we remove the NULL element and add the next set of elements to the bottom of the data source.
Let’s code!
The code for the activity_main.xml is given below:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <androidx.recyclerview.widget.RecyclerView android:id="@+id/recyclerView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
The layout for the rows of RecyclerView is defined in item_row.xml
,its code is given below:
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="50dp"> <TextView android:id="@+id/tvItem" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Item X" />
</androidx.cardview.widget.CardView>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
The layout for loading view is given below. it is defined in item_loading.xml
:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="50dp" xmlns:app="http://schemas.android.com/apk/res-auto"> <ProgressBar android:id="@+id/progressBar" android:layout_width="wrap_content" android:layout_height="wrap_content" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
To use RecycleView in application, we need to define an adapter. It is given below:
package com.example.pulluprefresh
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.Adapter
class RecyclerVewAdapter constructor(var mItemList:MutableList<String?>) : Adapter<RecyclerView.ViewHolder>() { class ItemViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { val mTvItem: TextView = itemView.findViewById(R.id.tvItem) } class LoadingViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder = when (viewType) { VIEW_TYPE_ITEM -> { ItemViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.item_row, parent, false)) } else -> { LoadingViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.item_loading, parent, false)) } } override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) { if (holder is ItemViewHolder) { holder.mTvItem.text = mItemList[position] } } override fun getItemCount(): Int = mItemList.size override fun getItemViewType(position: Int): Int = if (mItemList[position] == null) VIEW_TYPE_LOADING else VIEW_TYPE_ITEM companion object { const val VIEW_TYPE_ITEM: Int = 0 const val VIEW_TYPE_LOADING: Int = 1 }
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
Note:getItemViewType()
is where we check each element of the data source. If the data is NULL, we set the view type as 1 else 0.
Based on the view type, we instantiate the ViewHolder in onCreateViewHolder()
. We could bind the data to the view according to view type in onBindViewHolder()
.
Here is MainActivity
code. We instantiate adapter inside MainActivity.Here we also register OnScrollListener
on RecyclerView, where we check if the user has scrolled the the end of the data source. If the bottom-most element is visible, we show the loading view and to fetch the set of data.
package com.example.pulluprefresh
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Handler
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
class MainActivity : AppCompatActivity() { lateinit var recyclerView: RecyclerView private var mItemList: MutableList<String?> = mutableListOf() private var isLoading = false override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) recyclerView = findViewById(R.id.recyclerView) mockData() initAdapter() initScrollListener() } private fun mockData() { for (i in 0 until 10) { mItemList.add("Item $i") } } private fun initAdapter() { recyclerView.adapter = RecyclerVewAdapter(mItemList) } private fun initScrollListener() { recyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() { override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) { super.onScrolled(recyclerView, dx, dy) val linearLayoutManager = recyclerView.layoutManager as LinearLayoutManager if(!isLoading && linearLayoutManager.findLastCompletelyVisibleItemPosition() == mItemList.size - 1 ){ loadMore() isLoading = true } } }) } private fun loadMore(){ mItemList.add(null) recyclerView.adapter?.notifyItemInserted(mItemList.size - 1) val handler = Handler() handler.postDelayed({ mItemList.removeAt(mItemList.size - 1) recyclerView.adapter?.notifyItemRemoved(mItemList.size) for(i in mItemList.size .. mItemList.size+10){ mItemList.add("Item $i") } recyclerView.adapter?.notifyDataSetChanged() isLoading = false },2000) }
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
文章来源: blog.csdn.net,作者:WongKyunban,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/weixin_40763897/article/details/115422705
- 点赞
- 收藏
- 关注作者
评论(0)