Reprint please indicate the source: juejin.cn/post/684490…

This article is from Rong Hua Xie Hou’s blog

1. Write at the front

The first article in 2019, share an open source project written by myself, mainly on RecyclerView control of some common functions packaging, including (pull up load more, head and tail layout, drag sort, side slide delete, side slide selection, universal partition line).

RecyclerViewHelper mainly uses the decorator mode to expand the function of the original Adapter of the project, which will not affect the original structure of the project. It is very convenient to integrate and modify. Let’s take a look.

Making portal

2. Introduce dependent libraries

  • Add the following code to the build.gradle file in the project root directory:
allprojects {
    repositories {
        maven { url "https://jitpack.io"}}}Copy the code
  • Add dependencies to buil.gradle file in app root directory:
dependencies {
    implementation 'com. Making. Alidili: RecyclerViewHelper: 1.0.0'
}
Copy the code

As of this post, the latest version is 1.0.0, which can be viewed on GitHub.

RecyclerView version used in RecyclerViewHelper is as follows:

implementation "Androidx. Recyclerview: recyclerview: 1.0.0."
Copy the code

3. Pull-up loads more

Effect:

Integration:

// CommonAdapter = new CommonAdapter(mDataList); mLoadMoreWrapper = new LoadMoreWrapper(commonAdapter); // Custom load layout customLoadingView(); mRecyclerView.setLayoutManager(new LinearLayoutManager(this)); mRecyclerView.setAdapter(mLoadMoreWrapper); / / set the tensile load monitoring mRecyclerView. AddOnScrollListener (newOnScrollListener() {
	@Override
	public void onLoadMore() {/ / set the data being loaded state, customizable layout mLoadMoreWrapper. SetLoadStateNotify (mLoadMoreWrapper. LOADING);if(mDataList. Size () < 52) {// Get data // set data loading status, Customizable layout mLoadMoreWrapper. SetLoadStateNotify (mLoadMoreWrapper. LOADING_COMPLETE); }else{/ / set all the data loaded state, customizable layout mLoadMoreWrapper. SetLoadStateNotify (mLoadMoreWrapper. LOADING_END); }}}); / / need to use the refresh data outer Adapter mLoadMoreWrapper. NotifyDataSetChanged ();Copy the code

Custom load layout:

private void customLoadingViewProgressBar = new ProgressBar(this); mLoadMoreWrapper.setLoadingView(progressBar); TextView = new TextView(this); textView.setText("End"); mLoadMoreWrapper.setLoadingEndView(textView); / / layout highly mLoadMoreWrapper setLoadingViewHeight (DensityUtils dp2px (this, 50)); }Copy the code

4. Head and tail layout

Effect:

Integration:

// CommonAdapter = new CommonAdapter(mDataList); mHeaderAndFooterWrapper = new HeaderAndFooterWrapper(commonAdapter); // Add a header and tail layout addHeaderAndFooterView(); mRecyclerView.setLayoutManager(new LinearLayoutManager(this)); mRecyclerView.setAdapter(mHeaderAndFooterWrapper); / / need to use the refresh data outer Adapter mHeaderAndFooterWrapper. NotifyDataSetChanged ();Copy the code

Add a header and tail layout:

private void addHeaderAndFooterView() {// Header layout 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);
}
Copy the code

5. Drag and drop sort

Effect:

Integration:

// CommonAdapter = new CommonAdapter(mDataList); MDragAndDropWrapper = new DragAndDropWrapper(commonAdapter, mDataList, 200); mDragAndDropWrapper.attachToRecyclerView(mRecyclerView,true); mRecyclerView.setLayoutManager(new LinearLayoutManager(this)); mRecyclerView.setAdapter(mDragAndDropWrapper); / / need to use the refresh data outer Adapter mDragAndDropWrapper. NotifyDataSetChanged ();Copy the code

6. Sideslip deletion

Effect:

Integration:

// CommonAdapter = new CommonAdapter(mDataList); mSwipeToDismissWrapper = new SwipeToDismissWrapper(commonAdapter, mDataList); mSwipeToDismissWrapper.attachToRecyclerView(mRecyclerView); mRecyclerView.setLayoutManager(new LinearLayoutManager(this)); mRecyclerView.setAdapter(mSwipeToDismissWrapper); / / set to delete event listeners mSwipeToDismissWrapper. SetItemDismissListener (new ItemSwipeCallback.ItemDismissListener() { @Override public void onItemDismiss(final int position) { // TODO } }); / / need to use the refresh data outer Adapter mSwipeToDismissWrapper. NotifyDataSetChanged ();Copy the code

7. Sideslip selection

Effect:

Integration:

<? 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 layout <LinearLayout Android :layout_width="140dp"
            android:layout_height="match_parent"
            android:orientation="horizontal"> / / function button < / LinearLayout > < / LinearLayout > < / com. Yl. Recyclerview. Widget. SlideItemView >Copy the code

The SlideItemView control is generic and not limited to RecyclerView. It can also be used with other controls or by itself.

8. Universal divider

Effect:

Integration:

// CommonAdapter is the original Adapter of the project mDividerAdapter = new DividerAdapter(mDataList); LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this); mRecyclerView.setLayoutManager(linearLayoutManager); / / set the line SuperDividerItemDecoration dividerItemDecoration = new SuperDividerItemDecoration (this, linearLayoutManager); / / line style can customize dividerItemDecoration setDrawable (getResources (). GetDrawable (R.d rawable. Custom_bg_divider)); mRecyclerView.addItemDecoration(dividerItemDecoration); mRecyclerView.setAdapter(mDividerAdapter);Copy the code

Split line style:

<? 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>
Copy the code

9. Write at the end

Here, the basic function of RecyclerViewHelper is introduced. If you have any questions, you can give me a comment or submit Issues in GitHub, thank you!

Making portal

RecyclerViewHelper project will be followed by more common functions, Kotlin version is under development, stay tuned!