xlistview下拉刷新,上拉加载
来源:互联网 发布:淘宝抠图软件哪个好用 编辑:程序博客网 时间:2024/06/01 19:57
1. 下载gitHub上XlistView上的压缩包
. 复制view包下的三个类,,,,,注意导包的问题
.赋值layout下面的footer布局和header的布局
.赋值string下面的字段值
2. oncreate方法中
xListView.setPullRefreshEnable(true);//支持下拉刷新
xListView.setPullLoadEnable(true);//支持上拉加载更多
xListView.setXListViewListener(this);//设置xlistView的监听事件
getDataFromNet();
3.设置首次加载的数据
private void getDataFromNet() {
AsyncTask<Void, Void, String> asyncTask = new AsyncTask<Void, Void, String>() {
@Override
protected String doInBackground(Void... voids) {
String path = "http://www.yulin520.com/a2a/impressApi/news/mergeList?pageSize=10&page=1";
try {
URL url = new URL(path);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//设置
connection.setRequestMethod("GET");
connection.setReadTimeout(5000);
connection.setConnectTimeout(5000);
//获取
int responseCode = connection.getResponseCode();
if (responseCode == 200){
InputStream inputStream = connection.getInputStream();
String json = streamToString(inputStream,"utf-8");
return json;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String s) {
Gson gson = new Gson();
DataDataBean dataDataBean = gson.fromJson(s, DataDataBean.class);
//将解析到的集合数据添加到上面的大集合中
list.addAll(dataDataBean.getData());
//设置适配器...
setAdapter();
//上拉加载完成....停止加载
xListView.stopLoadMore();
}
};
asyncTask.execute();
}
4.设置适配器
private void setAdapter() {
if (myAdapter == null){
myAdapter = new MyAdapter(MainActivity.this, list);
xListView.setAdapter(myAdapter);
}else {
myAdapter.notifyDataSetChanged();
}
}
5.监听事件的实现方法中,刷新方法中,集合添加到最前,上拉加载中,集合添加到最后
@Override
public void onRefresh() {
page_num ++;
AsyncTask<Void, Void, String> asyncTask = new AsyncTask<Void, Void, String>() {
@Override
protected String doInBackground(Void... voids) {
String path = "http://www.yulin520.com/a2a/impressApi/news/mergeList?pageSize=10&page="+page_num;
try {
URL url = new URL(path);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//设置
connection.setRequestMethod("GET");
connection.setReadTimeout(5000);
connection.setConnectTimeout(5000);
//获取
int responseCode = connection.getResponseCode();
if (responseCode == 200){
InputStream inputStream = connection.getInputStream();
String json = streamToString(inputStream,"utf-8");
return json;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String s) {
Gson gson = new Gson();
DataDataBean dataDataBean = gson.fromJson(s, DataDataBean.class);
//下拉刷新的数据需要添加在大集合的最前边
list.addAll(0,dataDataBean.getData());
//设置适配器...
setAdapter();
//...............设置完数据之后刷新需要停止
xListView.stopRefresh();//停止刷新
//System.currentTimeMillis()....当前时间的long类型的值
Date date = new Date(System.currentTimeMillis());
//格式化....yyyy-MM-dd HH:mm
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");
//设置本次刷新的时间
xListView.setRefreshTime(simpleDateFormat.format(date));
}
};
asyncTask.execute();
}
/**
* 这是上拉加载的时候调用的方法
*
* 上拉加载更多......例如:让他请求第一页数据
*/
@Override
public void onLoadMore() {
//再次调用
getDataFromNet();
}
- XlistView上拉刷新下拉加载
- xListView上拉刷新下拉加载
- xListView上拉加载下拉刷新
- XListView上拉加载,下拉刷新
- xlistview上拉加载下拉刷新
- XListView上拉加载下拉刷新
- XListView上拉刷新,下拉加载
- 上拉刷新--下拉加载XListView
- XListView上拉加载下拉刷新
- xlistview上拉刷新、下拉加载
- XListView上拉加载下拉刷新
- XListView上拉加载下拉刷新
- XListView上拉加载下拉刷新
- Xlistview上拉加载下拉刷新
- xlistview上拉刷新,下拉加载
- 上拉刷新下拉加载xlistview-------string
- 上拉刷新下拉加载xlistview--------activity_main
- XlistView的上拉刷新下拉加载
- C# 跨线程调用控件
- ubuntu14.04安装QT及相关开发环境
- c# 线程池
- CS231N-8&9-Deep Learning Hard/Software & CNN Case Study
- 关于iOS11 应用内评分 跳转链接失效问题
- xlistview下拉刷新,上拉加载
- java创建http接口
- loadrunner录制webtours的一个例子(未完待续)
- Cron表达式
- Android性能优化
- Pandas——DataFrame函数使用
- gradle版本问题——解决Minimum supported Gradle version is 3.3. Current version is 2.14.1
- 数据结构:实验二线性表综合实验③双链表
- Jeesite增加spring-data-redis缓存功能以及相关配置