csdnListView上拉加载上拉刷新界面

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("刚刚");
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值