package com.ce.fragment;
import java.util.List;
import com.ce.adapter.MyAdapter;
import com.ce.vo.Blog;
import com.ce.vo.News1;
import com.ce.vo.Super1;
import com.ce.vo.Super2;
import com.ce.xlistview.XListView;
import com.ce.xlistview.XListView.IXListViewListener;
import com.ce.yuekao2.R;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.thoughtworks.xstream.XStream;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class F1 extends Fragment implements IXListViewListener {
String url;
int i;
int a = 0;
public F1(String url, int i) {
super();
this.url = url;
this.i = i;
}
private MyAdapter1 adapter;
private List<News1> list;
private XListView xlistview;
private Handler handler2;
List<News1> ll;
Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
int i = msg.what;
if (i == 1) {// 刷新
ll = (List<News1>) msg.obj;
adapter = new MyAdapter1(getActivity(), list);
xlistview.setAdapter(adapter);
} else if (i == 2) {
// 加载
// adapter = new List_Adapter(getActivity(), list);
List<News1> l = (List<News1>) msg.obj;
ll.addAll(l);
} else {
adapter = new MyAdapter1(getActivity(), list);
xlistview.setAdapter(adapter);
}
adapter.notifyDataSetChanged();
};
};
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = View.inflate(getActivity(), R.layout.f1, null);
xlistview = (XListView) view.findViewById(R.id.Xlistview);
xlistview.setPullLoadEnable(true);
xlistview.setPullRefreshEnable(true);
xlistview.setXListViewListener(this);// 监听
handler2 = new Handler();
// 解析数据
getData("");
return view;
}
private void getData(String s) {
// 请求数据
new Thread() {
public void run() {
HttpUtils();
}
}.start();
}
private void HttpUtils() {
a++;
com.lidroid.xutils.HttpUtils utils = new HttpUtils();
utils.send(HttpMethod.POST, url + a, new RequestCallBack<String>() {
@Override
public void onFailure(HttpException arg0, String arg1) {
// TODO Auto-generated method stub
}
@Override
public void onSuccess(ResponseInfo<String> arg0) {
String result = arg0.result;
// 成功请求到数据后我们需要解析不同的文件
XStream stream = new XStream();
stream.processAnnotations(Super1.class);
Super1 s = (Super1) stream.fromXML(result);
list = s.getNewslist().getNews();
if (s.equals("刷新")) {
handler.sendMessage(handler.obtainMessage(1, list));
}
if (s.equals("加载")) {
handler.sendMessage(handler.obtainMessage(2, list));
} else {
handler.sendMessage(handler.obtainMessage(3, list));
}
}
});
};
public void onLoad() {
xlistview.stopRefresh();
xlistview.stopLoadMore();
xlistview.setRefreshTime("刚刚");
}
// 刷新
public void onRefresh() {
handler2.postDelayed(new Runnable() {
public void run() {
getData("刷新");
xlistview.setAdapter(adapter);
onLoad();
}
}, 2000);
}
// 加载更多
public void onLoadMore() {
handler2.postDelayed(new Runnable() {
public void run() {
getData("加载");
adapter.notifyDataSetChanged();
onLoad();
}
}, 2000);
}
}