上拉加载更多的数据叠加

package com.bwie.test.baweistudent.fragment;

import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Toast;

import com.bwie.test.baweistudent.R;
import com.bwie.test.baweistudent.adapter.XLV_Confirm_Adapter;
import com.bwie.test.baweistudent.application.BaseApplication;
import com.bwie.test.baweistudent.bean.StuInfo;
import com.bwie.test.baweistudent.net.HttpUrl;
import com.bwie.test.baweistudent.ui.XListView;
import com.bwie.test.baweistudent.utils.GsonUtils;
import com.okhttplib.CacheLevel;
import com.okhttplib.CallbackOk;
import com.okhttplib.HttpInfo;
import com.okhttplib.OkHttpUtil;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2016/7/22.
 */
public class Confirm_Fragment extends BaseFragment implements XListView.IXListViewListener {

    private View view;
    private XListView xlv_confirm;
    private BaseApplication application;
    private String tk;
    private int pn = 1;
    private XLV_Confirm_Adapter xlv_confirm_adapter;
    private List allList = new ArrayList();

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
        }
    };

    @Override
    public View getView(LayoutInflater inflater) {
        view = inflater.inflate(R.layout.confirm_fragment, null);
        findView();
        application = (BaseApplication) getActivity().getApplication();
        tk = application.getTk();
        tk=getActivity().getSharedPreferences("isLogin",getActivity().MODE_PRIVATE).getString("token","");
        return view;
    }

    public void findView() {
        xlv_confirm = (XListView) view.findViewById(R.id.xlv_confirm);
        xlv_confirm.setPullLoadEnable(true);
        xlv_confirm.setXListViewListener(this);
    }

    @Override
    public void initData() {

        findView();

        OkHttpUtil.Builder().setCacheLevel(CacheLevel.FIRST_LEVEL).setConnectTimeout(30).build(getActivity()).doGetAsync(HttpInfo.Builder().setUrl(HttpUrl.getStuInfo + "flag=" + 0 + "&&pn=" + 1 + "&&tk=" + tk).build(), new CallbackOk() {
            @Override
            public void onResponse(HttpInfo info) throws IOException {
                if (info.isSuccessful()) {
                    String result = info.getRetDetail();
                    StuInfo stuInfo = GsonUtils.jsonToObject(result, StuInfo.class);
                    List list = stuInfo.getList();
                    allList.addAll(list);
                    if(xlv_confirm_adapter==null){
                        xlv_confirm_adapter = new XLV_Confirm_Adapter(getActivity(), allList,tk);
                        xlv_confirm.setAdapter(xlv_confirm_adapter);
                    }else{
                        xlv_confirm_adapter.notifyDataSetChanged();
                    }
                } else {
                    Toast.makeText(getActivity(), "网络请求失败!!!", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    @Override
    public void setData() {
    }


    @Override
    public void onRefresh() {
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                OkHttpUtil.Builder().setCacheLevel(CacheLevel.FIRST_LEVEL).setConnectTimeout(30).build(getActivity()).doGetAsync(HttpInfo.Builder().setUrl(HttpUrl.getStuInfo + "flag=" + 0 + "&&pn=" + 1 + "&&tk=" + tk).build(), new CallbackOk() {
                    @Override
                    public void onResponse(HttpInfo info) throws IOException {
                        if (info.isSuccessful()) {
                            String result = info.getRetDetail();
                            StuInfo stuInfo = GsonUtils.jsonToObject(result, StuInfo.class);
                            List list = stuInfo.getList();
                            allList.addAll(list);
                            if(xlv_confirm_adapter==null){
                                xlv_confirm_adapter = new XLV_Confirm_Adapter(getActivity(), allList,tk);
                                xlv_confirm.setAdapter(xlv_confirm_adapter);
                            }else{
                                xlv_confirm_adapter.notifyDataSetChanged();
                            }
                        } else {
                            Toast.makeText(getActivity(), "网络请求失败!!!", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
                onLoad();
            }
        }, 3000);
    }

    @Override
    public void onLoadMore() {
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                pn++;
                OkHttpUtil.Builder().setCacheLevel(CacheLevel.FIRST_LEVEL).setConnectTimeout(30).build(getActivity()).doGetAsync(HttpInfo.Builder().setUrl(HttpUrl.getStuInfo + "flag=" + 0 + "&&pn=" + pn + "&&tk=" + tk).build(), new CallbackOk() {
                    @Override
                    public void onResponse(HttpInfo info) throws IOException {
                        if (info.isSuccessful()) {
                            String result = info.getRetDetail();
                            StuInfo stuInfo = GsonUtils.jsonToObject(result, StuInfo.class);
                            List list = stuInfo.getList();
                            allList.addAll(list);
                            if(xlv_confirm_adapter==null){
                                xlv_confirm_adapter = new XLV_Confirm_Adapter(getActivity(), allList,tk);
                                xlv_confirm.setAdapter(xlv_confirm_adapter);
                            }else{
                                xlv_confirm_adapter.notifyDataSetChanged();
                            }
                        } else {
                            Toast.makeText(getActivity(), "网络请求失败!!!", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
                onLoad();
            }
        }, 3000);
    }

    private void onLoad() {
        xlv_confirm.stopRefresh();
        xlv_confirm.stopLoadMore();
        //获取系统当前时间
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd  HH:mm");
        String nowTime = format.format(new Date());
        xlv_confirm.setRefreshTime(nowTime);
    }
}

你可能感兴趣的:(上拉加载更多的数据叠加)