RecyclerViewHelper中使用的RecyclerView

版权声明:欢迎转载,转载附上链接 https://blog.csdn.net/generallizhong/article/details/91437154

一、在很多事时候我们都需要用到下拉列表,侧滑等,以前我们常常使用的基本都是listview,在这里我介绍另一种RecyclerView的几种使用,RecyclerView控件的一些常用功能封装, 包括(上拉加载更多、头尾布局、拖拽排序、侧滑删除、侧滑选择、万能分割线,最后附上DEMO

二、这里吧依赖添加上

在项目根目录的build.gradle文件中加入如下代码:

allprojects {
    repositories {
        maven { url "https://jitpack.io" }
    }
}

三、在app根目录的buil.gradle文件中加入依赖:

dependencies {
    implementation 'com.github.alidili:RecyclerViewHelper:1.0.0'
}

3.1上拉加载更多与下拉加载效果

效果图:

集成:

// 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();

自定义加载布局:

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));
}

 

3.2 头和尾布局

效果图:

集成:

// 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);
}

 

3.3 item拖移动排序

效果图:

忘记拖移就截图,这里就当被拖移了

集成:

// 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();

 

3.4  侧滑删除item

效果图:

集成:

// 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();

 

3.5 侧滑显示按钮

效果图:

集成:

<?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,也可以配合其他控件或者单独使用。

 

3.6  分割线

效果图:

集成:

// 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>

最后附上DEMO

百度网盘: 下载     提取码:wz19

CSDN: https://download.csdn.net/download/generallizhong/11235737

猜你喜欢

转载自blog.csdn.net/generallizhong/article/details/91437154