Android第三方PullToRefresh实现下拉刷新上拉加载使用教程。。。
程序员文章站
2024-01-17 19:55:40
...
介绍 :
PullToRefresh是一个非常好的刷新加载第三方库可以实现以下几种展示控件的刷新及加载:
1.ListView
2.ExpandableListView
3.GridView
4.WebView
PullToRefresh框架下载地址:https://download.csdn.net/download/qq_26464451/10315370(记得加依赖匹配版本)
这里有控件使用总结+https://blog.csdn.net/chenrushui/article/details/51394735
控件:
<com.handmark.pulltorefresh.library.PullToRefreshListView
android:id="@+id/prl"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
代码:
private int page = 1;
Handler handler = new Handler();
String path = "http://api.expoon.com/AppNews/getNewsList/type/1/p/";
private List<JsonBean.DataBean> list;
private ArrayList<JsonBean.DataBean> arrayList;
@Override
protected void initData() {
//设置可刷新加载
prl.setPullToRefreshEnabled(true);
//设置模式
prl.setMode(PullToRefreshListView.Mode.BOTH);
//监听
prl.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
@Override
public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
page = 1;
getJsonData(path + page);
handler.postDelayed(new Runnable() {
@Override
public void run() {
prl.onRefreshComplete();
}
}, 2000);
}
@Override
public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
page++;
getJsonData(path + page);
handler.postDelayed(new Runnable() {
@Override
public void run() {
prl.onRefreshComplete();
}
}, 2000);
}
});
getJsonData(path + page);
}
private void getJsonData(String path) {
MyAsyncTask myAsyncTask = new MyAsyncTask();
myAsyncTask.execute(path);
}
private class MyAsyncTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... strings) {
String uri = strings[0];
try {
URL url = new URL(uri);
//打开链接
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//请求方式
conn.setRequestMethod("GET");
//判断状态码
if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
InputStream inputStream = conn.getInputStream();
//设字节输出流
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while ((len = inputStream.read(buffer)) != -1) {
bos.write(buffer, 0, len);
}
//关流
inputStream.close();
bos.close();
String json = bos.toString();
return json;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String json) {
super.onPostExecute(json);
Log.i("xxx", "onPostExecute: " + json);
//解析
Gson gson = new Gson();
JsonBean jsonBean = gson.fromJson(json, JsonBean.class);
list = jsonBean.getData();
if (page == 1) {
arrayList = new ArrayList<>();
}
arrayList.addAll(list);
//展示
MyAdapter myAdapter = new MyAdapter(arrayList, getActivity());
prl.setAdapter(myAdapter);
prl.setSelection(myAdapter.getCount() - 19);
}
}
适配器展示多条目展示+ https://blog.csdn.net/weixin_43917449/article/details/85798278
推荐阅读
-
Android第三方PullToRefresh实现下拉刷新上拉加载使用教程。。。
-
在React中使用Native如何实现自定义下拉刷新上拉加载的列表
-
iOS实现MJRefresh下拉刷新(上拉加载)使用详解
-
Android实现上拉加载更多以及下拉刷新功能(ListView)
-
Android RecyclerView实现下拉刷新和上拉加载更多
-
Android RecyclerView实现下拉刷新和上拉加载更多
-
Android使用ListView实现下拉刷新及上拉显示更多的方法
-
Android控件PullRefreshViewGroup实现下拉刷新和上拉加载
-
Android使用ListView实现下拉刷新及上拉显示更多的方法
-
Android控件PullRefreshViewGroup实现下拉刷新和上拉加载