一武鲁、RecycleView的基本使用
代碼下載:https://github.com/TangfeiJi/MyRecycleViw
RecycleView同ListView一樣在APP頁(yè)面中以列表的形式顯示元素翎朱,由于提供數(shù)據(jù)的方式有很多種list狐榔,array,map或者set等牍帚,但是最終都需要將這些數(shù)據(jù)源晶衷,以統(tǒng)一的樣式顯示出來(lái)嗽元。因此在RecycleView(ListView)的使用中沧烈,有RecycleView負(fù)責(zé)顯示掠兄,Adpter負(fù)責(zé)對(duì)數(shù)據(jù)進(jìn)行適配操作,數(shù)據(jù)源(一般是List)部分锌雀。
1.1 gradle倒包
implementation'com.android.support:recyclerview-v7:26.1.0'
適配器的BaseQuickAdapter??
compile'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.30'
1.2 布局控件
在Activity的activity_main.xml布局文件中添加RecycleView控件
<android.support.v7.widget.RecyclerView? ? android:id="@+id/recycler"? ? android:layout_width="match_parent"? ? android:layout_height="match_parent"/>
為這個(gè)RecycleView創(chuàng)建一個(gè)item布局文件item_recycleview.xml蚂夕,自己定義布局
1.3 Adater代碼和Activity代碼的編寫(xiě)
我這里用的是?BaseQuickAdapter網(wǎng)上進(jìn)行封裝好的一般的話(huà)我們都自己有一些簡(jiǎn)單的封裝,但是相對(duì)而言不是很完善腋逆,擴(kuò)展性并不是很強(qiáng)婿牍,所有找到了BaseRecyclerViewAdapterHelper,它是一款BaseAdapter的集合體闲礼,在Github的Start較多牍汹,也較為好用,因?yàn)榉庋b比較全面柬泽,代碼如下
public class ArticleListAdapterextends BaseQuickAdapter {private ContextmContext;public ArticleListAdapter(Context context,@Nullable List data) {super(R.layout.item_article, data);mContext = context;? ? }@Override? ? protected void convert(final BaseViewHolder holder,final ArticleBean bean) {? ? ? ? holder.setText(R.id.tv_title, Html.fromHtml(bean.getTitle()))? ? ? ? ? ? ? ? .setText(R.id.tv_author, bean.getAuthor())? ? ? ? ? ? ? ? .setText(R.id.tv_time, bean.getNiceDate())? ? ? ? ? ? ? ? .setText(R.id.tv_type, bean.getChapterName());? ? }}
在項(xiàng)目中還運(yùn)用到了mvp? 的方法進(jìn)行解耦?
SwipeRefreshLayout? 的上拉下拉刷新?
butterknife 的使用
因?yàn)樽罱枰鲰?xiàng)目沒(méi)有具體的介紹 現(xiàn)在得趕項(xiàng)目了 另外附上代碼? 自己寫(xiě)的Demo可以大家參考借鑒
在代碼中進(jìn)行學(xué)習(xí)知識(shí)。項(xiàng)目中的接口用的洪洋大神的接口嫁蛇。