Android RecyclerView Kullanımı (Kotlin)

Zengin nesnelerden biri olan RecyclerView örneğini Kotlin üzerinden yazıyoruz.
CountryModel.kt adlı bir veri sınıfı üretelim;
data class CountryModel(val flagImage: Int, val countryName: String, val capitalName: String)
activity_main.xml layout dosyamıza RecyclerView nesnesi ekleyelim.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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/rvListe"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
row_view.xml adlı bir layout dosyası üretelim.
<?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="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:orientation="horizontal">
<ImageView
android:id="@+id/flagImage"
android:layout_marginLeft="20dp"
android:layout_width="100dp"
android:layout_height="70dp"/>
<LinearLayout
android:layout_gravity="center_vertical"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:textSize="15sp"
android:textStyle="bold"
android:textAllCaps="true"
android:text="countryname"
android:id="@+id/countryName"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"/>
<TextView
android:textAllCaps="true"
android:textStyle="bold"
android:textSize="15sp"
android:text="capitalname"
android:layout_margin="10dp"
android:id="@+id/capitalName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
Her satırda Resim, ülke ve şehir adı göstermek istediğimiz için flagImage adlı ImageView, countryName ve capitalName adlarında TextView nesneleri üretelim.
Adapter.kt adlı bir sınıf üretelim. Bu sınıf RecyclerView nesnesi içerisinde verilerin listelenmesini sağlayan adapter içerecektir.
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
class Adapter(val countryList: MutableList<CountryModel>) : RecyclerView.Adapter<Adapter.ModelViewHolder>() {
class ModelViewHolder(view: View) : RecyclerView.ViewHolder(view) {
val countryName: TextView = view.findViewById(R.id.countryName)
val capitalName: TextView = view.findViewById(R.id.capitalName)
val flagImage: ImageView = view.findViewById(R.id.flagImage)
fun bindItems(item: CountryModel) {
countryName.setText(item.countryName)
capitalName.setText(item.capitalName)
flagImage.setImageResource(item.flagImage)
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ModelViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.row_view, parent, false)
return ModelViewHolder(view)
}
override fun getItemCount(): Int {
return countryList.size
}
override fun onBindViewHolder(holder: ModelViewHolder, position: Int) {
holder.bindItems(countryList.get(position))
}
}
ModelViewHolder sınıfı View’ın içindeki nesnelerin tanımlanması ve içerisindeki bindItems adlı metot sayesinde ise componentlerin değerlerinin set edilmesi görevini üstleniyor.
onCreateViewHolder hangi layout türünde satırı döndürebileceğimizi belirtiyor.
getItemCount metodu listede bulunan eleman sayısını dönüyor, böylece liste kaç adet veri getireceği anlaşılıyor.
onBindViewHolder ise doldurma görevini üstleniyor. Satırda gözükecek nesnelerin içeriklerinin hangi değerler ile dolacağını burada belirtiyoruz.
MainActivity.kt dosyamıza RecyclerView ve listemizi bağlıyoruz.
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
class MainActivity : AppCompatActivity() {
lateinit var recyclerView : RecyclerView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
recyclerView = findViewById(R.id.rvListe)
recyclerView.layoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
recyclerView.adapter = Adapter(getModels())
}
fun getModels(): MutableList<CountryModel> {
val models = mutableListOf(
CountryModel(R.drawable.turkiye, "Türkiye", "Ankara"),
CountryModel(R.drawable.kktc,"K.K.T.C", "Lefkoşa"),
CountryModel(R.drawable.almanya, "Almanya", "Berlin"),
CountryModel(R.drawable.amerika, "Amerika", "Washington"),
)
return models
}
}
RecyclerView nesnesinin birçok görüntü gösterim şekli bulunuyor, bunlardan;
recyclerView.layoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
satır görüntüsünün liste formatında olmasını sağlıyor. Dilerseniz layout sınıfını değiştirerek Grid veya StaggeredGrid görüntüleri formatında da kullanabilirsiniz.
Keyifli çalışmalar dilerim.
Son Yorumlar