在这里插入代码片
package frag;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.bawei.week2lx.R;
import com.google.gson.Gson;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import adapter.MyAdapter;
import bean.Datas;
import bean.Json;
import me.maxwin.view.XListView;
/**
*/
public class Frag01 extends Fragment implements XListView.IXListViewListener{
private XListView xlist;
int page =1;
private ArrayList<Datas> data;
private ArrayList<Datas> list;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.frag_01, container, false);
//获取控件
xlist = view.findViewById(R.id.xlist);
//写网络请求
new MyAsyncTask().execute();
xlist.setPullLoadEnable(true);
xlist.setPullRefreshEnable(true);
xlist.setXListViewListener(this);
return view;
}
///下拉获取时间
@Override
public void onRefresh() {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
new MyAsyncTask().execute();
onloud();
}
},2000);
}
//刷新数据
@Override
public void onLoadMore() {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
page++;
new MyAsyncTask().execute();
onloud();
}
},2000);
}
//获取时间的方法
private void onloud() {
xlist.stopRefresh();;
xlist.stopLoadMore();
SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
Date date = new Date(System.currentTimeMillis());
//获取当前时间
String s = format.format(date);
xlist.setRefreshTime(s);
}
private class MyAsyncTask extends AsyncTask<String,Void,String>{
@Override
protected String doInBackground(String... strings) {
try {
URL url = new URL("http://www.xieast.com/api/news/news.php?page="+page);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setConnectTimeout(5000);
urlConnection.setReadTimeout(5000);
if (urlConnection.getResponseCode()==200){
InputStream stream = urlConnection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
StringBuilder builder = new StringBuilder();
String string = "";
while ((string=reader.readLine())!=null){
builder.append(string);
}
return builder.toString();
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
Gson gson = new Gson();
Json fromJson = gson.fromJson(s, Json.class);
data = fromJson.getData();
if (page==1){
list = new ArrayList<>();
}
list.addAll(data);
//设置适配器
MyAdapter myAdapter = new MyAdapter(getActivity(), list);
xlist.setAdapter(myAdapter);
xlist.setSelection(list.size()-29);
}
}
}
XListView上下拉刷新
最新推荐文章于 2019-07-05 17:32:19 发布