首先导入一个Xlistview的包
数据从网络获取
下面是小demo
是在网络请求类里面对Xlistview进行监听
利用handler进行刷新
package utils; import android.content.Context; import android.os.Handler; import android.os.Message; import com.bawei.xlistviewlibrary.XListView; import com.google.gson.Gson; import org.xutils.common.Callback; import org.xutils.http.RequestParams; import org.xutils.x; import java.text.SimpleDateFormat; import java.util.Date; import java.util.List; import adapter.Myadapter; import bean.MyBean; /** * Created by 1 on 2017/4/24. * 作用:网路请求类 * 作者: */ public class MyUtils { private List<MyBean.DataBean> mList; private Context context; private XListView mListView; private Handler handler = new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); } }; public MyUtils(Context context, XListView listView) { this.context = context; mListView = listView; } public void getxutils(String url){ RequestParams params = new RequestParams(MyURl.url); x.http().get(params, new Callback.CommonCallback<String>() { @Override public void onSuccess(String result) { Gson gson = new Gson(); MyBean bean = gson.fromJson(result, MyBean.class); mList = bean.getData(); final Myadapter myadapter = new Myadapter(context,mList); mListView.setAdapter(myadapter);
//设置是否可以刷新和加载
mListView.setPullLoadEnable(true); mListView.setPullRefreshEnable(true);
//给Xlistview设置监听 mListView.setXListViewListener(new XListView.IXListViewListener() { @Override public void onRefresh() { handler.postDelayed(new Runnable() { @Override public void run() {
//刷新适配器 myadapter.notifyDataSetChanged(); onLoad();//获取系统时间的方法 } },2000); } @Override public void onLoadMore() { handler.postDelayed(new Runnable() { @Override public void run() {
//加载数据 mListView.setAdapter(myadapter); onLoad(); } },2000); } }); } @Override public void onError(Throwable ex, boolean isOnCallback) { } @Override public void onCancelled(CancelledException cex) { } @Override public void onFinished() { } }); }
//停止刷新和停止加载,和获取系统当前时间的方法 public void onLoad(){ mListView.stopLoadMore(); mListView.stopRefresh(); SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期的格式 Date date = new Date(System.currentTimeMillis());//获取系统当前时间 String s = format.format(date);//格式化时间 mListView.setRefreshTime(s);//设置刷新时间 } }