## (一).前言:
項目開發中對于列表(listview)和表格(girdview)中的數據顯示,我們就需要寫自定義的Adapter。所以一般一個項目下來少得十幾個Adapter,多的二十幾個甚至更多。但是Adapter的處理一般就是傳入數據,view模板,getView,之類的在加入一些控制顯示之類的代碼。雖然寫起來難度不大,但是很多類似的代碼經常需要狂寫,簡直會有想吐的趕腳吧~。今天為大家介紹一款快速Adapter的框架,只需要創建QuickAdapter,傳入布局,數據,然后綁定控件以及顯示的數據即可。這樣一講是不是趕腳非常方便了,OK我們趕緊行動吧~。
Base-Adapter-Helper[(Github地址)](https://github.com/JoanZapata/base-adapter-helper)
FastDev4Android框架項目地址:[https://github.com/jiangqqlmj/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
*郵箱:jiangqqlmj@163.com
*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
*郵箱:jiangqqlmj@163.com
*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
*郵箱:jiangqqlmj@163.com
*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](https://github.com/jiangqqlmj/FastDev4Android)?同時歡迎大家star和fork整個開源快速開發框架項目~
- 前言
- Android快速開發框架介紹(一)
- Android首頁圖片自動無限循環輪播Gallery+FlowIndicator(二)
- Android 列表下拉刷新組件PullToRefreshListView使用(三)
- Android 數據緩存器ACache的詳解和使用(四)
- Android崩潰異常捕捉CustomCrash,提升用戶體驗(五)
- Android實現沉浸式狀態欄(六)
- AndroidAnnnotations注入框架介紹和Android Studios基本配置(七)
- AndroidAnnnotations注入框架的工作原理(八)
- AndroidAnnnotations注入框架使用之注入組件Components(九)
- AndroidAnnnotations注入框架使用之Injection標簽詳解(十)
- AndroidAnnnotations注入框架使用之事件綁定Event Binding(十一)
- AndroidAnnnotations注入框架使用之線程處理Threading(十二)
- AndroidAnnnotations注入框架使用之第三方框架集成RoboGuice(十三)
- AndroidAnnnotations注入框架使用之第三方框架集成Otto事件總線(十四)
- AndroidAnnnotations注入框架使用之第三方框架集成OrmLite(十五)
- AndroidAnnnotations注入框架使用之最佳實踐之Adapters和lists(十六)
- AndroidAnnnotations注入框架使用之最佳實踐SharedPreferences(十七)
- Android MVP開發模式詳解(十九)
- 消息總線EventBus的基本使用(二十)
- 消息總線EventBus源碼分析以及與Otto框架對比(二十一)
- 列表頭生成帶文本或者字母的圖片開源庫TextDrawable使用和詳解(二十二)
- 重寫WebView網頁加載以及JavaScript注入詳解(二十三)
- BaseAdapterHelper的基本使用介紹,讓你擺脫狂寫一堆Adapter煩惱(二十四)
- BaseAdapterHelper詳解源碼分析,讓你擺脫狂寫一堆Adapter煩惱(二十五)
- Volley完全解析之基礎使用(二十六)
- Volley完全解析之進階最佳實踐與二次封裝(二十七)
- RecyclerView完全解析,讓你從此愛上它(二十八)
- RecyclerView完全解析之打造新版類Gallery效果(二十九)
- RecyclerView完全解析之結合AA(Android Annotations)注入框架實例(三十)
- RecyclerView完全解析之下拉刷新與上拉加載SwipeRefreshLayout(三十一)
- CardView完全解析與RecyclerView結合使用(三十二)
- 神器ViewDragHelper完全解析,媽媽再也不擔心我自定義ViewGroup滑動View操作啦~(三十三)
- 神器ViewDragHelper完全解析之詳解實現QQ5.X側滑酷炫效果(三十四)
- 實例解析之SwipeRefreshLayout+RecyclerView+CardView(三十五)
- HorizontalScrollView,Fragment,FragmentStatePagerAdapter打造網易新聞Tab及滑動頁面效果(三十六)
- Android Design支持庫TabLayout打造仿網易新聞Tab標簽效果(三十七)
- 打造QQ6.X最新版本側滑界面效果(三十八)