//
添加权限 判断网络
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"></uses-permission>
//
XlistView 主要逻辑
package com.example.zhangt.elevent;
import android.icu.text.SimpleDateFormat;
import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.bwie.xlistviewlibrary.View.XListView;
import com.example.zhangt.elevent.Bean.Result;
import com.example.zhangt.elevent.Utils.ImageloaderUtil;
import com.example.zhangt.elevent.Utils.MyTask;
import com.google.gson.Gson;
import com.nostra13.universalimageloader.core.ImageLoader;
import java.sql.Date;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
Handler myHandler=new Handler(){
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if(msg.what==1){
//关闭底部视图
XListView.stopLoadMore();
}else{
Date date = new Date(System.currentTimeMillis());
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");
String format = simpleDateFormat.format(date);
//设置时间
XListView.setRefreshTime(format);
//关闭头部视图
XListView.stopRefresh();
}
}
};
//
private List<Result.DataBean> datas=new ArrayList<>();
private XListView XListView;
private int pageindex=1;
private myAdapter myAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
XListView = (XListView) findViewById(R.id.xlv);
XListView.setPullLoadEnable(true);
//支持加载更多
XListView.setPullLoadEnable(true);
XListView.setXListViewListener(new XListView.IXListViewListener() {
@Override
public void onRefresh() {//刷新回调的接口
//请求第一页的数据
MyTask myTask=new MyTask(new MyTask.Icallbacks() {
@Override
public void updateUiByjson(String jsonstr) {
Gson gson=new Gson();
Result result = gson.fromJson(jsonstr, Result.class);
//添加到大集合对象中
datas.addAll(result.getData());
//设置适配器
setAdapter();
//延迟关闭头部视图
myHandler.sendEmptyMessageDelayed(2,500);
}
});
myTask.execute("http://www.yulin520.com/a2a/impressApi/news/mergeList?pageSize=10&page=1");
}
/
public void onLoadMore() {//加载更多回调的接口方法
pageindex++;
Log.d("zzz","*************");
MyTask myTask=new MyTask(new MyTask.Icallbacks() {
@Override
public void updateUiByjson(String jsonstr) {
Log.d("zzz",jsonstr);
Gson gson=new Gson();
Result result = gson.fromJson(jsonstr, Result.class);
//添加到大集合对象中
datas.addAll(result.getData());
//设置适配器
setAdapter();
//关闭底部视图
myHandler.sendEmptyMessageDelayed(1,500);
}
});
myTask.execute("http://www.yulin520.com/a2a/impressApi/news/mergeList?pageSize=10&page="+pageindex);
}
});
//初使化
requestNetData();
}
///
private void requestNetData() {
MyTask myTask=new MyTask(new MyTask.Icallbacks() {
@Override
public void updateUiByjson(String jsonstr) {
Gson gson=new Gson();
Result result = gson.fromJson(jsonstr, Result.class);
//添加到大集合对象中
datas.addAll(result.getData());
//设置适配器
setAdapter();
}
});
myTask.execute("http://www.yulin520.com/a2a/impressApi/news/mergeList?pageSize=10&page=1");
}
///
public void setAdapter(){
if(myAdapter==null){
myAdapter=new myAdapter();
XListView.setAdapter(myAdapter);
}else{
myAdapter.notifyDataSetChanged();
}
}
/
class myAdapter extends BaseAdapter{
@Override
public int getCount() {
return datas.size();
}
@Override
public Object getItem(int position) {
return datas.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView==null){
convertView=View.inflate(MainActivity.this,R.layout.xlv_one,null);
holder=new ViewHolder();
holder.tv=convertView.findViewById(R.id.text_title);
holder.im=convertView.findViewById(R.id.image_view);
convertView.setTag(holder);
}else{
holder=(ViewHolder) convertView.getTag();
}
holder.tv.setText(datas.get(position).getTitle());
ImageLoader.getInstance().displayImage(datas.get(position).getImg(),holder.im,ImageloaderUtil.getImageOptions());
return convertView;
}
}
//
class ViewHolder{
TextView tv;
ImageView im;
}
//
}