public class MainActivity extends AppCompatActivity {
private PullToRefreshListView plv;
private int pageIndex=1;
private int operType=1;//1:刷新 2:加载更多
private String myurl="http://365jia.cn/news/api3/365jia/news/headline?page="+pageIndex;
private List<ResultData.DataBeanX.DataBean> datas=new ArrayList<>();
private ListViewAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
plv = findViewById(R.id.plv);
//初使化
initPlv();
//请求网络数据
requestNetData();
}
private void requestNetData() {
new MyTask().execute(myurl);
}
class MyTask extends AsyncTask<String,Void,String> {
@Override
protected String doInBackground(String... strings) {
try {
URL url=new URL(strings[0]);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setReadTimeout(5000);
urlConnection.setConnectTimeout(5000);
if(urlConnection.getResponseCode()==200){
InputStream inputStream = urlConnection.getInputStream();
String s = streamToString(inputStream);
return s;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
Gson gson=new Gson();
ResultData resultData = gson.fromJson(s, ResultData.class);
List<ResultData.DataBeanX.DataBean> data = resultData.getData().getData();
if(operType==1){
datas.clear();//清空
}
datas.addAll(data);
setAdapter();
//关闭头尾布局
plv.onRefreshComplete();
}
}
public void setAdapter(){
if (adapter == null) {
adapter = new ListViewAdapter(datas,MainActivity.this);
plv.setAdapter(adapter);
}else {
adapter.notifyDataSetChanged();
}
}
/**
* 将流转换成String
* @param stream
*/
private String streamToString(InputStream stream) {
StringBuilder sbuilder=new StringBuilder();
String str;
BufferedReader reader=new BufferedReader(new InputStreamReader(stream));
try {
while ((str=reader.readLine())!=null){
sbuilder.append(str);
}
} catch (Exception e) {
e.printStackTrace();
}
return sbuilder.toString();
}
private void initPlv() {
//设置模式 BOTH:支持上拉加载 下拉刷新 PULL_FROM_END:仅支持加载更多 PULL_FROM_START:下拉刷新
plv.setMode(PullToRefreshBase.Mode.BOTH);
//设置监听事件
plv.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
@Override
public void onPullDownToRefresh(PullToRefreshBase<ListView> pullToRefreshBase) {
//刷新回调
operType=1;
pageIndex=1;
myurl="http://365jia.cn/news/api3/365jia/news/headline?page="+pageIndex;
requestNetData();
}
@Override
public void onPullUpToRefresh(PullToRefreshBase<ListView> pullToRefreshBase) {
//上拉加载更多
operType=2;
pageIndex++;
myurl="http://365jia.cn/news/api3/365jia/news/headline?page="+pageIndex;
requestNetData();
}
});
/*这里通过getLoadingLayoutProxy 方法来指定上拉和下拉时显示的状态的区别,第一个true 代表下来状态 ,第二个true 代表上拉的状态
如果想区分上拉和下拉状态的不同,可以分别设置*/
ILoadingLayout startLabels = plv.getLoadingLayoutProxy(true, false);
startLabels.setPullLabel("下拉刷新一下");
startLabels.setRefreshingLabel("正在玩命加载...");
startLabels.setReleaseLabel("放开我就刷新");
ILoadingLayout endLabels = plv.getLoadingLayoutProxy(false, true);
endLabels.setPullLabel("上拉刷新");
endLabels.setRefreshingLabel("正在载入...");
endLabels.setReleaseLabel("放开刷新...");
}
}