pulltorefresh(上拉下拉加载数据) 使用方法!
来源:互联网 发布:注册域名要多少钱 编辑:程序博客网 时间:2024/04/27 19:52
注意:
依赖:compile 'com.github.userswlwork:pull-to-refresh:1.0.0'
xml文件:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.wzq.pulltorefreshdemo.MainActivity">
<com.handmark.pulltorefresh.library.PullToRefreshListView
android:id="@+id/pull_refresh_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:cacheColorHint="#00000000"
android:divider="#19000000"
android:dividerHeight="4dp"
android:fadingEdge="none"
android:fastScrollEnabled="false"
android:footerDividersEnabled="false"
android:headerDividersEnabled="false"
android:smoothScrollbar="true" />
</android.support.constraint.ConstraintLayout>
主代码:
package com.wzq.pulltorefreshdemo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import java.util.ArrayList;
import java.util.List;
/**
* 接入PullToRefresh
* 1.引入Library
* 2.依赖model
* 3. 布局文件中使用可刷新的控件
* 4. 设置可刷新监听--> mPullRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>()
* 设置可以上拉 mPullRefreshListView.setMode(PullToRefreshBase.Mode.BOTH);
*/
public class MainActivity extends AppCompatActivity {
String baseUrl = "http://www.93.gov.cn/93app/data.do?channelId=0&startNum=";
private ArrayList<Product.DataBean> list = new ArrayList();
private PullToRefreshListView plv;
private ArrayAdapter<String> mAdapter;
private ArrayAdapter arrayAdapter;
private int page = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
plv = (PullToRefreshListView) findViewById( R.id.pull_refresh_list);
arrayAdapter = new ArrayAdapter(MainActivity.this,android.R.layout.simple_list_item_1,list);
plv.setAdapter(arrayAdapter);
getNetData(0);
plv.setMode(PullToRefreshBase.Mode.BOTH);//BOTH 设置支持上下拉
plv.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
//下拉刷新
@Override
public void onPullDownToRefresh(PullToRefreshBase<ListView> pullToRefreshBase) {
// 清空集合,
list.clear();
//重新请求数据;更新适配器
getNetData(0);
}
//上拉加载
@Override
public void onPullUpToRefresh(PullToRefreshBase<ListView> pullToRefreshBase) {
//请求数据把请求到的新数据放入大集合;更新适配器
getNetData(page++);
}
});
}
private void getNetData(int page) {
new MAsyncTask().execute(baseUrl+page);
}
class MAsyncTask extends AsyncTask<String,Void,String>{
@Override
protected String doInBackground(String... strings) {
return NetWordUtils.getNetjson(strings[0]);
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
Gson gson = new Gson();
Product product = gson.fromJson(s, Product.class);
List<Product.DataBean> listTemp = product.getData();
list.addAll(listTemp);
arrayAdapter.notifyDataSetChanged();
plv.onRefreshComplete();//关闭刷新头和底部;
}
}
}
- pulltorefresh(上拉下拉加载数据) 使用方法!
- pulltorefresh(上拉下拉加载数据) 使用方法!
- PullToRefresh修改上拉下拉加载动画
- Android pulltorefresh上拉下拉刷新加载
- pulltorefresh上拉下拉
- PulltoRefresh上拉下拉刷新
- 使用PullToRefresh进行上拉下拉刷新数据
- android上拉下拉加载更多数据
- 上拉下拉加载数据的应用.....
- XRecyclerView的上拉下拉加载数据
- Android_XRecyclerView上拉下拉加载数据
- Android_XRecyclerView上拉下拉加载数据
- Android_XRecyclerView上拉下拉加载数据
- Android_XRecyclerView上拉下拉加载数据
- 上拉下拉加载
- 利用PullToRefresh实现上拉下拉
- PullToRefresh的实现上拉下拉刷新
- pullToRefresh使用(实现listview上拉下拉)
- Jenkins RCE CVE-2016-0788分析及利用
- mysql连接
- 【第九周】项目1-二叉树算法库
- 第十一周-项目4
- 意大利输掉比赛的第三天,布冯痛哭后让我们明白这件事
- pulltorefresh(上拉下拉加载数据) 使用方法!
- 月薪3万的程序员都避开了哪些坑
- sql复习2
- 用Rectangle输出一个矩形的周长和面积
- jdbc连接数据库sid、service name tns
- 三个方法来定义Js类(翻译)
- Java安全系列-RSA登录表单加密
- Linux系统升级Python2至Python3
- Okhttp笔记