EasyRecyclerView
描述
這是一個下拉刷新上拉加載更多框架(ps:後期還會加入一些常用的功能.),頭部用的秋哥的android-Ultra-Pull-To-Refresh,底部和沒有數據的狀態自己實現的.其實剛剛開始我是想找個庫直接用的,試了幾個排名靠前的,感覺跟自己想要的不太一樣,索性自己寫了一個,當然這當中也遇到了問題,多虧仲錦大師的幫助在此感謝.
感謝完了這裏附上庫的地址EasyRecyclerView
特點:
- 可定製的頭部 (可以查看android-Ultra-Pull-To-Refresh文檔)
- 可定製的底部 (加載中/沒有數據/加載失敗 三種狀態的定製)
- 可定製的沒有數據狀態顯示 (目前只有一個狀態)
- 目前提供一個實現好的ItemDecoration(頭部吸附效果)
效果預覽
1.定製頭部&定製腳步
- 頭部秋哥已經定製了很多樣式就直接使用了
- 腳部這邊使用的已經實現好的ErvDefaultFooter
默認頭部與頂部效果
material style頭部
2.頭部吸附
使用方式
依賴
gradle依賴(一切不能gradle依賴的庫都是耍流氓^_^,所以我加上了)
compile 'com.yysauce:easyrecyclerview:1.0.0'
library依賴
配置
目前有兩個參數可以配置
app:emply_layout
沒有數據時候佈局app:number_load_more
最後可見條目 + number_load_more > total 觸發加載更多;默認值爲4
xml中配置示例
<com.zly.www.easyrecyclerview.EasyDefRecyclerView
android:id="@+id/erv"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:emply_layout="@layout/erv_default_empty" />
activity代碼配置
erv.setAdapter(rvAdapter = new RvAdapter());//設置adapter
erv.setLastUpdateTimeRelateObject(this);//傳入參數類名作爲記錄刷新時間key
erv.setOnRefreshListener(this);//設置刷新監聽
erv.setOnLoadListener(this);//設置加載更多監聽
由於這裏使用的EasyDefRecyclerView,頭部就是默認經典樣式所以需要調用,使用其他頭部時不需要調用
erv.setLastUpdateTimeRelateObject(this);//傳入參數類名作爲記錄刷新時間key
adapter代碼配置
adapter需要實現CommonAdapter或者MultipleAdapter抽象方法
//創建ViewHolder
public abstract VH createCustomViewHolder(ViewGroup parent, int viewType);
//ViewHolder設置數據
public abstract void bindCustomViewHolder(VH holder, T t, int position);
MultipleAdapter多條目佈局還多一個方法需要實現
//返回多條目的type
public abstract int customItemViewType(int position);
目前提供了下面這些方法操作adapter數據,具體實現可以在CommonAdapter中查看
新增數據
- public void add(@NonNull T object)
- public void addAll(@NonNull Collection collection)
- public void addAll(@NonNull T… items)
- public void insert(@NonNull T object, int index)
- public void insertAll(@NonNull Collection collection, int index)
刪除數據
- public void remove(int index)
- public boolean remove(@NonNull T object)
- public void clear()
修改數據
- public void update(@NonNull List mDatas)
查看數據
- public T getItem(int position)
- public int getPosition(T item)
- public List getData()
排序
- public void sort(Comparator comparator)
加載佈局
- public View inflateView(@LayoutRes int resId, ViewGroup parent)
adapter中ViewHolder需要繼承BaseViewHolder
其他配置
頭部吸附效果
mItemDecoration = new StickItemDecoration(context,dataList) {
@Override
public String getTag(int position) {
return "吸附頭部顯示的文字";
}
}
erv.addItemDecoration(mItemDecoration);
這裏StickItemDecoration提供瞭如下方法來定製吸附效果
//設置吸附條目高度
public void setStickHeight(int mStickHeight)
//設置吸附條目背景
public void setStickBackgroundColor(int mStickBackgroundColor)
//設置吸附文字顏色
public void setStickTextColor(int mStickTextColor)
//設置吸附文字大小
public void setStickTextSize(int mStickTextSize)
//設置吸附文字leftmargin
public void setStickTextoffset(int mStickTextoffset)
自定義
頭部使用秋哥的android-Ultra-Pull-To-Refresh
秋哥默認已經實現了3個頭部
- MaterialHeader
- PtrClassicDefaultHeader
- StoreHouseHeader
一般情況下這些樣式應該夠了,如果有特殊需求可以自定義頭部.然後erv.setHeaderView(view);
底部的話目前我只實現了一個ErvDefaultFooter,自定義的話需要實現ErvLoadUIHandle接口.寫法可以參考ErvDefaultFooter
public interface ErvLoadUIHandle {
/**
* 允許加載更多
*/
int LOAD = 1;
/**
* 暫無更多數據
*/
int NOMORE = 2;
/**
* 加載失敗
*/
int LOADFAIL = 3;
/**
* @return 獲取底部當前狀態
*/
int getState();
void onLoading();//loading狀態實現
void onNoMore();//沒有數據狀態實現
void onLoadFail(OnLoadListener listener);//加載失敗實現
}
實現後調用setFooterView()方法設置
總結
目前還在EasyRecyclerView還在優化歡迎各位提出你們寶貴的意見,例子可以參考Sample
感謝
秋哥的android-Ultra-Pull-To-Refresh
仲錦大師的幫助
聯繫方式
qq:1835556188
License
Copyright (c) 2016 zhuliyuan
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Come on, don't tell me you read that.