轉載請标明出處:
http://blog.csdn.net/developer_jiangqq/article/details/49724999
本文出自:【江清清的部落格】
(一).前言:
【好消息】個人網站已經上線運作,後面部落格以及技術幹貨等精彩文章會同步更新,請大家關注收藏:http://www.lcode.org
項目開發中對于清單(listview)和表格(girdview)中的資料顯示,我們就需要寫自定義的Adapter。是以一般一個項目下來少得十幾個Adapter,多的二十幾個甚至更多。但是Adapter的處理一般就是傳入資料,view模闆,getView,之類的在加入一些控制顯示之類的代碼。雖然寫起來難度不大,但是很多類似的代碼經常需要狂寫,簡直會有想吐的趕腳吧~。今天為大家介紹一款快速Adapter的架構,隻需要建立QuickAdapter,傳入布局,資料,然後綁定控件以及顯示的資料即可。這樣一講是不是趕腳非常友善了,OK我們趕緊行動吧~。
Base-Adapter-Helper(Github位址)
FastDev4Android架構項目位址:https://github.com/jiangqqlmj/FastDev4Android
(二).基本介紹:
2.1:BaseAdapterHelper是BaseAdapter ViewHolder模闆的抽象,讓我們更快的寫出Adapter來綁定資料顯示。官網給出的使用執行個體和實作效果如下:
下載下傳該項目下來,整個項目一共四個類檔案:
- BaseAdapterHelper 可以使得BaseAdapter的getView()方法更加簡單的使用。擺脫ViewHolder模闆的寫法
- QuickAdapter 該類可以讓我們自定義的BaseAdapter類的代碼大量的縮減,讓我們把精力集中在業務實作上面。我們隻需要做的是綁定控件view和資料model即可。
- EnhancedQuickAdapter 增強的QuickAdapter,主要是用于綁定控件view和資料model
- BaseQuickAdapter QuickAdapter的高層類封裝,實作BaseAdapter中抽象方法,同時對于data資料的操作以及其他操作
2.2.AndroidStudio引入Base-Adapter-Helper庫,一種方式最簡單了直接使用以下方式依賴:
compile'com.joanzapata.android:base-adapter-helper:1.1.11'
我這邊采用的是第二種方式:因為該庫隻有四個類,是以我就把這個類直接複制進入去了,如下:
不過直接這樣還行,因為該項目中setImageUrl()方法中圖檔異步加載采用的是Picasso架構,是以要麼你
修改該架構,要麼你就可以以下方式引入該架構:
compile 'com.squareup.picasso:picasso:2.5.2'
2.3.BaseAdapterHelper中的方法介紹:
- 對于任何TextView:setText()調用setText(String)
- 對于任何View:setAlpha()調用setAlpha(float)
- 對于任何View:setVisiable()調用setVisibility(int)
- 對于任何TextView:linkify()調用Linkify.addLinks(view,ALL)
- 對于任何TextView:setTypeface()調用setTypeface(Typeface)
- 對于任何ProgressBar:setProgress()調用setProgress(int)
- 對于任何ProgressBar:setMax()調用setMax(int)
- 對于任何RatingBar:setRating()調用setRating(int)
- 對于任何ImageView:setImageResource()調用setImageResource(int)
- 對于任何ImageView:setImageDrawable()調用setImageDrawable(Drawable)
- 對于任何ImageVIew:setImageBitmap()調用setImageBitmap(bitmap)
- 使用setImageUrl()會使用Picasso架構來進行下載下傳圖檔然後現在ImageView中
- 使用seImageBuilder()會使用Picasso的圖檔請求建構器
- setOnClickListener()
- setOnTouchListener()
- setOnLongClickListener()
- setTag()
- setChecked()
- setAdapter()
2.4.我們可以使用showIndeterminateProgress(boolean)可以控制清單底下顯示進度。如下:
(三).使用執行個體:
有了以上對于架構的基本介紹和引入之後,現在我們可以來具體實作一個例子了,其實用法很簡單:
- 準備資料(和以往一樣)
- 建立擴充卡(建立擴充卡并且綁定資料和相關布局控件,model即可)--這邊就不需要重寫一個自定義的擴充卡了。
- Listview綁定擴充卡(和以往一樣)
3.1.準備資料:ModuleBean和DataUtil
packagecom.chinaztt.fda.entity;
/**
*目前類注釋:清單資料測試資料實體類
*項目名:FastDev4Android
*包名:com.chinaztt.fda.entity
*作者:江清清 on 15/11/8 17:56
*郵箱:[email protected]
*QQ: 781931404
* 公司:江蘇中天科技軟體技術有限公司
*/
publicclass ModuleBean {
private String modulename;
private String imgurl;
private String description;
public ModuleBean() {
}
public ModuleBean(String modulename, Stringimgurl, String description) {
this.modulename = modulename;
this.imgurl = imgurl;
this.description = description;
}
public String getModulename() {
return modulename;
}
public void setModulename(Stringmodulename) {
this.modulename = modulename;
}
public String getImgurl() {
return imgurl;
}
public void setImgurl(String imgurl) {
this.imgurl = imgurl;
}
public String getDescription() {
return description;
}
public void setDescription(Stringdescription) {
this.description = description;
}
@Override
public String toString() {
return "ModuleBean{" +
"modulename='" +modulename + '\'' +
", imgurl='" + imgurl+ '\'' +
", description='" +description + '\'' +
'}';
}
}
packagecom.chinaztt.fda.test;
importcom.chinaztt.fda.entity.ModuleBean;
importjava.util.ArrayList;
importjava.util.List;
/**
*目前類注釋:
*項目名:FastDev4Android
*包名:com.chinaztt.fda.test
*作者:江清清 on 15/11/8 17:53
*郵箱:[email protected]
*QQ: 781931404
* 公司:江蘇中天科技軟體技術有限公司
*/
publicclass DataUtils {
/**
* 進行構造相關資料
* @return
*/
public static List<ModuleBean>getAdapterData(){
List<ModuleBean> moduleBeans=newArrayList<ModuleBean>();
ModuleBean moduleBean=new ModuleBean();
moduleBean.setModulename("完美“價”給你");
moduleBean.setImgurl("http://interface.zttmall.com/Images/upload/image/20150325/20150325083110_0898.jpg");
moduleBean.setDescription("标題1的簡要說明");
moduleBeans.add(moduleBean);
moduleBean=new ModuleBean();
moduleBean.setModulename("探路者,旅行記");
moduleBean.setImgurl("http://interface.zttmall.com/Images/upload/image/20150325/20150325083214_8280.jpg");
moduleBean.setDescription("标題2的簡要說明");
moduleBeans.add(moduleBean);
moduleBean=new ModuleBean();
moduleBean.setModulename("進口商品,徹底放價");
moduleBean.setImgurl("http://interface.zttmall.com/Images/upload/image/20150328/20150328105404_2392.jpg");
moduleBean.setDescription("标題3的簡要說明");
moduleBeans.add(moduleBean);
moduleBean=new ModuleBean();
moduleBean.setModulename("鮮果季");
moduleBean.setImgurl("http://interface.zttmall.com/Images/upload/image/20150325/20150325083611_0644.jpg");
moduleBean.setDescription("标題4的簡要說明");
moduleBeans.add(moduleBean);
moduleBean=new ModuleBean();
moduleBean.setModulename("盼盼 法式小面包");
moduleBean.setImgurl("http://interface.zttmall.com/Images/upload/image/20150312/20150312100454_8837.jpg");
moduleBean.setDescription("标題5的簡要說明");
moduleBeans.add(moduleBean);
moduleBean=new ModuleBean();
moduleBean.setModulename("雀巢 脆脆鲨 威化 480g/盒");
moduleBean.setImgurl("http://interface.zttmall.com/Images/upload/image/20150312/20150312100617_0693.jpg");
moduleBean.setDescription("标題6的簡要說明");
moduleBeans.add(moduleBean);
moduleBean=new ModuleBean();
moduleBean.setModulename("主題館1");
moduleBean.setImgurl("http://interface.zttmall.com/Images/upload/image/20150129/20150129163540_6179.jpg");
moduleBean.setDescription("标題7的簡要說明");
moduleBeans.add(moduleBean);
moduleBean=new ModuleBean();
moduleBean.setModulename("主題館2");
moduleBean.setImgurl("http://interface.zttmall.com/Images/upload/image/20150129/20150129163615_1774.jpg");
moduleBean.setDescription("标題8的簡要說明");
moduleBeans.add(moduleBean);
moduleBean=new ModuleBean();
moduleBean.setModulename("主題館3");
moduleBean.setImgurl("http://interface.zttmall.com/Images/upload/image/20150129/20150129163635_1130.jpg");
moduleBean.setDescription("标題9的簡要說明");
moduleBeans.add(moduleBean);
moduleBean=new ModuleBean();
moduleBean.setModulename("主題館4");
moduleBean.setImgurl("http://interface.zttmall.com/Images/upload/image/20150129/20150129163840_0270.jpg");
moduleBean.setDescription("标題10的簡要說明");
moduleBeans.add(moduleBean);
moduleBean=new ModuleBean();
moduleBean.setModulename("主題館5");
moduleBean.setImgurl("http://interface.zttmall.com/Images/upload/image/20150129/20150129163849_4099.jpg");
moduleBean.setDescription("标題11的簡要說明");
moduleBeans.add(moduleBean);
return moduleBeans;
}
}
3.2.建立擴充卡(綁定資料,控件相關)和綁定擴充卡
packagecom.chinaztt.fda.test;
importandroid.os.Bundle;
importandroid.widget.ListView;
importcom.chinaztt.fda.adapter.base.BaseAdapterHelper;
importcom.chinaztt.fda.adapter.base.QuickAdapter;
importcom.chinaztt.fda.entity.ModuleBean;
importcom.chinaztt.fda.ui.R;
importcom.chinaztt.fda.ui.base.BaseActivity;
importorg.androidannotations.annotations.AfterViews;
importorg.androidannotations.annotations.EActivity;
importorg.androidannotations.annotations.ViewById;
importjava.util.List;
/**
*目前類注釋:baseAdapterhelper 使用執行個體
*項目名:FastDev4Android
*包名:com.chinaztt.fda.test
*作者:江清清 on 15/11/8 17:39
*郵箱:[email protected]
*QQ: 781931404
* 公司:江蘇中天科技軟體技術有限公司
*/
@EActivity(R.layout.base_adapter_test_layout)
publicclass BaseAdapterTestActivity extends BaseActivity {
@ViewById
ListView lv_base_adapter;
private QuickAdapter<ModuleBean>mAdapter;
private List<ModuleBean> moduleBeans;
@Override
protected void onCreate(BundlesavedInstanceState) {
super.onCreate(savedInstanceState);
}
@AfterViews
public void bindLvData(){
moduleBeans=DataUtils.getAdapterData();
if(mAdapter==null) {
mAdapter = newQuickAdapter<ModuleBean>(this, R.layout.lv_item_base_layout,moduleBeans){
@Override
protected voidconvert(BaseAdapterHelper helper, ModuleBean item) {
mAdapter.showIndeterminateProgress(true);
helper.setText(R.id.text_lv_item_title, item.getModulename())
.setText(R.id.text_lv_item_description, item.getDescription())
.setImageUrl(R.id.img_lv_item, item.getImgurl());
}
};
lv_base_adapter.setAdapter(mAdapter);
}
}
}
3.3.運作效果如下
好了到此Base-Adapter-Helper的基本介紹和基本使用已經講完了,相信大家已經會初步使用了,下一
篇我們對于源代碼的實作做一個分析和擴充進階使用做講解。具體全部代碼已經上傳到FastDev4Android項目中了。同時歡迎大家去Github站點進行clone或者下載下傳浏覽:
https://github.com/jiangqqlmj/FastDev4Android 同時歡迎大家star和fork整個開源快速開發架構項目~
尊重原創,轉載請注明:From Sky丶清(http://blog.csdn.net/developer_jiangqq) 侵權必究!
關注我的訂閱号(codedev123),每天分享移動開發技術(Android/IOS),項目管理以及部落格文章!第一時間擷取推送文章!
關注我的微網誌,可以獲得更多精彩内容