欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

SwipeRefreshLayout使用

程序员文章站 2024-03-18 14:18:46
...

一.新建LoadDataScrollController
public class LoadDataScrollController extends RecyclerView.OnScrollListener implements SwipeRefreshLayout.OnRefreshListener {
public enum LayoutManagerType {
LINEAR_LAYOUT,
GRID_LAYOUT,
STAGGERED_GRID_LAYOUT
}
/**
* 当前布局管理器的类型
*/
private LayoutManagerType mLayoutManagerType;

/**
 * 当前RecycleView显示的最大条目
 */
private int mLastVisibleItemPosition;


/**
 * 每列的最后一个条目
 */
private int[] mLastPostions;


/**
 * 是否正在加载数据 包括刷新和向上加载更多
 */
private boolean isLoadData = false;


/**
 * 回调接口
 */
private OnRecycleRefreshListener mListener;


public LoadDataScrollController(OnRecycleRefreshListener onRecycleRefreshListener) {
    this.mListener = onRecycleRefreshListener;
}

@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {

    /**
     * 获取布局参数
     */
    RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();

    //如果为null,第一次运行,确定布局类型
    if (mLayoutManagerType == null) {
        if (layoutManager instanceof LinearLayoutManager) {
            mLayoutManagerType = LayoutManagerType.LINEAR_LAYOUT;
        } else if (layoutManager instanceof GridLayoutManager) {
            mLayoutManagerType = LayoutManagerType.GRID_LAYOUT;
        } else if (layoutManager instanceof StaggeredGridLayoutManager) {
            mLayoutManagerType = LayoutManagerType.STAGGERED_GRID_LAYOUT;
        } else {
            throw new RuntimeException("LayoutManager should be LinearLayoutManager,GridLayoutManager,StaggeredGridLayoutManager");
        }
    }

    //对于不太能够的布局参数,不同的方法获取到当前显示的最后一个条目数
    switch (mLayoutManagerType) {
        case LINEAR_LAYOUT:
            mLastVisibleItemPosition = ((LinearLayoutManager) layoutManager).findLastVisibleItemPosition();
            break;
        case GRID_LAYOUT:
            mLastVisibleItemPosition = ((GridLayoutManager) layoutManager).findLastVisibleItemPosition();
            break;
        case STAGGERED_GRID_LAYOUT:
            StaggeredGridLayoutManager staggeredGridLayoutManager = (StaggeredGridLayoutManager) layoutManager;
            if (mLastPostions == null) {
                mLastPostions = new int[staggeredGridLayoutManager.getSpanCount()];
            }
            staggeredGridLayoutManager.findLastVisibleItemPositions(mLastPostions);
            mLastVisibleItemPosition = findMax(mLastPostions);
            break;
        default:
            break;
    }

}

@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {

    RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();

    //RecycleView 显示的条目数
    int visibleCount = layoutManager.getChildCount();

    //显示数据总数
    int totalCount = layoutManager.getItemCount();


    // 四个条件,分别是是否有数据,状态是否是滑动停止状态,显示的最大条目是否大于整个数据(注意偏移量),是否正在加载数据
    if(visibleCount>0
            &&newState==RecyclerView.SCROLL_STATE_IDLE
            &&mLastVisibleItemPosition>=totalCount-1
            &&!isLoadData){
        //可以加载数据
        if(mListener!=null){
            isLoadData = true;
            mListener.loadMore();
        }
    }

}


/**
 * 当是瀑布流时,获取到的是每一个瀑布最下方显示的条目,通过条目进行对比
 */
private int findMax(int[] lastPositions) {
    int max = lastPositions[0];
    for (int value : lastPositions) {
        if (value > max) {
            max = value;
        }
    }
    return max;
}


public void setLoadDataStatus(boolean isLoadData){
    this.isLoadData = isLoadData;
}

@Override
public void onRefresh() {
    //刷新数据的方法
    if(mListener!=null){
        isLoadData = true;
        mListener.refresh();
    }

}

/**
 * 数据加载接口回调
 */
public interface OnRecycleRefreshListener{
    void refresh();
    void loadMore();
}

}
二.调用
private LoadDataScrollController mController;

private ProgressDialog pd;
private MyWalletAdapter mAdapter;

void getDate(){
mSwipeRefresh.setColorSchemeColors(Color.GRAY);

    /**
     * 创建控制器,同时使当前activity实现数据监听回调接口
     */
    mController = new LoadDataScrollController(this);
    mAdapter = new MyWalletAdapter(getContext());

    //设置垂直的线性布局管理器,Orientation --> VERTICAL:垂直 HORIZONTAL:水平
    LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
    layoutManager.setOrientation(LinearLayoutManager.VERTICAL);

    //StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);

    //添加分割线
    mRecycle.addItemDecoration(new DividerItemDecoration(getContext().getApplicationContext(), DividerItemDecoration.VERTICAL));


    mRecycle.setLayoutManager(layoutManager);

    mRecycle.setItemAnimator(new DefaultItemAnimator());

    mRecycle.setAdapter(mAdapter);

    mAdapter.notifyDataSetChanged();

    /**
     * 设置监听
     */
    mRecycle.addOnScrollListener(mController);

    mSwipeRefresh.setOnRefreshListener(mController);

}
@Override
public void refresh() {
    //刷新的接口调
    mSwipeRefresh.postDelayed(new Runnable() {
        @Override
        public void run() {
            mAdapter.notifyDataSetChanged();
            mSwipeRefresh.setRefreshing(false);
            mController.setLoadDataStatus(false);
        }
    },2000);
}

@Override
public void loadMore() {
    //加载更多的接口回调
    pd = new ProgressDialog(getContext());
    pd.show();
    mSwipeRefresh.postDelayed(new Runnable() {
        @Override
        public void run() {
          //  mAdapter.notifyDataSetChanged();
            //设置数据加载结束的监听状态
            mController.setLoadDataStatus(false);
            pd.dismiss();
        }
    },2000);
}