package com.bwie.testOneFull.fragment;
import java.io.ByteArrayInputStream;
import java.util.ArrayList;
import java.util.List;
import me.maxwin.view.XListView;
import me.maxwin.view.XListView.IXListViewListener;
import com.bwie.testOneFull.R;
import com.bwie.testOneFull.R.layout;
import com.bwie.testOneFull.utils.MyPullParser;
import com.bwie.testOneFull.utils.NewsList;
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 android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
@SuppressLint("InflateParams")
public class FragmentOne extends Fragment implements IXListViewListener {
private View view;
private XListView mListView;
private MyAdapter adapter;
private int i = 1;
List<NewsList> newsParsesr = new ArrayList<>();
@Override
public View onCreateView(LayoutInflater inflater,
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragmentone, null);
mListView = (XListView) view.findViewById(R.id.mListView);
mListView.setPullLoadEnable(true);
mListView.setXListViewListener(this);
getInfo("刷新");
return view;
}
private void getInfo(final String str) {
// TODO Auto-generated method stub
HttpUtils utils = new HttpUtils();
String url = "http://www.oschina.net/action/api/news_list?catalog=1&pageIndex="
+ i + "&pageSize=10";
utils.send(HttpMethod.GET, url, new RequestCallBack<String>() {
public void onFailure(HttpException arg0, String arg1) {
// TODO Auto-generated method stub
Toast.makeText(getActivity(), "请检查网络", 0).show();
}
@Override
public void onSuccess(ResponseInfo<String> info) {
// 请求成功
String result = info.result;
// 获取流
ByteArrayInputStream bis = new ByteArrayInputStream(result
.getBytes());
// 解析流 通过Pull进行解析
List<NewsList> pullParsesr = MyPullParser.pullParsesr(bis);
if (str.equals("刷新")) {
newsParsesr.addAll(0, pullParsesr);
} else {
newsParsesr.addAll(pullParsesr);
}
// 设置适配器
setAdapter();
}
});
}
private void setAdapter() {
if (adapter == null) {
adapter = new MyAdapter(getActivity(), newsParsesr);
mListView.setAdapter(adapter);
} else {
adapter.notifyDataSetChanged();
}
}
@Override
public void onRefresh() {
// TODO Auto-generated method stub
new Handler().postAtTime(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
i = i + 1;
getInfo("刷新");
onLoder();
}
}, 2000);
}
@Override
public void onLoadMore() {
// TODO Auto-generated method stub
new Handler().postAtTime(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
i = i + 1;
getInfo("加载");
onLoder();
}
}, 2000);
}
private void onLoder() {
mListView.stopRefresh();
mListView.stopLoadMore();
mListView.setRefreshTime("刚刚");
}
}
import java.io.ByteArrayInputStream;
import java.util.ArrayList;
import java.util.List;
import me.maxwin.view.XListView;
import me.maxwin.view.XListView.IXListViewListener;
import com.bwie.testOneFull.R;
import com.bwie.testOneFull.R.layout;
import com.bwie.testOneFull.utils.MyPullParser;
import com.bwie.testOneFull.utils.NewsList;
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 android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
@SuppressLint("InflateParams")
public class FragmentOne extends Fragment implements IXListViewListener {
private View view;
private XListView mListView;
private MyAdapter adapter;
private int i = 1;
List<NewsList> newsParsesr = new ArrayList<>();
@Override
public View onCreateView(LayoutInflater inflater,
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragmentone, null);
mListView = (XListView) view.findViewById(R.id.mListView);
mListView.setPullLoadEnable(true);
mListView.setXListViewListener(this);
getInfo("刷新");
return view;
}
private void getInfo(final String str) {
// TODO Auto-generated method stub
HttpUtils utils = new HttpUtils();
String url = "http://www.oschina.net/action/api/news_list?catalog=1&pageIndex="
+ i + "&pageSize=10";
utils.send(HttpMethod.GET, url, new RequestCallBack<String>() {
public void onFailure(HttpException arg0, String arg1) {
// TODO Auto-generated method stub
Toast.makeText(getActivity(), "请检查网络", 0).show();
}
@Override
public void onSuccess(ResponseInfo<String> info) {
// 请求成功
String result = info.result;
// 获取流
ByteArrayInputStream bis = new ByteArrayInputStream(result
.getBytes());
// 解析流 通过Pull进行解析
List<NewsList> pullParsesr = MyPullParser.pullParsesr(bis);
if (str.equals("刷新")) {
newsParsesr.addAll(0, pullParsesr);
} else {
newsParsesr.addAll(pullParsesr);
}
// 设置适配器
setAdapter();
}
});
}
private void setAdapter() {
if (adapter == null) {
adapter = new MyAdapter(getActivity(), newsParsesr);
mListView.setAdapter(adapter);
} else {
adapter.notifyDataSetChanged();
}
}
@Override
public void onRefresh() {
// TODO Auto-generated method stub
new Handler().postAtTime(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
i = i + 1;
getInfo("刷新");
onLoder();
}
}, 2000);
}
@Override
public void onLoadMore() {
// TODO Auto-generated method stub
new Handler().postAtTime(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
i = i + 1;
getInfo("加载");
onLoder();
}
}, 2000);
}
private void onLoder() {
mListView.stopRefresh();
mListView.stopLoadMore();
mListView.setRefreshTime("刚刚");
}
}