PullToRefreshListView实现下拉刷新和上拉加载更多

PullToRefreshListView是一个很好用的第三方库,可以支持下拉刷新和上拉加载更多数据,下面给出一个万能的框架:

大家使用的时侯只需要改变适配器和网络的访问方式就可以了,这里用到的是okhttp

public class TuanFragment extends Fragment {

    private List datasEntities;//数据集合
    @ViewInject(R.id.lv_tuanlist)
    PullToRefreshListView pullToRefreshListView;
    private TuanGoodsAdapter adapter;
    private int page;//当前页数,默认为1
    private int size = 10;//每页数量
    private int count;//数据总量
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_tuan, null);
        ViewUtils.inject(this, view);
        initView();
        return view;
    }
    public void initView(){
        pullToRefreshListView.setMode(PullToRefreshBase.Mode.BOTH);//设置模式
        pullToRefreshListView.setShowIndicator(false);//去掉箭头图标
        pullToRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener() {
            @Override
            public void onRefresh(PullToRefreshBase refreshView) {
                //当getScrollY()小于0时,代表下拉刷新,否则是上拉加载
                getDataFromServer(pullToRefreshListView.getScrollY() < 0);
                //设置上拉加载的文字提示
                ILoadingLayout loadingLayout = pullToRefreshListView.getLoadingLayoutProxy(false, true);
                loadingLayout.setPullLabel("上拉加载...");
            }
        });
        pullToRefreshListView.setOnItemClickListener(this);//条目点击
//        //首次来到页面加载数据
        new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message msg) {
                pullToRefreshListView.setRefreshing();//刷新
                return true;
            }
        }).sendEmptyMessageDelayed(0, 300);
    }
    public void getDataFromServer(final boolean refresh){
        if(refresh){
            //下拉刷新
            page = 1;
        }else{
            //上拉加载
            page++;
        }
        //访问网络
        OkHttpUtils.get().url(Constant.TUAN_URL).addParams("page",page+"")
                .addParams("size",size+"").build().execute(new StringCallback() {
            @Override
            public void onError(Request request, Exception e) {
                pullToRefreshListView.onRefreshComplete();
            }

            @Override
            public void onResponse(String response) {
                pullToRefreshListView.onRefreshComplete();
                Goods goods = new Gson().fromJson(response, new TypeToken() {
                }.getType());
                count = goods.getCount();//数据的总量
                if (refresh) {//下拉刷新
                    datasEntities = goods.getDatas();//获取数据
                    adapter = new TuanGoodsAdapter(getActivity(), datasEntities);
                    pullToRefreshListView.setAdapter(adapter);
                } else {//上拉加载更多
                    //在原有数据的集合中把新的数据添加进去
                    datasEntities.addAll(goods.getDatas());
                    adapter.notifyDataSetChanged();
                }
                //当前获取的数据量小于每页的条数,说明没有数据了
                if (goods.getDatas.size < size) {
                    ToastUtil.showToast(getActivity(), "没数据了");
                    //设置为不支持上刷新了
                    pullToRefreshListView.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
                }
            }
        });
    }
}

你可能感兴趣的:(PullToRefreshListView实现下拉刷新和上拉加载更多)