Public number: program ape development center hope to write something that can help you 🤣🤣

1. Write at the front

Share an open source project written by myself, mainly on RecyclerView control of some common functions of packaging, including (pull load more, head and tail layout, drag sorting, 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 is the original Adapter of the project
CommonAdapter commonAdapter = new CommonAdapter(mDataList);
mLoadMoreWrapper = new LoadMoreWrapper(commonAdapter);
// Customize the loading layout
customLoadingView();
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mRecyclerView.setAdapter(mLoadMoreWrapper);

// Set the pull-up load listener
mRecyclerView.addOnScrollListener(new OnScrollListener() {
    @Override
    public void onLoadMore(a) {
        // Set the loading state of the data, customizable layout
        mLoadMoreWrapper.setLoadStateNotify(mLoadMoreWrapper.LOADING);

        if (mDataList.size() < 52) {
            // Get data
            // Set the state of data loading completion, you can customize the layout
            mLoadMoreWrapper.setLoadStateNotify(mLoadMoreWrapper.LOADING_COMPLETE);
        } else {
            // Set the loading state of all data, you can customize the layoutmLoadMoreWrapper.setLoadStateNotify(mLoadMoreWrapper.LOADING_END); }}});// Refresh data using the outer Adapter
mLoadMoreWrapper.notifyDataSetChanged();

Copy the code

Custom load layout:

private void customLoadingView(a) {
    // Loading the layout
    ProgressBar progressBar = new ProgressBar(this);
    mLoadMoreWrapper.setLoadingView(progressBar);

    // All data is loaded to complete the layout
    TextView textView = new TextView(this);
    textView.setText("End");
    mLoadMoreWrapper.setLoadingEndView(textView);

    // Layout height
    mLoadMoreWrapper.setLoadingViewHeight(DensityUtils.dp2px(this.50));
}

Copy the code

4. Head and tail layout

Effect:

Integration:

// CommonAdapter is the original Adapter of the project
CommonAdapter commonAdapter = new CommonAdapter(mDataList);
mHeaderAndFooterWrapper = new HeaderAndFooterWrapper(commonAdapter);
// Add a header and tail layout
addHeaderAndFooterView();
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mRecyclerView.setAdapter(mHeaderAndFooterWrapper);

// Refresh data using the outer Adapter
mHeaderAndFooterWrapper.notifyDataSetChanged();

Copy the code

Add a header and tail layout:

private void addHeaderAndFooterView(a) {
    / / head 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);

    / / layout
    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 is the original Adapter of the project
CommonAdapter commonAdapter = new CommonAdapter(mDataList);
// The drag event response time is 200ms by default and can be customized
mDragAndDropWrapper = new DragAndDropWrapper(commonAdapter, mDataList, 200);
mDragAndDropWrapper.attachToRecyclerView(mRecyclerView, true);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mRecyclerView.setAdapter(mDragAndDropWrapper);

// Refresh data using the outer Adapter
mDragAndDropWrapper.notifyDataSetChanged();

Copy the code

6. Sideslip deletion

Effect:

Integration:

// CommonAdapter is the original Adapter of the project
CommonAdapter commonAdapter = new CommonAdapter(mDataList);
mSwipeToDismissWrapper = new SwipeToDismissWrapper(commonAdapter, mDataList);
mSwipeToDismissWrapper.attachToRecyclerView(mRecyclerView);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mRecyclerView.setAdapter(mSwipeToDismissWrapper);

// Set the deletion event listener
mSwipeToDismissWrapper.setItemDismissListener(new ItemSwipeCallback.ItemDismissListener() {
    @Override
    public void onItemDismiss(final int position) {
        // TODO}});// Refresh data using the 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 dividing line
SuperDividerItemDecoration dividerItemDecoration = new SuperDividerItemDecoration(this, linearLayoutManager);
// The split line style can be customized
dividerItemDecoration.setDrawable(getResources().getDrawable(R.drawable.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!

RecyclerViewHelper project will be followed by more common functions, Kotlin version is under development, will be forwarded to my public account, please look forward to it!

Public number: program ape development center hope to write something that can help you 🤣🤣