轉載請註明出處:juejin.im/post/5c6dfd…android
本文出自 容華謝後的博客git
2019年的第一篇文章,分享一個本身寫的開源項目,主要是對RecyclerView控件的一些經常使用功能封裝, 包括(上拉加載更多、頭尾佈局、拖拽排序、側滑刪除、側滑選擇、萬能分割線)。github
RecyclerViewHelper主要使用了裝飾者模式對項目原有的Adapter進行功能擴展,不會影響項目的原有結構,集成和修改都很是方便,一塊兒來看下。bash
GitHub傳送門app
allprojects {
repositories {
maven { url "https://jitpack.io" }
}
}
複製代碼
dependencies {
implementation 'com.github.alidili:RecyclerViewHelper:1.0.0'
}
複製代碼
截止此篇文章發佈時,最新版本爲1.0.0,最新版本可在GitHub中查看。maven
RecyclerViewHelper中使用的RecyclerView版本以下:ide
implementation "androidx.recyclerview:recyclerview:1.0.0"
複製代碼
效果圖:佈局
集成:post
// CommonAdapter爲項目原有Adapter
CommonAdapter commonAdapter = new CommonAdapter(mDataList);
mLoadMoreWrapper = new LoadMoreWrapper(commonAdapter);
// 自定義加載佈局
customLoadingView();
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mRecyclerView.setAdapter(mLoadMoreWrapper);
// 設置上拉加載監聽
mRecyclerView.addOnScrollListener(new OnScrollListener() {
@Override
public void onLoadMore() {
// 設置數據正在加載狀態,可自定義佈局
mLoadMoreWrapper.setLoadStateNotify(mLoadMoreWrapper.LOADING);
if (mDataList.size() < 52) {
// 獲取數據
// 設置數據加載完成狀態,可自定義佈局
mLoadMoreWrapper.setLoadStateNotify(mLoadMoreWrapper.LOADING_COMPLETE);
} else {
// 設置全部數據加載完成狀態,可自定義佈局
mLoadMoreWrapper.setLoadStateNotify(mLoadMoreWrapper.LOADING_END);
}
}
});
// 刷新數據須要使用外層Adapter
mLoadMoreWrapper.notifyDataSetChanged();
複製代碼
自定義加載佈局:gradle
private void customLoadingView() {
// 正在加載佈局
ProgressBar progressBar = new ProgressBar(this);
mLoadMoreWrapper.setLoadingView(progressBar);
// 全部數據加載完成佈局
TextView textView = new TextView(this);
textView.setText("End");
mLoadMoreWrapper.setLoadingEndView(textView);
// 佈局高度
mLoadMoreWrapper.setLoadingViewHeight(DensityUtils.dp2px(this, 50));
}
複製代碼
效果圖:
集成:
// CommonAdapter爲項目原有Adapter
CommonAdapter commonAdapter = new CommonAdapter(mDataList);
mHeaderAndFooterWrapper = new HeaderAndFooterWrapper(commonAdapter);
// 添加頭尾佈局
addHeaderAndFooterView();
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mRecyclerView.setAdapter(mHeaderAndFooterWrapper);
// 刷新數據須要使用外層Adapter
mHeaderAndFooterWrapper.notifyDataSetChanged();
複製代碼
添加頭尾佈局:
private void addHeaderAndFooterView() {
// 頭佈局
View headerView = View.inflate(this, R.layout.layout_header_footer, null);
TextView headerItem = headerView.findViewById(R.id.tv_item);
headerItem.setText("HeaderView");
mHeaderAndFooterWrapper.addHeaderView(headerView);
// 尾佈局
View footerView = View.inflate(this, R.layout.layout_header_footer, null);
TextView footerItem = footerView.findViewById(R.id.tv_item);
footerItem.setText("FooterView");
mHeaderAndFooterWrapper.addFooterView(footerView);
}
複製代碼
效果圖:
集成:
// CommonAdapter爲項目原有Adapter
CommonAdapter commonAdapter = new CommonAdapter(mDataList);
// 拖拽事件響應時間默認爲200ms,可自定義
mDragAndDropWrapper = new DragAndDropWrapper(commonAdapter, mDataList, 200);
mDragAndDropWrapper.attachToRecyclerView(mRecyclerView, true);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mRecyclerView.setAdapter(mDragAndDropWrapper);
// 刷新數據須要使用外層Adapter
mDragAndDropWrapper.notifyDataSetChanged();
複製代碼
效果圖:
集成:
// CommonAdapter爲項目原有Adapter
CommonAdapter commonAdapter = new CommonAdapter(mDataList);
mSwipeToDismissWrapper = new SwipeToDismissWrapper(commonAdapter, mDataList);
mSwipeToDismissWrapper.attachToRecyclerView(mRecyclerView);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mRecyclerView.setAdapter(mSwipeToDismissWrapper);
// 設置刪除事件監聽
mSwipeToDismissWrapper.setItemDismissListener(new ItemSwipeCallback.ItemDismissListener() {
@Override
public void onItemDismiss(final int position) {
// TODO
}
});
// 刷新數據須要使用外層Adapter
mSwipeToDismissWrapper.notifyDataSetChanged();
複製代碼
效果圖:
集成:
<?xml version="1.0" encoding="utf-8"?>
<com.yl.recyclerview.widget.SlideItemView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/slide_item"
android:layout_width="match_parent"
android:layout_height="40dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
// Item佈局
<LinearLayout
android:layout_width="140dp"
android:layout_height="match_parent"
android:orientation="horizontal">
// 功能按鈕
</LinearLayout>
</LinearLayout>
</com.yl.recyclerview.widget.SlideItemView>
複製代碼
SlideItemView控件是通用的,不侷限於RecyclerView,也能夠配合其餘控件或者單獨使用。
效果圖:
集成:
// CommonAdapter爲項目原有Adapter
mDividerAdapter = new DividerAdapter(mDataList);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(linearLayoutManager);
// 設置分割線
SuperDividerItemDecoration dividerItemDecoration = new SuperDividerItemDecoration(this, linearLayoutManager);
// 分割線樣式可自定義
dividerItemDecoration.setDrawable(getResources().getDrawable(R.drawable.custom_bg_divider));
mRecyclerView.addItemDecoration(dividerItemDecoration);
mRecyclerView.setAdapter(mDividerAdapter);
複製代碼
分割線樣式:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<size
android:width="5dp"
android:height="5dp" />
<solid android:color="@color/colorPrimary" />
</shape>
複製代碼
到這裏,RecyclerViewHelper的基本功能就介紹完了,若有問題能夠給我留言評論或者在GitHub中提交Issues,謝謝!
RecyclerViewHelper項目後續還會增長更多經常使用功能,Kotlin版本正在開發中,敬請期待!