package com.example.bwie.day9; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.AsyncTask; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.ListView; import android.widget.TextView; import com.example.bwie.day9.News.Bean; import com.example.bwie.day9.view.XListView; import com.google.gson.Gson; import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.util.List; public class MainActivity extends AppCompatActivity implements XListView.IXListViewListener{ XListView lv; MyAdapter adapter; Bean be; List<Bean.ResultBean.DataBean> dateBean; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); lv = (XListView) findViewById(R.id.lv); lv.setPullLoadEnable(true); lv.setXListViewListener(this); getInto(); } protected Bitmap getImg(String path) { Bitmap bit = null; try { URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setConnectTimeout(5000); conn.setReadTimeout(5000); int code = conn.getResponseCode(); if (code == 200) { InputStream is = conn.getInputStream(); bit = BitmapFactory.decodeStream(is); } } catch (Exception e) { e.printStackTrace(); } return bit; } //刷新 @Override public void onRefresh() { getInto(); onLoad(); } public void onLoad() { lv.stopRefresh();//停止刷新 lv.stopLoadMore();//停止加载 lv.setRefreshTime(""); } //加载 @Override public void onLoadMore() { new AsyncTask<String, Integer, String>() { @Override protected String doInBackground(String... strings) { String result=getConn(); return result; } @Override protected void onPostExecute(String result) { Gson gson=new Gson(); be=gson.fromJson(result,Bean.class); List<Bean.ResultBean.DataBean> list=be.getResult().getData(); dateBean.addAll(list); adapter.notifyDataSetChanged(); super.onPostExecute(result); } }.execute(); onLoad(); } public class MyAdapter extends BaseAdapter { public static final int TYPE_ONE = 0; public static final int TYPE_TWO = 1; public static final int TYPE_THREE = 2; @Override public int getCount() { if (be != null) { return dateBean.size(); } return 0; } @Override public int getItemViewType(int position) { if (dateBean.get(position).getThumbnail_pic_s03()!=null) { return TYPE_ONE; } else if (dateBean.get(position).getThumbnail_pic_s02()!=null) { return TYPE_TWO; } else { return TYPE_THREE; } } @Override public int getViewTypeCount() { return 3; } @Override public Object getItem(int i) { return null; } @Override public long getItemId(int i) { return 0; } @Override public View getView(final int i, View view, ViewGroup viewGroup) { ViewHolder holder = null; ViewHolder1 holder1 = null; ViewHolder2 holder2 = null; int c = getItemViewType(i); if (view == null) { if (c == 0) { view = View.inflate(MainActivity.this, R.layout.list_item, null); holder = new ViewHolder(); holder.tx1 = (TextView) view.findViewById(R.id.tx1); holder.im1 = (ImageView) view.findViewById(R.id.im1); holder.im2 = (ImageView) view.findViewById(R.id.im2); holder.im3 = (ImageView) view.findViewById(R.id.im3); view.setTag(holder); }else if (c == 1) { view = View.inflate(MainActivity.this, R.layout.list_item1, null); holder1 = new ViewHolder1(); holder1.tv1 = (TextView) view.findViewById(R.id.tv1); holder1.iv1 = (ImageView) view.findViewById(R.id.iv1); holder1.iv2 = (ImageView) view.findViewById(R.id.iv2); view.setTag(holder1); } else { view = View.inflate(MainActivity.this, R.layout.list_item2, null); holder2 = new ViewHolder2(); holder2.tt1 = (TextView) view.findViewById(R.id.tt1); holder2.imv1 = (ImageView) view.findViewById(R.id.imv1); view.setTag(holder2); } } else { if (c == 0) { holder = (ViewHolder) view.getTag(); } else if (c == 1) { holder1 = (ViewHolder1) view.getTag(); } else { holder2 = (ViewHolder2) view.getTag(); } } if(c==0) { holder.tx1.setText(dateBean.get(i).getTitle()); final ViewHolder finalHolder = holder; new Thread(new Runnable() { @Override public void run() { final Bitmap img = getImg(dateBean.get(i).getThumbnail_pic_s()); final Bitmap img1 = getImg(dateBean.get(i).getThumbnail_pic_s02()); final Bitmap img2 = getImg(dateBean.get(i).getThumbnail_pic_s03()); runOnUiThread(new Runnable() { @Override public void run() { finalHolder.im1.setImageBitmap(img); finalHolder.im2.setImageBitmap(img1); finalHolder.im3.setImageBitmap(img2); } }); } }).start(); } else if(c==1) { holder1.tv1.setText(dateBean.get(i).getTitle()); final ViewHolder1 finalHolder1 = holder1; new Thread(new Runnable() { @Override public void run() { final Bitmap img = getImg(dateBean.get(i).getThumbnail_pic_s()); final Bitmap img1 = getImg(dateBean.get(i).getThumbnail_pic_s02()); runOnUiThread(new Runnable() { @Override public void run() { finalHolder1.iv1.setImageBitmap(img); finalHolder1.iv2.setImageBitmap(img1); } }); } }).start(); } else { holder2.tt1.setText(dateBean.get(i).getTitle()); final ViewHolder2 finalHolder2 = holder2; new Thread(new Runnable() { @Override public void run() { final Bitmap img = getImg(dateBean.get(i).getThumbnail_pic_s()); runOnUiThread(new Runnable() { @Override public void run() { finalHolder2.imv1.setImageBitmap(img); } }); } }).start(); } return view; } } class ViewHolder{ TextView tx1; ImageView im1,im2,im3; } class ViewHolder1{ TextView tv1; ImageView iv1,iv2; } class ViewHolder2 { TextView tt1; ImageView imv1; } public Object getInto() { new AsyncTask<String, Integer, String>() { @Override protected String doInBackground(String... strings) { String result=getConn(); return result; } @Override protected void onPostExecute(String result) { Gson gson=new Gson(); be=gson.fromJson(result,Bean.class); dateBean =be.getResult().getData(); adapter = new MyAdapter(); lv.setAdapter(adapter); super.onPostExecute(result); } }.execute(); return null; } public String getConn() { String result=""; String path="http://v.juhe.cn/toutiao/index?type=top&key=2f092bd9ce76c0257052d6d3c93c11b4"; try { URL url=new URL(path); HttpURLConnection conn= (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setConnectTimeout(5000); conn.setReadTimeout(5000); int code=conn.getResponseCode(); if(code==200){ InputStream is=conn.getInputStream(); ByteArrayOutputStream bos=new ByteArrayOutputStream(); int leng=-1; byte[] buff=new byte[1024]; while((leng=is.read(buff))!=-1){ bos.write(buff,0,leng); } result=new String(bos.toByteArray()); } } catch (Exception e) { e.printStackTrace(); } return result; } }
XlistView展示
最新推荐文章于 2021-05-26 09:52:46 发布