Merhaba bu yazımızda RecyclerView nasıl kullanılır bunu anlatacağım.
Bu şekilde ir liste hazırlayacağız şimdi boş bir proje açalım.(Android proje açma)
öncelikle projemize bir DataModel ekleyelim ben bilgiler adında bir data model ekledim
bilgiler
public class bilgiler {
private int resim; // integer olarak ekledim ben bu projede drawable klasörünün içindeki bir resmi kullanacağım.
private String isim;
private String yas;
public bilgiler(int resim, String isim, String yas) {
this.resim = resim;
this.isim = isim;
this.yas = yas;
}
public int getResim() {
return resim;
}
public String getIsim() {
return isim;
}
public String getYas() {
return yas;
}
}
şimdi Recyclerviewde görünecek satırın tasarımını yapalım bu tasarımı layout klasörünün altına recyle_item olarak açtım.
<androidx.cardview.widget.CardView 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="wrap_content"
android:layout_marginHorizontal="10dp"
android:layout_marginTop="10dp"
android:padding="3dp"
app:cardElevation="8dp"
app:cardCornerRadius="10dp">
<!--cardElevation bununla gölge veriyoruz-->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/image"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginStart="5dp"
android:layout_marginTop="25dp"
android:layout_marginEnd="5dp"
android:layout_marginBottom="25dp"
android:importantForAccessibility="no"
android:scaleType="fitXY"
android:src="@android:drawable/ic_menu_gallery" />
<TextView
android:id="@+id/isim"
android:layout_width="225dp"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="10dp"
android:layout_toEndOf="@+id/image"
android:text="Yazi"
android:textSize="15dp"
android:textStyle="bold" />
<TextView
android:id="@+id/yas"
android:layout_width="70dp"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="10dp"
android:layout_toEndOf="@+id/isim"
android:gravity="center"
android:text="Yaş 35"
android:textSize="15dp"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/yas"
android:layout_marginStart="10dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="10dp"
android:layout_marginBottom="10dp"
android:layout_toEndOf="@+id/image"
android:text="Burası Da Açıklama\nAlt Sarıra inip yazı yazabiliyoruz\nbir alt satıra daha indik" />
</RelativeLayout>
<!--layout_below bu eiketi kullandığımız nesne hangi nesneden sonra gelsin onu belirtiyoruz-->
</androidx.cardview.widget.CardView>
şimdi MainActivity tasarım ekranı için activity layoutunu açıp aşağıdaki kodları yazalım.
<RelativeLayout
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"
android:background="@color/background"
tools:context=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recylerliste"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:listitem="@layout/recyler_item" />
</RelativeLayout>
RecylerView dataları listeleyebilmemiz için bir adapter oluşturmamız gerekiyor.
recyclerViewAdapter adında bir adapter oluşturuyorum.
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.harundemir.egitim.databinding.RecylerItemBinding;
import java.util.ArrayList;
public class recyclerViewAdapter extends RecyclerView.Adapter<recyclerViewAdapter.ViewHolder> { // javadakş RecyclerView adapter metodunu kullanmak için buraya extends edip byrda kendimize göre düzenleyeceğiz RecyclerView adapter extent ettiğimizde bizden view holder istiyor
public class ViewHolder extends RecyclerView.ViewHolder { // burada bizden istenen biewholder classını tanımlıyoruz ve bunada RecyclerView ViewHolder extends ediyoruz
private RecylerItemBinding recylerItemBinding; // Main aktivity olduğu gibi burada da binding tanımlıyoruz.
public ViewHolder(@NonNull RecylerItemBinding binding) { // Constractor metodumuzu yazıyoruz yada ctrl+insert tuşuna bastığımızda contractor çift tıklamamız yeterli
super(binding.getRoot());
this.recylerItemBinding = binding;
}
}
ArrayList<bilgiler> bilgilers; // Listedeki dataları buraya aktaracağız constractor bunu isteyeceğiz bu bize lazım olacak
public recyclerViewAdapter(ArrayList<bilgiler> bilgilers) { // constructor metodu ike listeyi alacağız
this.bilgilers = bilgilers;
} // recyclerViewAdapter contractor oluşturuyoruz sistem yine aynı ister elimizle isteniz ctrl+insert tuşuna bastığımızda contractor çift tıklamamız yeterli
//Başlarda demiştim RecyclerView.Adapter extends ediyoruz bunu kendimize göre düzenleyeceğiz bunun için bizim 3 adet mothodu düzenlememiz lazım
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { //burda görünümü bağlıyoruz
RecylerItemBinding recylerItemBinding = RecylerItemBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
return new ViewHolder(recylerItemBinding);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) { //burada arralistteki dataları alıyoruz ve gorunumdeki textview ve image setliyoruz position pozisyonu belirtiyor yani arraylistteki x sıradaki ismi isimtextviewvine yaz gibi
String uyeYasi = "Yaş " + bilgilers.get(position).getYas();
holder.recylerItemBinding.isim.setText(bilgilers.get(position).getIsim());
holder.recylerItemBinding.yas.setText(uyeYasi);
holder.recylerItemBinding.image.setImageResource(bilgilers.get(position).getResim());
_Events(holder); // bunu kişiye bağlı bir method bu methodda ben eventleri yakadım yani butona tıkladığımda ne olacak kodlar birbirine girmesin kafa karışmasın diye method olarak yazdım aşağıda bunu oluşturacağım.
}
@Override
public int getItemCount() { // bu metod ile listede kaçtane satır var onu alaacğız
return bilgilers.size(); //size ile aldık
}
private void _Events(@NonNull ViewHolder holder) { // buda kendi yazdığım method
holder.recylerItemBinding.isim.setOnClickListener(new View.OnClickListener() { // burada isime tıklandığında bir toast açması için Textview click yakaladık
@Override
public void onClick(View view) {
Toast.makeText(holder.itemView.getContext(), holder.recylerItemBinding.isim.getText().toString(), Toast.LENGTH_SHORT).show();
}
});
holder.recylerItemBinding.image.setOnClickListener(new View.OnClickListener() { // burada image click
@Override
public void onClick(View view) {
Toast.makeText(holder.itemView.getContext(), "Resime Tıklandı", Toast.LENGTH_SHORT).show();
}
});
holder.recylerItemBinding.yas.setOnClickListener(new View.OnClickListener() { // burada yaş
@Override
public void onClick(View view) {
Toast.makeText(view.getContext(), "Yaşa Tıklandı", Toast.LENGTH_SHORT).show();
}
});
holder.itemView.setOnClickListener(new View.OnClickListener() { // burası farklı burda holder.itemview kullandık burada ise fotoğrafa ya da isme değilde listedeki satıra tıklanırsa ne olacak olnu belirtiyoruz.
@Override
public void onClick(View view) {
Toast.makeText(view.getContext(), "Liste iteme Tıklandı", Toast.LENGTH_SHORT).show();
}
});
}
ve son olarak MainActivity kodlarımızı yazıyoruz.
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.os.Bundle;
import android.view.View;
import com.harundemir.egitim.databinding.ActivityMainBinding;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
ArrayList<bilgiler> arrayList; // Bilgiler tipinde Arraylist oluşturuyoruz
private ActivityMainBinding binding; //Layoutaki elemanlara daha kolay erişmek için binding tanımlıyoruz
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityMainBinding.inflate(getLayoutInflater());
View view = binding.getRoot();
setContentView(view);
arrayList = new ArrayList<>(); // yeni bir Array list oluşturuyoruz
/* Burada Array listemize değerleri ekliyoruz burada iki şekilde array liste data ekleyi gösterdim ikincisi bana daha basit geldiği için onu kullandım
yaklaşık 100 adet kayıt var */
bilgiler kisi = new bilgiler(R.drawable.ic_launcher_foreground, "Harun", "31");
arrayList.add(kisi);
arrayList.add(new bilgiler(R.drawable.ic_launcher_foreground, "JALE", "41"));
arrayList.add(new bilgiler(R.drawable.ic_launcher_foreground, "ALİ", "50"));
arrayList.add(new bilgiler(R.drawable.ic_launcher_foreground, "MAHMUT", "23"));
arrayList.add(new bilgiler(R.drawable.ic_launcher_foreground, "MANSUR KÜRŞAD", "94"));
arrayList.add(new bilgiler(R.drawable.ic_launcher_foreground, "GAMZE", "28"));
arrayList.add(new bilgiler(R.drawable.ic_launcher_foreground, "MİRAÇ", "73"));
arrayList.add(new bilgiler(R.drawable.ic_launcher_foreground, "YÜCEL", "83"));
arrayList.add(new bilgiler(R.drawable.ic_launcher_foreground, "KUBİLAY", "96"));
arrayList.add(new bilgiler(R.drawable.ic_launcher_foreground, "HAYATİ", "26"));
arrayList.add(new bilgiler(R.drawable.ic_launcher_foreground, "BEDRİYE MÜGE", "88"));
arrayList.add(new bilgiler(R.drawable.ic_launcher_foreground, "BİRSEN", "54"));
arrayList.add(new bilgiler(R.drawable.ic_launcher_foreground, "SERDAL", "55"));
arrayList.add(new bilgiler(R.drawable.ic_launcher_foreground, "BÜNYAMİN", "91"));
arrayList.add(new bilgiler(R.drawable.ic_launcher_foreground, "ÖZGÜR", "81"));
arrayList.add(new bilgiler(R.drawable.ic_launcher_foreground, "FERDİ", "93"));
arrayList.add(new bilgiler(R.drawable.ic_launcher_foreground, "REYHAN", "68"));
arrayList.add(new bilgiler(R.drawable.ic_launcher_foreground, "İLHAN", "27"));
arrayList.add(new bilgiler(R.drawable.ic_launcher_foreground, "GÜLŞAH", "90"));
arrayList.add(new bilgiler(R.drawable.ic_launcher_foreground, "NALAN", "68"));
arrayList.add(new bilgiler(R.drawable.ic_launcher_foreground, "SEMİH", "38"));
arrayList.add(new bilgiler(R.drawable.ic_launcher_foreground, "ERGÜN", "93"));
arrayList.add(new bilgiler(R.drawable.ic_launcher_foreground, "FATİH", "26"));
arrayList.add(new bilgiler(R.drawable.ic_launcher_foreground, "ŞENAY", "39"));
arrayList.add(new bilgiler(R.drawable.ic_launcher_foreground, "EMRE", "80"));
arrayList.add(new bilgiler(R.drawable.ic_launcher_foreground, "BAHATTİN", "38"));
arrayList.add(new bilgiler(R.drawable.ic_launcher_foreground, "IRAZCA", "55"));
arrayList.add(new bilgiler(R.drawable.ic_launcher_foreground, "BARIŞ", "17"));
arrayList.add(new bilgiler(R.drawable.ic_launcher_foreground, "REZAN", "83"));
arrayList.add(new bilgiler(R.drawable.ic_launcher_foreground, "FUAT", "89"));
arrayList.add(new bilgiler(R.drawable.ic_launcher_foreground, "GÖKHAN", "31"));
arrayList.add(new bilgiler(R.drawable.ic_launcher_foreground, "ORHAN", "72"));
arrayList.add(new bilgiler(R.drawable.ic_launcher_foreground, "EVREN", "84"));
arrayList.add(new bilgiler(R.drawable.ic_launcher_foreground, "OKTAY", "78"));
arrayList.add(new bilgiler(R.drawable.ic_launcher_foreground, "HARUN", "30"));
arrayList.add(new bilgiler(R.drawable.ic_launcher_foreground, "YAVUZ", "80"));
arrayList.add(new bilgiler(R.drawable.ic_launcher_foreground, "PINAR", "95"));
arrayList.add(new bilgiler(R.drawable.ic_launcher_foreground, "UMUT", "39"));
arrayList.add(new bilgiler(R.drawable.ic_launcher_foreground, "MESUDE", "37"));
arrayList.add(new bilgiler(R.drawable.ic_launcher_foreground, "HÜSEYİN CAHİT", "22"));
arrayList.add(new bilgiler(R.drawable.ic_launcher_foreground, "HAŞİM ONUR", "75"));
arrayList.add(new bilgiler(R.drawable.ic_launcher_foreground, "EYYUP SABRİ", "82"));
arrayList.add(new bilgiler(R.drawable.ic_launcher_foreground, "AHMET ALİ", "63"));
arrayList.add(new bilgiler(R.drawable.ic_launcher_foreground, "MEDİHA", "69"));
arrayList.add(new bilgiler(R.drawable.ic_launcher_foreground, "KAMİL", "43"));
arrayList.add(new bilgiler(R.drawable.ic_launcher_foreground, "NEBİ", "24"));
arrayList.add(new bilgiler(R.drawable.ic_launcher_foreground, "ÖZCAN", "44"));
arrayList.add(new bilgiler(R.drawable.ic_launcher_foreground, "NAGİHAN", "20"));
arrayList.add(new bilgiler(R.drawable.ic_launcher_foreground, "CEREN", "86"));
arrayList.add(new bilgiler(R.drawable.ic_launcher_foreground, "SERKAN", "83"));
arrayList.add(new bilgiler(R.drawable.ic_launcher_foreground, "HASAN", "37"));
arrayList.add(new bilgiler(R.drawable.ic_launcher_foreground, "YUSUF KENAN", "19"));
arrayList.add(new bilgiler(R.drawable.ic_launcher_foreground, "ÇETİN", "61"));
arrayList.add(new bilgiler(R.drawable.ic_launcher_foreground, "TARKAN", "51"));
arrayList.add(new bilgiler(R.drawable.ic_launcher_foreground, "MERAL LEMAN", "23"));
arrayList.add(new bilgiler(R.drawable.ic_launcher_foreground, "KENAN AHMET", "75"));
arrayList.add(new bilgiler(R.drawable.ic_launcher_foreground, "URAL", "29"));
arrayList.add(new bilgiler(R.drawable.ic_launcher_foreground, "YAHYA", "25"));
arrayList.add(new bilgiler(R.drawable.ic_launcher_foreground, "BENGÜ", "94"));
arrayList.add(new bilgiler(R.drawable.ic_launcher_foreground, "FATİH NAZMİ", "64"));
arrayList.add(new bilgiler(R.drawable.ic_launcher_foreground, "DİLEK", "49"));
arrayList.add(new bilgiler(R.drawable.ic_launcher_foreground, "TUFAN AKIN", "53"));
arrayList.add(new bilgiler(R.drawable.ic_launcher_foreground, "MEHMET", "92"));
arrayList.add(new bilgiler(R.drawable.ic_launcher_foreground, "TURGAY YILMAZ", "30"));
arrayList.add(new bilgiler(R.drawable.ic_launcher_foreground, "GÜLDEHEN", "41"));
arrayList.add(new bilgiler(R.drawable.ic_launcher_foreground, "GÖKMEN", "27"));
arrayList.add(new bilgiler(R.drawable.ic_launcher_foreground, "BÜLENT", "47"));
arrayList.add(new bilgiler(R.drawable.ic_launcher_foreground, "EROL", "26"));
arrayList.add(new bilgiler(R.drawable.ic_launcher_foreground, "BAHRİ", "37"));
arrayList.add(new bilgiler(R.drawable.ic_launcher_foreground, "ÖZEN ÖZLEM", "60"));
arrayList.add(new bilgiler(R.drawable.ic_launcher_foreground, "SELMA", "45"));
arrayList.add(new bilgiler(R.drawable.ic_launcher_foreground, "TUĞSEM", "47"));
arrayList.add(new bilgiler(R.drawable.ic_launcher_foreground, "TESLİME NAZLI", "23"));
arrayList.add(new bilgiler(R.drawable.ic_launcher_foreground, "GÜLÇİN", "31"));
arrayList.add(new bilgiler(R.drawable.ic_launcher_foreground, "İSMAİL", "29"));
arrayList.add(new bilgiler(R.drawable.ic_launcher_foreground, "MURAT", "64"));
arrayList.add(new bilgiler(R.drawable.ic_launcher_foreground, "EBRU", "24"));
arrayList.add(new bilgiler(R.drawable.ic_launcher_foreground, "TÜMAY", "25"));
arrayList.add(new bilgiler(R.drawable.ic_launcher_foreground, "AHMET", "64"));
arrayList.add(new bilgiler(R.drawable.ic_launcher_foreground, "HÜSEYİN YAVUZ", "50"));
arrayList.add(new bilgiler(R.drawable.ic_launcher_foreground, "AYŞEGÜL", "48"));
arrayList.add(new bilgiler(R.drawable.ic_launcher_foreground, "EVRİM", "43"));
arrayList.add(new bilgiler(R.drawable.ic_launcher_foreground, "YASER", "72"));
arrayList.add(new bilgiler(R.drawable.ic_launcher_foreground, "ÜLKÜ", "23"));
arrayList.add(new bilgiler(R.drawable.ic_launcher_foreground, "ÖZHAN", "38"));
arrayList.add(new bilgiler(R.drawable.ic_launcher_foreground, "UFUK", "59"));
arrayList.add(new bilgiler(R.drawable.ic_launcher_foreground, "AKSEL", "44"));
arrayList.add(new bilgiler(R.drawable.ic_launcher_foreground, "FULYA", "48"));
arrayList.add(new bilgiler(R.drawable.ic_launcher_foreground, "MESUT", "32"));
arrayList.add(new bilgiler(R.drawable.ic_launcher_foreground, "MEHMET HİLMİ", "57"));
arrayList.add(new bilgiler(R.drawable.ic_launcher_foreground, "ASUDAN TUĞÇE", "89"));
arrayList.add(new bilgiler(R.drawable.ic_launcher_foreground, "AHMET GÖKHAN", "30"));
arrayList.add(new bilgiler(R.drawable.ic_launcher_foreground, "BAŞAK", "35"));
arrayList.add(new bilgiler(R.drawable.ic_launcher_foreground, "CEYHAN", "84"));
arrayList.add(new bilgiler(R.drawable.ic_launcher_foreground, "ÜMİT", "95"));
arrayList.add(new bilgiler(R.drawable.ic_launcher_foreground, "SENEM", "48"));
arrayList.add(new bilgiler(R.drawable.ic_launcher_foreground, "DENİZ", "26"));
arrayList.add(new bilgiler(R.drawable.ic_launcher_foreground, "MÜNEVER", "27"));
arrayList.add(new bilgiler(R.drawable.ic_launcher_foreground, "HATİCE", "44"));
arrayList.add(new bilgiler(R.drawable.ic_launcher_foreground, "ÖZLEM", "51"));
arrayList.add(new bilgiler(R.drawable.ic_launcher_foreground, "ALİ SEÇKİN", "23"));
binding.recylerliste.setLayoutManager(new LinearLayoutManager(this));
recyclerViewAdapter adapter = new recyclerViewAdapter(arrayList); // burada recycler ile arraylist dataları birbirine bağlıyoruz
binding.recylerliste.setAdapter(adapter);
}
}