天天看点

使用通用适配器Base-adapter-helper

适配器写多了会有一种写到吐的感觉,今天来体验一下不用写适配器等待感觉,一般来说,我们写适配器都是重写getView方法,然后使用ViewHolder设计模式,在getView里进行数据绑定,写一次还好,写多了会感觉在不断做重复工作,那么有没有这么一个通用的适配器供我们使用,而不用写那么多次重复代码呢,答案是有的,见https://github.com/JoanZapata/base-adapter-helper,今天就使用一下这个适配器。

在这之前,先看下实现效果。

使用通用适配器Base-adapter-helper

开始编写实体类,涉及到的就是标题,描述,以及图片资源

package cn.edu.zafu.demo;

public class News {
    private String title;
    private String description;
    private int imgId;

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public int getImgId() {
        return imgId;
    }

    public void setImgId(int imgId) {
        this.imgId = imgId;
    }

    @Override
    public String toString() {
        return "News [title=" + title + ", description=" + description
                + ", imgId=" + imgId + "]";
    }

}
           

对应的布局

<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="wrap_content"
    tools:context="${relativePackage}.${activityClass}" >

    <ImageView
        android:id="@+id/img"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_centerVertical="true"
        android:layout_marginLeft="5dp"
        android:background="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="5dp"
        android:layout_toRightOf="@id/img"
        android:text="标题标题标题标题标标题标题 "
        android:textSize="16sp" />

    <TextView
        android:id="@+id/description"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/title"
        android:layout_marginBottom="5dp"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="5dp"
        android:layout_toRightOf="@id/img"
        android:text="描述描述描述描述描 " />

</RelativeLayout>
           

见证奇迹的时候到了,看我们如何用几行代码搞定这个界面,在这之前,我们先伪造几个数据。

private List<News> news=new ArrayList<News>();
    private void initData() {
        News _new=new News();
        _new.setTitle("南京遭虐待男童养母被刑拘");
        _new.setDescription("涉故意伤害;此前男童被");
        _new.setImgId(R.drawable.qblog_fig1);
        news.add(_new);

        _new=new News();
        _new.setTitle("今日头条");
        _new.setDescription("今日描述啊飒飒");
        _new.setImgId(R.drawable.qblog_fig2);
        news.add(_new);

        _new=new News();
        _new.setTitle("今日头条");
        _new.setDescription("今日描述啊飒飒");
        _new.setImgId(R.drawable.qblog_fig3);
        news.add(_new);
    }
           

开始初始化我们的界面

private ListView listView;
    private void initView() {
        listView=(ListView) findViewById(R.id.listview);
        listView.setAdapter(new QuickAdapter<News>(this, R.layout.item, news) {

            @Override
            protected void convert(BaseAdapterHelper helper, News item) {
                helper.setText(R.id.title, item.getTitle());
                helper.setText(R.id.description, item.getDescription());
                helper.setBackgroundRes(R.id.img, item.getImgId());
            }
        });
    }
           

在onCreate里调用

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initData();
        initView();
    }
           

运行一下,你就会发现,好神奇,数据和listView进行了绑定,没错,这就是通用的适配器的作用,其实这个库里还有另外一个适配器,有兴趣的自己去看看用法。

源码下载

http://download.csdn.net/detail/sbsujjbcy/8565361