最近在寫android程序,要常常用到BaseAdapter,用多了以爲很很差用,代碼不少冗餘。因而網上找個好的替代品,發現有一個叫作easy-adapter的庫,試用了下以爲很好用,這裏先記錄下來。把其中的源碼貼出來,稍微作些解釋。
先貼出要實現的效果: android
其次要實現一個繼承自easy-adapter庫中的叫ItemViewHolder的類的ViewHolder,這個ViewHolder和item佈局文件對應,看代碼更容易懂,其中的Person類看後面代碼,而裏面的@LayoutId和@ViewId在庫中被解析,@LayoutId後跟佈局文件id,@ViewId後跟佈局文件中的view的id,這樣就將佈局文件中的view和ViewHolder中的成員直接綁定了:<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_height="wrap_content"android:layout_width="match_parent"><ImageViewandroid:id="@+id/image_view_person"android:layout_width="80dp"android:layout_height="80dp"android:scaleType="centerCrop" /><TextViewandroid:id="@+id/text_view_name"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_toRightOf="@id/image_view_person"android:paddingTop="10dp"android:paddingRight="10dp"android:paddingLeft="10dp"android:textSize="20sp" /><TextViewandroid:id="@+id/text_view_phone"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_toRightOf="@id/image_view_person"android:layout_below="@id/text_view_name"android:layout_marginLeft="10dp"android:textSize="16sp"android:textColor="#0099cc" /></RelativeLayout>
上面的Person類代碼以下://Annotate the class with the layout ID of the item.
@LayoutId(R.layout.person_item_layout)public class PersonViewHolder extends ItemViewHolder<Person> {//Annotate every field with the ID of the view in the layout.
//The views will automatically be assigned to the fields.
@ViewId(R.id.image_view_person)ImageView imageViewPerson;@ViewId(R.id.text_view_name)TextView textViewName;@ViewId(R.id.text_view_phone)TextView textViewPhone;//Extend ItemViewHolder and call super(view)
public PersonViewHolder(View view) {
super(view);
}//Override onSetValues() to set the values of the items in the views.
@Overridepublic void onSetValues(Person person, PositionInfo positionInfo) {imageViewPerson.setImageResource(person.getResDrawableId());textViewName.setText(person.getName());textViewPhone.setText(person.getPhoneNumber());}//Optionally override onSetListeners to add listeners to the views.
@Overridepublic void onSetListeners(final Person item, PositionInfo positionInfo) {imageViewPerson.setOnClickListener(new View.OnClickListener() {
@Overridepublic void onClick(View v) {Toast.makeText(getContext(), getContext().getString(R.string.my_name_string, item.getName()), Toast.LENGTH_LONG).show();}});}}
而後在Activiy裏這樣用,其中的DataProvider是提供數據的,後面會列出代碼:public class Person {private String name;
private String phoneNumber;
private int resDrawableId;public Person() {
}public Person(String name, String phoneNumber, int resDrawableId) {this.name = name;
this.phoneNumber = phoneNumber;
this.resDrawableId = resDrawableId;
}public String getName() {
return name;
}public void setName(String name) {this.name = name;
}public String getPhoneNumber() {
return phoneNumber;
}public void setPhoneNumber(String phoneNumber) {this.phoneNumber = phoneNumber;
}public int getResDrawableId() {return resDrawableId;
}public void setResDrawableId(int resDrawableId) {this.resDrawableId = resDrawableId;
}}
DataProvider類的代碼:public class MainActivity extends Activity {ListView mListView;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);mListView = (ListView) findViewById(R.id.list_view);/*
Simply create an EasyAdapter by passing a Context, your ItemViewHolder class and the list of items.Alternatively, you can create an EasyAdapter only with a Context and an ItemViewHolder class and setthe list of items later.*///加上這一句就能夠了
mListView.setAdapter(new EasyAdapter<Person>(this, PersonViewHolder.class, DataProvider.getListPeople()));}}
這就是全部的代碼,很是清晰易懂,這只是一個簡單的例子,你能夠將EasyAdapter用於全部的listview裏,只需改變幾個文件,就能夠實現不一樣佈局的listview。public class DataProvider {//Returns a mock List of Person objects
public static List<Person> getListPeople() {List<Person> listPeople = new ArrayList<Person>();
listPeople.add(new Person("Henry Blair", "07123456789", R.drawable.male1));listPeople.add(new Person("Jenny Curtis", "07123456789", R.drawable.female1));listPeople.add(new Person("Vincent Green", "07123456789", R.drawable.male2));listPeople.add(new Person("Ada Underwood", "07123456789", R.drawable.female2));listPeople.add(new Person("Daniel Erickson", "07123456789", R.drawable.male3));listPeople.add(new Person("Maria Ramsey", "07123456789", R.drawable.female3));listPeople.add(new Person("Rosemary Munoz", "07123456789", R.drawable.female4));listPeople.add(new Person("John Singleton", "07123456789", R.drawable.male4));listPeople.add(new Person("Lorena Bowen", "07123456789", R.drawable.female5));listPeople.add(new Person("Kevin Stokes", "07123456789", R.drawable.male5));listPeople.add(new Person("Johnny Sanders", "07123456789", R.drawable.male6));listPeople.add(new Person("Jim Ramirez", "07123456789", R.drawable.male7));listPeople.add(new Person("Cassandra Hunter", "07123456789", R.drawable.female6));listPeople.add(new Person("Viola Guerrero", "07123456789", R.drawable.female7));return listPeople;
}}