public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view=inflater.inflate(R.layout.fragment_movie1, container, false);
xlistview = view.findViewById(R.id.xlistview);
xlistview.setPullRefreshEnable(true);
xlistview.setPullLoadEnable(true);
xlistview.setXListViewListener(this);
//初始化数据
initData();
return view;
}
private void initData() {
new Thread(){
@Override
public void run() {
super.run();
try {
URL url=new URL("http://172.17.8.100/movieApi/movie/v1/findHotMovieList?page=1&count="+i);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("GET");
httpURLConnection.setConnectTimeout(5000);
httpURLConnection.setReadTimeout(5000);
int responseCode = httpURLConnection.getResponseCode();
if(responseCode==200){
InputStream inputStream = httpURLConnection.getInputStream();
InputStreamReader inputStreamReader=new InputStreamReader(inputStream,"utf-8");
String s = CharStreams.toString(inputStreamReader);
Message message=Message.obtain();
message.what=0;
message.obj=s;
handler.sendMessage(message);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}.start();
}
Handler handler=new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what){
case 0:
String s= (String) msg.obj;
Gson gson=new Gson();
Bean bean = gson.fromJson(s, Bean.class);
ArrayList<Bean.Result> list=bean.getResult();
MyAdapter adapter=new MyAdapter(getActivity(),list);
xlistview.setAdapter(adapter);
close();
break;
case 1:
break;
}
}
};
@Override
public void onRefresh() {
handler.postDelayed(new Runnable() {
@Override
public void run() {
i=3;
initData();
}
},2000);
}
@Override
public void onLoadMore() {
handler.postDelayed(new Runnable() {
@Override
public void run() {
i++;
initData();
}
},2000);
}
public void close(){
xlistview.stopRefresh();
xlistview.stopLoadMore();
xlistview.setRefreshTime(new Date().toString());
}
}
XListView的刷新和加载
最新推荐文章于 2021-05-29 06:34:37 发布