Android Custom ListView Kullanımı

Selamlar, sizlere bu makalemde Özelleştirilmiş ListView nasıl oluşturulur, aşama aşama anlatacağım. Yararlı olması dileklerimle.

Model üretiniz; (PhoneModel.java)

public class PhoneModel {
private int indexId;
private String nameSurname;
private String phoneNumber;
private String photo;

public PhoneModel() {
}

public PhoneModel(int indexId, String nameSurname, String phoneNumber, String photo) {
this.indexId = indexId;
this.nameSurname = nameSurname;
this.phoneNumber = phoneNumber;
this.photo = photo;
}

public int getIndexId() {
return indexId;
}

public void setIndexId(int indexId) {
this.indexId = indexId;
}

public String getNameSurname() {
return nameSurname;
}

public void setNameSurname(String nameSurname) {
this.nameSurname = nameSurname;
}

public String getPhoneNumber() {
return phoneNumber;
}

public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}

public String getPhoto() {
return photo;
}

public void setPhoto(String photo) {
this.photo = photo;
}
}

MainActivity xml layout'u içerisine ListView nesnesi ekleyiniz.

<?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=".MainActivity">

<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true" />
</RelativeLayout>

Projenizin build.gradle(Module: app) içerisinde depentencies içerisine ekleyiniz;

compile 'com.github.bumptech.glide:glide:3.5.2'

AndroidManifest.xml içerisine internet bağlantısı iznimizi ekleyelim, Resimleri Glide kütüphanesi web'den çekecek.

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

listview_custom_row.xml Layout'u ekleyiniz (Her listview satırı bu görüntüde olacak)

<?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"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<ImageView
android:id="@+id/ivPhoto"
android:layout_width="120dp"
android:layout_height="120dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
app:srcCompat="@drawable/ic_launcher_background" />

<TextView
android:id="@+id/tvNameSurname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginStart="17dp"
android:layout_marginTop="21dp"
android:layout_toEndOf="@+id/ivPhoto"
android:text="TextView"
android:textSize="18sp"
android:textStyle="bold" />

<TextView
android:id="@+id/tvPhone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="@+id/tvNameSurname"
android:layout_below="@+id/tvNameSurname"
android:layout_marginTop="20dp"
android:text="TextView" />
</RelativeLayout>

ListViewAdapter classı oluşturunuz;

public class ListViewAdapter extends BaseAdapter {

private Context context;
private LayoutInflater layoutInflater;
private ArrayList<PhoneModel> arrayList;

public ListViewAdapter(Context context,ArrayList<PhoneModel> arrayList){
this.context = context;
this.arrayList = arrayList;
this.layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getCount() {
return arrayList.size();
}

@Override
public Object getItem(int i) {
return arrayList.get(i);
}

@Override
public long getItemId(int i) {
return arrayList.get(i).getIndexId();
}

@Override
public View getView(int i, View view, ViewGroup viewGroup) {
View v = layoutInflater.inflate(R.layout.listview_custom_row,null);

TextView tvNameSurname = (TextView)v.findViewById(R.id.tvNameSurname);
TextView tvPhone = (TextView)v.findViewById(R.id.tvPhone);
ImageView ivPhoto = (ImageView)v.findViewById(R.id.ivPhoto);

tvNameSurname.setText(arrayList.get(i).getNameSurname());
tvPhone.setText(arrayList.get(i).getPhoneNumber());
Glide.with(context).load(arrayList.get(i).getPhoto()).into(ivPhoto);

return v;
}
}

MainActivity sınıfı içerisine;

public class MainActivity extends AppCompatActivity {

ListView listView;
ArrayList<PhoneModel> arrayList = new ArrayList<>();

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

arrayList.add( new PhoneModel(1,"Şerif","0212 000 11 22","https://scontent-sof1-1.cdninstagram.com/vp/e44621ac6e0fa20dde7652d2431ad0ed/5B58D62F/t51.2885-19/s150x150/29738612_164502504253972_2938372867349282816_n.jpg") );
arrayList.add( new PhoneModel(1,"Ben","0212 000 11 33","https://scontent-sof1-1.cdninstagram.com/vp/24eadfbcaceaa9eb394100e11a983923/5B5EE183/t51.2885-15/e35/29417489_358577954660719_7491071830703210496_n.jpg") );
arrayList.add( new PhoneModel(1,"Sen","0212 000 11 44","https://scontent-sof1-1.cdninstagram.com/vp/eb8dbd9e324d721434d1b64caf168396/5B5DD012/t51.2885-15/e35/28752551_151094132251408_8689739882383278080_n.jpg") );
arrayList.add( new PhoneModel(1,"O","0212 000 11 55","https://scontent-sof1-1.cdninstagram.com/vp/60a289ae42c3bd361803643a1bc93535/5B691A75/t51.2885-15/e35/29404300_159380121418069_42480559452061696_n.jpg") );
arrayList.add( new PhoneModel(1,"Biz","0212 000 11 66","https://scontent-sof1-1.cdninstagram.com/vp/854b53c561e3494e3d4e052e519e8a90/5B5E29A3/t51.2885-15/e35/28763608_152561305415651_6929213314371158016_n.jpg") );
arrayList.add( new PhoneModel(1,"Siz","0212 000 11 77","https://scontent-sof1-1.cdninstagram.com/vp/087d15b9cfd55ddd70da317f309e0e88/5B74A361/t51.2885-15/e35/29089015_174676626667408_7073313903853174784_n.jpg") );
arrayList.add( new PhoneModel(1,"Onlar","0212 000 11 88","https://scontent-sof1-1.cdninstagram.com/vp/60a289ae42c3bd361803643a1bc93535/5B691A75/t51.2885-15/e35/29404300_159380121418069_42480559452061696_n.jpg") );

listView = (ListView)findViewById(R.id.listView);
ListViewAdapter adapter = new ListViewAdapter(getApplicationContext(),arrayList);
listView.setAdapter(adapter);
}
}
Bu makaleyi paylaşın:
İstanbul'da yaşıyorum.Web üzerinde Frontend/Backend Geliştirmenin yanı sıra, Android Geliştirme, PHP/ASP.NET, C# Yazılım Uzmanlığı ve Java konuları işliyorum. Kod Şiirdir, onları seviyorum.

Bir yorum bırakın