优秀案例

basicUI(五)——RecycleView的下拉刷新、加载更

2020-07-23  本文已影响0人  Peakmain

以下是我的基本UI组件该系列的文章,欢迎大家转载和分享:
基本UI组件的封装库(一)——basicUI
基本UI组件的封装库(二)——basicUI
基本UI组件的封装库——basicUI的demo的编写
基本UI组件的封装库(三)——basicUI
基本UI组件的封装库(四)——basicUI

RecycleView封装实现下拉刷新和加载更多

前言

How to

Step 1. Add the JitPack repository to your build file

Add it in your root build.gradle at the end of repositories:

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

Step 2. Add the dependency

    dependencies {
            implementation 'com.github.Peakmain:BasicUI:1.0.0'
    }

效果

录屏软件不行,录制的不清晰


效果图.gif
下拉刷新效果2.gif

使用

    <com.peakmain.ui.recyclerview.view.LoadRefreshRecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
        mRecyclerView.addRefreshViewCreator(new DefaultRefreshViewCreator());
        mRecyclerView.setOnRefreshListener(this);
    public void onRefresh() {
        new Handler().postDelayed(() -> {
            List<String> data = getData();
            mAdapter.setData(data);
            mRecyclerView.onStopRefresh();
        }, 2000);
    }
  mRecyclerView.addLoadViewCreator(new DefalutLoadViewCreator());
        mRecyclerView.setOnLoadMoreListener(this);
   public void onLoad() {
        new Handler().postDelayed(() -> {
            List<String> moreData = getMoreData();
            mAdapter.addData(moreData);
            mRecyclerView.onStopLoad();
        }, 2000);

    }
public class BestMissRefreshCreator extends RefreshViewCreator {
    // 加载数据的ImageView
    private ImageView mRefreshIv;
    @Override
    public View getRefreshView(Context context, ViewGroup parent) {
        View refreshView = LayoutInflater.from(context).inflate(R.layout.layout__bestmiss_refresh_header_view, parent, false);
        mRefreshIv = refreshView.findViewById(R.id.img_progress);
        return refreshView;
    }

    @Override
    public void onPull(int currentDragHeight, int refreshViewHeight, int currentRefreshStatus) {
        if (currentRefreshStatus == LoadRefreshRecyclerView.LOAD_STATUS_PULL_DOWN_REFRESH) {
            mRefreshIv.setImageResource(R.drawable.list_view_pull);
        }
        if (currentRefreshStatus == LoadRefreshRecyclerView.LOAD_STATUS_LOOSEN_LOADING) {
            mRefreshIv.setImageResource(R.drawable.list_view_release);
        }
    }

    @Override
    public void onRefreshing() {
        mRefreshIv.setImageResource(R.drawable.load_more_anim);
        ((AnimationDrawable) mRefreshIv.getBackground()).start();
    }
    @Override
    public void onStopRefresh() {
        // 停止加载的时候清除动画
        mRefreshIv.setRotation(0);
        ((AnimationDrawable) mRefreshIv.getBackground()).stop();
        mRefreshIv.clearAnimation();
    }
}

加载更多继承LoadViewCreator即可
上图下拉刷新2示例代码

public class LoadMoreCreator extends LoadViewCreator {
    // 加载数据的ImageView
    private TextView mLoadTv;
    private View mRefreshIv;

    @Override
    public View getLoadView(Context context, ViewGroup parent) {
        View refreshView = LayoutInflater.from(context).inflate(R.layout.layout_load_footer_view, parent, false);
        mLoadTv = (TextView) refreshView.findViewById(R.id.load_tv);
        mRefreshIv = refreshView.findViewById(R.id.refresh_iv);
        return refreshView;
    }

    @Override
    public void onPull(int currentDragHeight, int refreshViewHeight, int currentRefreshStatus) {
        if (currentRefreshStatus == LoadRefreshRecyclerView.LOAD_STATUS_PULL_DOWN_REFRESH) {
            mLoadTv.setText("上拉加载更多");
        }
        if (currentRefreshStatus == LoadRefreshRecyclerView.LOAD_STATUS_LOOSEN_LOADING) {
            mLoadTv.setText("松开加载更多");
        }
    }

    @Override
    public void onLoading() {
        mLoadTv.setVisibility(View.INVISIBLE);
        mRefreshIv.setVisibility(View.VISIBLE);

        // 加载的时候不断旋转
        RotateAnimation animation = new RotateAnimation(0, 720,
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        animation.setRepeatCount(-1);
        animation.setDuration(1000);
        mRefreshIv.startAnimation(animation);
    }

    @Override
    public void onStopLoad() {
        // 停止加载的时候清除动画
        mRefreshIv.setRotation(0);
        mRefreshIv.clearAnimation();
        mLoadTv.setText("上拉加载更多");
        mLoadTv.setVisibility(View.VISIBLE);
        mRefreshIv.setVisibility(View.INVISIBLE);
    }


    @Override
    public void onFinishLoadData() {
       mLoadTv.setText("无更多数据");
    }
}

RecycleView实现多状态布局

多状态布局.gif
使用
使用WarpRecyclerView(只能添加头部或尾部)、RefreshRecyclerView(下拉刷新)或者LoadRefreshRecyclerView(加载更多或者下拉刷新)
    <declare-styleable name="MultipleStatusView">
        <attr name="loadingView" format="reference" />
        <attr name="errorView" format="reference" />
        <attr name="emptyView" format="reference" />
        <attr name="noNetworkView" format="reference" />
    </declare-styleable>

所以使用示例如下

    <com.peakmain.ui.recyclerview.view.LoadRefreshRecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:emptyView="@layout/layout_empty_view"
        app:errorView="@layout/layout_error_view"
        app:noNetworkView="@layout/layout_network_view"
        app:loadingView="@layout/layout_loading_view"/>

注意:这里的emptyView、errorView、noNetworkView布局的根布局需要设置id,对应的id分别是:empty_retry_view、error_retry_view、no_network_retry_view

 mRecyclerView.showNoNetwork();
mRecyclerView.showEmptyView();
  mRecyclerView.showLoading();
mRecyclerView.hideLoading();
 mRecyclerView.showError();
mRecyclerView.showContentView();
 mRecyclerView.setOnRetryClickListener(v -> ToastUtils.showShort("正在重新请求接口..."));
上一篇下一篇

猜你喜欢

热点阅读