Skip to main content

How to implement pagination in a GridView in Kotlin Android

How to implement pagination in a GridView in Kotlin Android.

Here's a step-by-step tutorial on how to implement pagination in a GridView in Kotlin Android:

  1. Create a new Android project in Android Studio and add the necessary dependencies in your app's build.gradle file:
dependencies {
implementation 'androidx.recyclerview:recyclerview:1.2.0'
}
  1. In your layout XML file, add a GridView element:
<GridView
android:id="@+id/gridView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="2"
android:columnWidth="100dp"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:padding="10dp"
/>
  1. Create a new Kotlin class for your GridView adapter:
class GridAdapter(private val items: List<String>) : BaseAdapter() {

override fun getCount(): Int {
return items.size
}

override fun getItem(position: Int): Any {
return items[position]
}

override fun getItemId(position: Int): Long {
return position.toLong()
}

override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
val view: View
if (convertView == null) {
view = LayoutInflater.from(parent?.context).inflate(R.layout.grid_item, parent, false)
} else {
view = convertView
}

val itemTextView = view.findViewById<TextView>(R.id.itemTextView)
itemTextView.text = items[position]

return view
}
}
  1. Create a new layout XML file for the grid item (grid_item.xml):
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/itemTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
/>
  1. In your activity or fragment, initialize the GridView and set the adapter:
class MainActivity : AppCompatActivity() {

private lateinit var gridView: GridView
private lateinit var adapter: GridAdapter

private val items = mutableListOf<String>()
private var currentPage = 1
private val itemsPerPage = 10

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

gridView = findViewById(R.id.gridView)
adapter = GridAdapter(items)
gridView.adapter = adapter

loadItems(currentPage)

gridView.setOnScrollListener(object : AbsListView.OnScrollListener {
override fun onScrollStateChanged(view: AbsListView?, scrollState: Int) {}

override fun onScroll(view: AbsListView?, firstVisibleItem: Int, visibleItemCount: Int, totalItemCount: Int) {
if (firstVisibleItem + visibleItemCount == totalItemCount && totalItemCount != 0) {
if (currentPage * itemsPerPage < totalItemCount) {
currentPage++
loadItems(currentPage)
}
}
}
})
}

private fun loadItems(page: Int) {
// Simulate loading items from a data source
val start = (page - 1) * itemsPerPage + 1
val end = page * itemsPerPage
for (i in start..end) {
items.add("Item $i")
}
adapter.notifyDataSetChanged()
}
}
  1. Run your app and you should see the GridView with the initial set of items. As you scroll to the bottom, more items will be loaded and added to the GridView.

That's it! You have successfully implemented pagination in a GridView in Kotlin Android.