好用的ListView adapter

最近在写android程序,要常常用到BaseAdapter,用多了以为很很差用,代码不少冗余。因而网上找个好的替代品,发现有一个叫作easy-adapter的库,试用了下以为很好用,这里先记录下来。把其中的源码贴出来,稍微作些解释。
先贴出要实现的效果:  android

 20140612_222717

首先把ListView的item布局贴出来: git

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="wrap_content"
    android:layout_width="match_parent">
    <ImageView
        android:id="@+id/image_view_person"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:scaleType="centerCrop" />
    <TextView
        android: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" />
    <TextView
        android: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>
其次要实现一个继承自easy-adapter库中的叫ItemViewHolder的类的ViewHolder,这个ViewHolder和item布局文件对应,看代码更容易懂,其中的Person类看后面代码,而里面的@LayoutId和@ViewId在库中被解析,@LayoutId后跟布局文件id,@ViewId后跟布局文件中的view的id,这样就将布局文件中的view和ViewHolder中的成员直接绑定了:
//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.
    @Override
    public 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.
    @Override
    public void onSetListeners(final Person item, PositionInfo positionInfo) {
        imageViewPerson.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getContext(), getContext().getString(R.string.my_name_string, item.getName()), Toast.LENGTH_LONG).show();
            }
        });
    }
}
上面的Person类代码以下:
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;
    }
}
而后在Activiy里这样用,其中的DataProvider是提供数据的,后面会列出代码:
public class MainActivity extends Activity {
    ListView mListView;
    @Override
    protected 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 set
          the list of items later.
         */
        //加上这一句就能够了
        mListView.setAdapter(new EasyAdapter<Person>(this, PersonViewHolder.class, DataProvider.getListPeople()));
    }
}
DataProvider类的代码:
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;
    }
}
这就是全部的代码,很是清晰易懂,这只是一个简单的例子,你能够将EasyAdapter用于全部的listview里,只需改变几个文件,就能够实现不一样布局的listview。
相关文章
相关标签/搜索