擴充卡寫多了會有一種寫到吐的感覺,今天來體驗一下不用寫擴充卡等待感覺,一般來說,我們寫擴充卡都是重寫getView方法,然後使用ViewHolder設計模式,在getView裡進行資料綁定,寫一次還好,寫多了會感覺在不斷做重複工作,那麼有沒有這麼一個通用的擴充卡供我們使用,而不用寫那麼多次重複代碼呢,答案是有的,見https://github.com/JoanZapata/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