使用谷歌官方SwipeRefreshLayout+Recyclerview实现下拉刷新和上拉加载

目前市场上已经有很多成熟的刷新框架,本文主要说一下利用谷歌官方提供的SwipeRefreshLayout实现下拉刷新和上拉加载。

使用SwipeRefreshLayout的步骤比较简单,,就两步:

1,添加布局组件

2,实现下拉刷新

    

        
    
        refreshLayout = view.findViewById(R.id.id_ref);
        refreshLayout.setColorSchemeColors(Color.RED, Color.BLUE, Color.YELLOW);
        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        doRefresh(adapter,0);
                    }
                }, 1000);
            }
        });

onRefresh中的写法是模拟网络请求1秒延迟,其中的doRefresh是自己定义的刷新方法

    //刷新,增加随机数量的项
    private void doRefresh(RecyclerViewNewsAdapter adapter,int type) {
        Random random = new Random();
        int i = random.nextInt(5) + 1;
        for (int j = 0; j < i; j++) {
            NewsBean newsBean = new NewsBean();
            int k = random.nextInt(3) + 1;
            newsBean.setType(k);
            newsBean.setTitle("刷新item");
            newsBean.setContent("刷新的内容...");
            newsBean.setPic(R.mipmap.ic_launcher);
            if (type==0){
            list.add(0, newsBean);
            }else {
            list.add(list.size(), newsBean);
            }
        }
        adapter.update(list);
        if (type==0){
        refreshLayout.setRefreshing(false);
        }
    }

doRefresh中的NewsBean是自己定义的实体类

type==1的情况是上拉加载的情况,下面会看到

 

上拉加载:SwipeRefreshLayout并没有提供上拉加载功能,但我们可以利用Recyclerview实现它。

也是两步:

1,自定义一个listener,重写RecyclerView的OnScrollListener的onScrollStateChanged和onScrolled方法

2,为RecyclerView添加这个listener

/**
 * author SGY
 * date 2019/7/4
 * desc
 */
public abstract class onLoadMoreListener extends RecyclerView.OnScrollListener {

    private int countItem;
    private int lastItem;
    private boolean isScrolled = false;
    private RecyclerView.LayoutManager layoutManager;

    protected abstract  void onLoading(int countItem,int lastItem);

    @Override
    public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
        
        //拖拽或者惯性滑动时isScolled设置为true
        if (newState == SCROLL_STATE_DRAGGING || newState == SCROLL_STATE_SETTLING) {
            isScrolled = true;
        } else {
            isScrolled = false;
        }
    }

    @Override
    public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
        if (recyclerView.getLayoutManager() instanceof LinearLayoutManager) {
            layoutManager = recyclerView.getLayoutManager();
            countItem = layoutManager.getItemCount();
            lastItem = ((LinearLayoutManager) layoutManager).findLastCompletelyVisibleItemPosition();
        }
        if (isScrolled && countItem != lastItem && lastItem == countItem - 1) {
            onLoading(countItem, lastItem);
        }
    }
}

然后使用它

        id_rv_news.addOnScrollListener(new onLoadMoreListener() {
            @Override
            protected void onLoading(int countItem, int lastItem) {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        doRefresh(adapter,1);
                    }
                }, 1000);
            }
        });

这里会看到,doRefresh方法通过传入不同的类型来区别上拉和下拉

你可能感兴趣的:(安卓开发)