首先关联XListView依赖库,添加网络权限,导入jar包;这些就不用演示了;
一:创建bean类解析网络数据
import java.util.List;
public class RootBean {
public int dataSize;
public List<ApkBean> apk;
@Override
public String toString() {
return "RootBean{" +
"dataSize=" + dataSize +
", apk=" + apk +
'}';
}
public static class ApkBean {
public int type;
public String id;
public String name;
public String iconUrl;
public String downloadUrl;
public String packageName;
public String versionName;
public String versionCode;
public String apkSize;
public String downloadTimes;
public String categoryName;
public String from;
public int markid;
@Override
public String toString() {
return "ApkBean{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", iconUrl='" + iconUrl + '\'' +
", downloadUrl='" + downloadUrl + '\'' +
", packageName='" + packageName + '\'' +
", versionName='" + versionName + '\'' +
", versionCode='" + versionCode + '\'' +
", apkSize='" + apkSize + '\'' +
", downloadTimes='" + downloadTimes + '\'' +
", categoryName='" + categoryName + '\'' +
", from='" + from + '\'' +
", markid=" + markid +
'}';
}
}
}
二:创建ImageLoader工具类
import android.content.Context; import android.graphics.Bitmap; import android.widget.ImageView; import com.nostra13.universalimageloader.core.DisplayImageOptions; import com.nostra13.universalimageloader.core.ImageLoader; import com.nostra13.universalimageloader.core.ImageLoaderConfiguration; import com.nostra13.universalimageloader.core.assist.ImageScaleType; public class Image { public static void setImageView(String url, Context context, ImageView imageView) { //初始化 ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context).build(); //创建Imageloader 对象 ImageLoader imageLoader = ImageLoader.getInstance(); //设置初始化 imageLoader.init(config); DisplayImageOptions options = new DisplayImageOptions.Builder() .showImageOnLoading(R.mipmap.ic_launcher) //设置图片在下载期间显示的图片 .showImageForEmptyUri(R.mipmap.ic_launcher)//设置图片Uri为空或是错误的时候显示的图片 .showImageOnFail(R.mipmap.ic_launcher) //设置图片加载/解码过程中错误时候显示的图片 .cacheInMemory(true)//设置下载的图片是否缓存在内存中 .cacheOnDisk(true)//设置下载的图片是否缓存在SD卡中 .imageScaleType(ImageScaleType.IN_SAMPLE_INT)//设置图片以如何的编码方式显示 .bitmapConfig(Bitmap.Config.RGB_565)//设置图片的解码类型 .build();//构建完成 imageLoader.displayImage(url, imageView, options); //.displayer(new RoundedBitmapDisplayer(90)) 构建圆形图片 } }
三:最后在MainActivity里面写代码
import android.os.AsyncTask; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.support.v7.app.AppCompatActivity; 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.example.mylibrary.XListView; import com.google.gson.Gson; import com.nostra13.universalimageloader.core.ImageLoader; import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity implements XListView.IXListViewListener { private String path = "http://huixinguiyu.cn/Assets/js/data.js"; private List<RootBean.ApkBean> list; private List<RootBean.ApkBean> list2=new ArrayList<>(); private XListView myXListView; private ImageLoader imageLoader; private Handler handler = new Handler() { @Override public void handleMessage(Message msg) { myAdapter.notifyDataSetChanged(); close(); } }; private MyAdapter myAdapter; private RootBean bean; private Image im; private RootBean.ApkBean bean2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); myXListView = (XListView) findViewById(R.id.myXListView_XListView); asyncTask.execute(); myAdapter = new MyAdapter(); myXListView.setAdapter(myAdapter); //开始加载更多 myXListView.setPullLoadEnable(true); //开始下拉刷新 myXListView.setPullRefreshEnable(true); //设置XListView的接口并实现两个方法 myXListView.setXListViewListener(MainActivity.this); im = new Image(); imageLoader = ImageLoader.getInstance(); } AsyncTask<Void, Void, String> asyncTask = new AsyncTask<Void, Void, String>() { //准备工作 @Override protected void onPreExecute() { super.onPreExecute(); } //相当于子线程 @Override protected String doInBackground(Void... params) { try { URL url = new URL(path); //建立一连接对象 HttpURLConnection connection = (HttpURLConnection) url.openConnection(); //设置连接超时时间 connection.setConnectTimeout(5000); //设置读取超时时间 connection.setReadTimeout(5000); //设置请求方式 connection.setRequestMethod("GET"); //获取响应码 并判断是否为200 if (connection.getResponseCode() == 200) { //通过连接对象获取输入流数据 InputStream inputStream = connection.getInputStream(); //需要获取的数据 byte[] b = new byte[1024]; int len=0; ByteArrayOutputStream bos = new ByteArrayOutputStream(); try { while ((len = inputStream.read(b)) != -1) { bos.write(b, 0, len); } String s = bos.toString(); return s; } catch (Exception e) { e.printStackTrace(); } } } catch (Exception e) { e.printStackTrace(); } return null; } //相当于主线程 @Override protected void onPostExecute(String o) { Gson gson = new Gson(); bean = gson.fromJson(o, RootBean.class); list = new ArrayList<>(); list = bean.apk; for (int i = 0; i < list.size(); i++) { bean2 = new RootBean.ApkBean(); if (i%2==0){ bean2.type=0; bean2.id=list.get(i).id; bean2.iconUrl=list.get(i).iconUrl; }else if(i%3==0){ bean2.type=1; bean2.id=list.get(i).id; bean2.iconUrl=list.get(i).iconUrl; }else { bean2.type=2; bean2.id=list.get(i).id; bean2.iconUrl=list.get(i).iconUrl; } list2.add(bean2); } super.onPostExecute(o); } }; //下拉刷新 @Override public void onRefresh() { handler.postDelayed(new Runnable() { @Override public void run() { bean2.id="1111111111111"; list2.add(0,bean2); handler.sendEmptyMessage(0); } },1000); } //上拉加载 @Override public void onLoadMore() { handler.postDelayed(new Runnable() { @Override public void run() { bean2.id="22222222222"; list2.add(bean2); handler.sendEmptyMessage(0); } },1000); } //刷新完关闭的方法 public void close() { myXListView.stopRefresh(); myXListView.stopLoadMore(); } //添加适配器 class MyAdapter extends BaseAdapter { @Override public int getCount() { return list2.size(); } @Override public Object getItem(int position) { return list2.get(position); } @Override public long getItemId(int position) { return position; } @Override public int getViewTypeCount() { return 3; } @Override public int getItemViewType(int position) { Log.d("PY","多少"+list2.get(position).type); return list2.get(position).type; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder1 holder1 = null; ViewHolder2 holder2 = null; ViewHolder3 holder3 = null; int type1 = getItemViewType(position); if (convertView == null) { switch (type1) { case 0: holder1 = new ViewHolder1(); convertView = View.inflate(MainActivity.this, R.layout.item1, null); holder1.item1_image = (ImageView) convertView.findViewById(R.id.item1_image); holder1.item1_id = (TextView) convertView.findViewById(R.id.item1_id); convertView.setTag(holder1); break; case 1: holder2 = new ViewHolder2(); convertView = View.inflate(MainActivity.this, R.layout.item2, null); holder2.item2_image = (ImageView) convertView.findViewById(R.id.item2_image); holder2.item2_id = (TextView) convertView.findViewById(R.id.item2_id); convertView.setTag(holder2); break; case 2: holder3 = new ViewHolder3(); convertView = View.inflate(MainActivity.this, R.layout.item3, null); holder3.item3_image = (ImageView) convertView.findViewById(R.id.item3_image); holder3.item3_id = (TextView) convertView.findViewById(R.id.item3_id); convertView.setTag(holder3); break; } } else { switch (type1) { case 0: holder1 = (ViewHolder1) convertView.getTag(); break; case 1: holder2 = (ViewHolder2) convertView.getTag(); break; case 2: holder3 = (ViewHolder3) convertView.getTag(); break; } } //赋值 switch (type1) { case 0: holder1.item1_id.setText(list2.get(position).id); // imageLoader.displayImage(list.get(position).iconUrl, holder1.item1_image); im.setImageView(list2.get(position).iconUrl,MainActivity.this,holder1.item1_image); break; case 1: holder2.item2_id.setText(list2.get(position).id); // imageLoader.displayImage(list.get(position).iconUrl, holder2.item2_image); im.setImageView(list2.get(position).iconUrl,MainActivity.this,holder2.item2_image); break; case 2: holder3.item3_id.setText(list2.get(position).id); // imageLoader.displayImage(list.get(position).iconUrl, holder3.item3_image); im.setImageView(list2.get(position).iconUrl,MainActivity.this,holder3.item3_image); break; } return convertView; } class ViewHolder1 { ImageView item1_image; TextView item1_id; } class ViewHolder2 { ImageView item2_image; TextView item2_id; } class ViewHolder3 { ImageView item3_image; TextView item3_id; } } }