PullToRefreshListView上拉加载下拉刷新
Posted 巫山老妖
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PullToRefreshListView上拉加载下拉刷新相关的知识,希望对你有一定的参考价值。
说明:此项目使用studio完成的、需要导入library作为依赖,使用了xuitls获得网络请求、使用Pull解析了XML
//布局文件
<com.handmark.pulltorefresh.library.PullToRefreshListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
package com.bw.com.yuekao1;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.util.Xml;
import android.widget.ListView;
import android.widget.Toast;
import com.bw.com.yuekao1.com.bw.yuekao1.bean.News;
import com.bw.com.yuekao1.com.bw.yuekao1.bean.com.bw.yuekao1.adapter.MyAdapter;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
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;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
//封装结合
private List<News> news=new ArrayList<News>();
//PullToRefreshListView用于上拉加载、上拉刷新
private PullToRefreshListView lv;
private MyAdapter adapter;
//定义一个页数,分页加载
private int ye=1;
//使用Handler更新UI
private Handler handler=new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case 0:
//获得线程发来的数据
String text=(String) msg.obj;
//XML解析
toXml(text);
break;
default:
break;
}
}
};
private void toXml(String text) {
News newss=null;
try {
//将读取到的结果码 添加到XML里面
XmlPullParser Parser = Xml.newPullParser();
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(text.getBytes());
Parser.setInput(byteArrayInputStream,"utf-8");
//通过parse获得项目类型 相当于文件里面的属性吧
int eventType = Parser.getEventType();
//判断
while ((eventType != XmlPullParser.END_DOCUMENT)) {
String name = Parser.getName();
switch (eventType) {
case XmlPullParser.START_TAG:
if (name.equals("news")) {
newss = new News();
} else if (name.equals("id")) {
String id = Parser.nextText();
newss.setId(id);
} else if (name.equals("title")) {
String title = Parser.nextText();
newss.setTitle(title);
} else if (name.equals("body")) {
String body = Parser.nextText();
newss.setBody(body);
} else if (name.equals("commentCount")) {
String commentCount = Parser.nextText();
newss.setCommentCount(commentCount);
}
else if (name.equals("author")) {
String author = Parser.nextText();
newss.setAuthor(author);
}
else if (name.equals("pubDate")) {
String pubDate = Parser.nextText();
newss.setPubDate(pubDate);
}
break;
case XmlPullParser.END_TAG:
if (name.equals("news")) {
news.add(newss);
//更新适配器
if (adapter == null) {
adapter = new MyAdapter(news,this);
lv.setAdapter(adapter);
} else {
adapter .notifyDataSetChanged();
}
}
break;
default:
break;
}
eventType=Parser.next();
}
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
};
//-------------onCreate中--------------
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//找到PullToRefreshListView的控件
lv= (PullToRefreshListView) findViewById(R.id.list_view);
//获得网络数据-传一个页数
huodeshuju(ye);
//设置适配器
adapter = new MyAdapter(news,this);
lv.setAdapter(adapter);
//设置PullToRefreshListView的模式,BOTH时,既可以刷新也可以加载
lv.setMode(PullToRefreshBase.Mode.BOTH);
//设置刷新的方法
lv.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
@Override//下拉刷新
public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
//重新定义为第一页
ye=1;
Toast.makeText(MainActivity.this,""+ye,Toast.LENGTH_SHORT).show();
//获得数据
huodeshuju(ye);
//刷新适配器
adapter.notifyDataSetChanged();
//创建一个方法结束刷新
new FinishRefresh().execute();
}
@Override//上拉加载更多
public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
//使页数加1
ye++;
Toast.makeText(MainActivity.this,""+ye,Toast.LENGTH_SHORT).show();
huodeshuju(ye);
adapter.notifyDataSetChanged();
new FinishRefresh().execute();
}
});
}
//使用的是HttpUtils请求网络数据——需要导入一个xuitls.jar包
private void huodeshuju(int ye) {
//创建HttpUtils
HttpUtils httpUtils=new HttpUtils();
//发送GET请求
httpUtils.send(HttpRequest.HttpMethod.GET, "http://www.oschina.net/action/api/news_list?catalog=1&pageIndex="+ye+"&pageSize=20", new RequestCallBack<String>() {
@Override//请求成功的方法
public void onSuccess(ResponseInfo<String> responseInfo) {
String str=responseInfo.result;
Message message=new Message();
message.what=0;
message.obj=str;
handler.sendMessage(message);
}
//请求失败的方法
@Override
public void onFailure(HttpException e, String s) {
}
});
}
//完成刷新的方法
private class FinishRefresh extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
return null;
}
@Override
protected void onPostExecute(Void result){
//刷新完成
lv.onRefreshComplete();
}
}
}
以上是关于PullToRefreshListView上拉加载下拉刷新的主要内容,如果未能解决你的问题,请参考以下文章