Android RecyclerView Adapter ve CardView Kullanımı

Android RecyclerView Adapter ve CardView Kullanımı

Android RecyclerView Adapter ve CardView Kullanımı

RecyclerView Adapter ile CardView kullanımı

Android RecyclerView Adapter ve CardView Kullanımı

1. Adım

Projenizin Gradle Scripts kısmından, build.gradle (Module: app) dosyasını açınız. Sırasıyla alttakileri ekleyiniz.

  • implementation 'com.android.support:recyclerview-v7:26.1.0'
  • implementation 'com.android.support:cardview-v7:26.1.0'
  • implementation 'com.squareup.picasso:picasso:2.5.2'

2. Adım

activity_main.xml içerisine ekleyin;

<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context="com.serifgungor.cardviewexample.MainActivity">

<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical" />

</RelativeLayout>

3. Adım

Model Üretiniz (Uyeler.java)

/**
* Created by Serif for www.serifgungor.com
*/

public class Uyeler {
private String ad;
private String email;
private String profilResim;

public Uyeler() {
}

public Uyeler(String ad, String email, String profilResim) {
this.ad = ad;
this.email = email;
this.profilResim = profilResim;
}

public String getAd() {
return ad;
}

public void setAd(String ad) {
this.ad = ad;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

public String getProfilResim() {
return profilResim;
}

public void setProfilResim(String profilResim) {
this.profilResim = profilResim;
}
}

4. Adım

item_layout.xml isminde Layout üretiniz

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
card_view:cardCornerRadius="4dp"
android:layout_marginBottom="10dp"
>

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:paddingTop="5dp"
android:paddingBottom="5dp">

<ImageView
android:id="@+id/profilResim"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginRight="10dp"
/>

<TextView
android:id="@+id/txtAd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/profilResim"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
android:id="@+id/txtEmail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/txtAd"
android:layout_toRightOf="@+id/profilResim"
android:text="Small Text"
android:textAppearance="?android:attr/textAppearanceSmall" />

</RelativeLayout>

</android.support.v7.widget.CardView>

5. Adım

ItemHolder.java isminde Class oluşturunuz.

/**
* Created by Serif for www.serifgungor.com
*/

public class ItemHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

public TextView txtAd, txtEmail;
public ImageView profilResim;

@Override
public void onClick(View v) {
Toast.makeText(v.getContext(),"LOG "+getAdapterPosition(),Toast.LENGTH_LONG).show();
}

public ItemHolder(final View view) {
super(view);
txtAd = (TextView) view.findViewById(R.id.txtAd);
txtEmail = (TextView) view.findViewById(R.id.txtEmail);
profilResim = (ImageView) view.findViewById(R.id.profilResim);
view.setOnClickListener(this);
}
}

6. Adım

ItemAdapter.java isminde Class oluşturunuz.

/**
* Created by Serif for www.serifgungor.com
*/

public class ItemAdapter extends RecyclerView.Adapter<ItemHolder> {

private List<Uyeler> uyelerList;

public ItemAdapter(List<Uyeler> uyelerList) {
this.uyelerList = uyelerList;
}

@Override
public ItemHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_layout, parent, false);

return new ItemHolder(itemView);
}

@Override
public void onBindViewHolder(ItemHolder holder, int position) {
Uyeler uye = uyelerList.get(position);
holder.txtAd.setText(uye.getAd());
holder.txtEmail.setText(uye.getEmail());
Picasso.with(holder.txtAd.getContext()).load(uye.getProfilResim()).into(holder.profilResim);
}

@Override
public int getItemCount() {
return uyelerList.size();
}
}

onCreateViewHolder methodu sayesinde Adapter'den bize dönen her Item'ı R.layout.item_layout görüntüsünde üretiyoruz.

7. Adım

public class MainActivity extends AppCompatActivity {

private RecyclerView recyclerView;
private ItemAdapter itemAdapter;
private ArrayList<Uyeler> uyeList;
RecyclerView.LayoutManager mLayoutManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

uyeList = new ArrayList<>();
listeyiDoldur(uyeList);

recyclerView = (RecyclerView)findViewById(R.id.recycler_view);
itemAdapter = new ItemAdapter(uyeList);
mLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(itemAdapter);
}

private void listeyiDoldur(ArrayList<Uyeler> uyeList){
uyeList.add(new Uyeler("Şerif GÜNGÖR","contact@serifgungor.com","http://serifgungor.com/images/my-photo.jpg"));
uyeList.add(new Uyeler("Şerif GÜNGÖR","contact@serifgungor.com","http://serifgungor.com/images/my-photo.jpg"));
uyeList.add(new Uyeler("Şerif GÜNGÖR","contact@serifgungor.com","http://serifgungor.com/images/my-photo.jpg"));
}
}

8. Adım

AndroidManifest.xml'de internet iznini tanımlayınız.

<uses-permission android:name="android.permission.INTERNET" />

CardView & RecyclerView kullanımına ilişkin Android Developer kaynağı; https://developer.android.com/training/material/lists-cards.html


İlgili kelimeler;
android studio cardview adapter recyclerview model object
İlgili kategori;
Android Studio Dersleri
Makaleyi paylaşın; Google+FacebookTwitterTumblrPinterestLinkedIn

Benzer makaleler mi eklemeliyim ? Bana LinkedIn ve Github adreslerimden üzerinden ulaşabilirsiniz.