Skip to content

Instantly share code, notes, and snippets.

@ekirastogi
Last active October 19, 2015 19:51
Show Gist options
  • Save ekirastogi/af74a1a9e779e1971838 to your computer and use it in GitHub Desktop.
Save ekirastogi/af74a1a9e779e1971838 to your computer and use it in GitHub Desktop.
Android : Custom List View
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<TextView
android:id="@+id/title_person_list"
android:text="Person List"
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center"
android:textSize="22dp"/>
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="@android:color/transparent"
android:dividerHeight="5dp"
android:layout_below="@+id/title_person_list"
android:id="@+id/person_list"
/>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/buzz_item_container"
android:layout_height="94dp"
android:layout_width="match_parent"
android:weightSum="1"
android:padding="5dp"
android:background="#f7f7f7"
android:layout_margin="2dp"
android:elevation="3dp"
android:orientation="horizontal"
android:divider="@android:color/holo_blue_bright"
xmlns:android="http://schemas.android.com/apk/res/android" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal"
android:padding="2dp"
>
<ImageView
android:layout_width="160dp"
android:layout_height="90dp"
android:background="@drawable/user"
/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:padding="2dp">
<TextView
android:id="@+id/text_view_name"
android:layout_height="20dp"
android:textColor="@android:color/holo_blue_dark"
android:paddingLeft="15dp"
android:textStyle="bold"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:gravity="center_vertical"
android:layout_width="match_parent"
android:background="#e7e7e1"
xmlns:android="http://schemas.android.com/apk/res/android" />
<TextView
android:id="@+id/text_view_email"
android:layout_height="20dp"
android:textAlignment="gravity"
android:textColor="@android:color/holo_blue_dark"
android:paddingLeft="15dp"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:gravity="center_vertical"
android:layout_width="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android" />
<TextView
android:id="@+id/text_view_desc"
android:layout_height="20dp"
android:textAlignment="gravity"
android:textColor="@android:color/holo_blue_dark"
android:paddingLeft="15dp"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:gravity="center_vertical"
android:layout_width="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android" />
</LinearLayout>
</LinearLayout>
package com.ekiras.demo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import com.ekiras.demo.adapter.PersonAdapter;
import com.ekiras.demo.model.Person;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private ListView listView;
private PersonAdapter personAdapter;
private int bootCounter=1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
PersonAdapter personAdapter = new PersonAdapter(this,bootData(bootCounter));
listView = (ListView) findViewById(R.id.person_list);
listView.setAdapter(personAdapter);
}
private List<Person> bootData(int start){
List<Person> persons = new ArrayList<Person>();
for(int i=start;i<start+20;i++){
Person person = new Person();
person.setName("person-" + i);
person.setDesc("description :" + i);
person.setEmail("person" + i + "@ekiras.com");
person.setImage(R.drawable.user);
persons.add(person);
}
return persons;
}
}
package com.ekiras.demo.model;
/**
* Created by ekansh on 19/10/15.
*/
public class Person {
private String name;
private Integer image;
private String email;
private String desc;
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", image='" + image + '\'' +
", email='" + email + '\'' +
", desc='" + desc + '\'' +
'}';
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getImage() {
return image;
}
public void setImage(Integer image) {
this.image = image;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
}
package com.ekiras.demo.adapter;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.ekiras.demo.R;
import com.ekiras.demo.model.Person;
import java.util.List;
/**
* Created by ekansh on 20/10/15.
*/
public class PersonAdapter extends BaseAdapter{
private List<Person> personList;
private Activity activity;
private LayoutInflater layoutInflater;
private ViewHolder viewHolder;
public PersonAdapter(Activity activity,List<Person> personList) {
this.personList = personList;
this.activity = activity;
}
@Override
public int getCount() {
return personList.size();
}
@Override
public Person getItem(int i) {
return personList.get(i);
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
if(view ==null){
layoutInflater = LayoutInflater.from(activity);
view = layoutInflater.inflate(R.layout.list_item_person, viewGroup, false);
viewHolder = new ViewHolder(view);
view.setTag(viewHolder);
}
else
viewHolder = (ViewHolder) view.getTag();
Person person = getItem(i);
viewHolder.setValues(person);
return view;
}
private class ViewHolder {
private TextView name;
private TextView email;
private TextView desc;
public ViewHolder(View view) {
name = (TextView) view.findViewById(R.id.text_view_name);
email = (TextView) view.findViewById(R.id.text_view_email);
desc = (TextView) view.findViewById(R.id.text_view_desc);
}
public void setValues(Person person) {
name.setText(person.getName());
email.setText(person.getEmail());
desc.setText(person.getDesc());
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<TextView
android:id="@+id/title_person_list"
android:text="Person List"
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center"
android:textSize="22dp"/>
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="@android:color/transparent"
android:dividerHeight="5dp"
android:layout_below="@+id/title_person_list"
android:id="@+id/person_list"
/>
</RelativeLayout>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment