效果图:
第一步:设计
先实现图文加载到列表
public class MainActivity extends AppCompatActivity { private ListView listView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listView = (ListView) findViewById(R.id.listview); StringTask task = new StringTask(listView,MainActivity.this); String url = String.format(Contants.BK_URL,1); task.execute(url); }}
public class StringTask extends AsyncTask> { private ListView listview; private Context context; public StringTask(ListView listview, Context context) { this.listview = listview; this.context = context; } @Override protected List doInBackground(String... params) { String jsonStr = HttpUtils.getJSONStr(params[0]); List list = JSONUtils.parseJSON(jsonStr); return list; } @Override protected void onPostExecute(List teases) { TeasAdapter adapter = new TeasAdapter(teases,context); listview.setAdapter(adapter); super.onPostExecute(teases); }}
public class TeasAdapter extends BaseAdapter { private Listlist; private Context context; public TeasAdapter(List list, Context context) { this.list = list; this.context = context; } @Override public int getCount() { return list.size(); } @Override public Object getItem(int position) { return list.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { LayoutInflater inflater = LayoutInflater.from(context); View view = inflater.inflate(R.layout.list_item, null); ViewHolder viewHolder = new ViewHolder(); viewHolder.title = (TextView) view.findViewById(R.id.title); viewHolder.create_time = (TextView) view.findViewById(R.id.creat_time); viewHolder.image = (ImageView) view.findViewById(R.id.image); Teas teas = list.get(position); viewHolder.title.setText(teas.getTitle()); viewHolder.create_time.setText(teas.getCreate_time()); //下载图片并赋值 String image_url = teas.getWap_thumb(); ImageTask task = new ImageTask(viewHolder.image); task.execute(image_url); return view; } public class ViewHolder{ TextView title,create_time; ImageView image; }}
public class HttpUtils { //下载数据 public static String getJSONStr(String path){ try { URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); int code = conn.getResponseCode(); if(code == 200){ InputStream is = conn.getInputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int ret; while((ret = is.read(buffer))>0){ baos.write(buffer,0,ret); } return baos.toString(); } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } //下载图片 public static Bitmap getBitmap(String path){ try { URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); int code = conn.getResponseCode(); if(code == 200){ InputStream is = conn.getInputStream(); return BitmapFactory.decodeStream(is); } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; }}
public class JSONUtils { //解析json字符串 public static ListparseJSON(String jsonStr){ List list = new ArrayList (); try { JSONObject object = new JSONObject(jsonStr); JSONArray array = (JSONArray) object.get("data"); for(int i = 0;i
public class ImageTask extends AsyncTask{ private ImageView image; public ImageTask(ImageView image) { this.image = image; } @Override protected Bitmap doInBackground(String... params) { Bitmap bitmap = HttpUtils.getBitmap(params[0]); return bitmap; } @Override protected void onPostExecute(Bitmap bitmap) { image.setImageBitmap(bitmap); super.onPostExecute(bitmap); }}
第二步:滑动复用内存空间
优化TeasAdapter的getView方法
@Overridepublic View getView(int position, View convertView, ViewGroup parent) { ViewHolder viewHolder; if(convertView == null){ LayoutInflater inflater = LayoutInflater.from(context); convertView = inflater.inflate(R.layout.list_item, null); viewHolder = new ViewHolder(); viewHolder.title = (TextView) convertView.findViewById(R.id.title); viewHolder.create_time = (TextView) convertView.findViewById(R.id.creat_time); viewHolder.image = (ImageView) convertView.findViewById(R.id.image); convertView.setTag(viewHolder); } viewHolder = (ViewHolder) convertView.getTag(); Teas teas = list.get(position); viewHolder.title.setText(teas.getTitle()); viewHolder.create_time.setText(teas.getCreate_time()); //下载图片并赋值 String image_url = teas.getWap_thumb(); ImageTask task = new ImageTask(viewHolder.image); task.execute(image_url); return convertView;}
第三步:滑动分页
优化MainActivity和StringTask类且在TeasAdapter中增加addData方法
public class MainActivity extends AppCompatActivity { private TeasAdapter adapter; private ListView listView; private boolean flag; private int index = 1; private Listlist = new ArrayList (); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listView = (ListView) findViewById(R.id.listview); adapter = new TeasAdapter(list,MainActivity.this); listView.setAdapter(adapter); StringTask task = new StringTask(adapter); String url = String.format(Contants.BK_URL,index); task.execute(url); //滑动分页 listView.setOnScrollListener(new AbsListView.OnScrollListener() { @Override public void onScrollStateChanged(AbsListView view, int scrollState) { if (flag) { index++; String new_url = String.format(Contants.BK_URL, index); StringTask new_task = new StringTask(adapter); new_task.execute(new_url); } } @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { if (firstVisibleItem + visibleItemCount == totalItemCount) { flag = true; } else { flag = false; } } }); }}
public class StringTask extends AsyncTask> { private TeasAdapter adapter; public StringTask(TeasAdapter adapter) { this.adapter = adapter; } @Override protected List doInBackground(String... params) { String jsonStr = HttpUtils.getJSONStr(params[0]); List list = JSONUtils.parseJSON(jsonStr); return list; } @Override protected void onPostExecute(List teases) { adapter.addData(teases); super.onPostExecute(teases); }}
public void addData(Listteas){ list.addAll(teas); notifyDataSetChanged(); }
第四步:保存图片加缓存,保存到sd卡且防止图片加载错位
(先在缓存查找图片,再去sdcard查找,如果没有,网络下载,然后保存到sdcard,最后保存到缓存)
优化TeasAdapter和ImageTask类
public class TeasAdapter extends BaseAdapter { private Listlist; private Context context; private LruCache cache; public TeasAdapter(List list, Context context) { this.list = list; this.context = context; long maxMemory = Runtime.getRuntime().maxMemory(); cache = new LruCache ((int) (maxMemory/8)){ @Override protected int sizeOf(String key, Bitmap value) { return value.getByteCount()*value.getHeight(); } }; } @Override public int getCount() { return list.size(); } @Override public Object getItem(int position) { return list.get(position); } @Override public long getItemId(int position) { return position; } //滑动复用内存空间 @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder viewHolder; if(convertView == null){ LayoutInflater inflater = LayoutInflater.from(context); convertView = inflater.inflate(R.layout.list_item, null); viewHolder = new ViewHolder(); viewHolder.title = (TextView) convertView.findViewById(R.id.title); viewHolder.create_time = (TextView) convertView.findViewById(R.id.creat_time); viewHolder.image = (ImageView) convertView.findViewById(R.id.image); convertView.setTag(viewHolder); } viewHolder = (ViewHolder) convertView.getTag(); Teas teas = list.get(position); viewHolder.title.setText(teas.getTitle()); viewHolder.create_time.setText(teas.getCreate_time()); //下载图片并赋值 String image_url = teas.getWap_thumb(); Bitmap bitmap = cache.get("image_url"); //判断是否在缓存中 if(bitmap!=null){ viewHolder.image.setImageBitmap(bitmap); }else{ viewHolder.image.setTag(image_url); ImageTask task = new ImageTask(viewHolder.image,context,cache); task.execute(image_url); } return convertView; } public class ViewHolder{ TextView title,create_time; ImageView image; } public void addData(List teas){ list.addAll(teas); notifyDataSetChanged(); }}
public class ImageTask extends AsyncTask{ private String image_url; private ImageView image; private Context context; private LruCache cache; public ImageTask(ImageView image, Context context, LruCache cache) { this.image = image; this.context = context; this.cache = cache; } @Override protected Bitmap doInBackground(String... params) { image_url = params[0]; File file = context.getCacheDir(); File file2 = new File(file,image_url); //判断是否在sdcard中 if(file2.exists()){ return BitmapFactory.decodeFile(file2.getAbsolutePath()); }else{ Bitmap bitmap = HttpUtils.getBitmap(image_url); try { //保存到sdcard中 FileOutputStream fos = new FileOutputStream(file2); bitmap.compress(Bitmap.CompressFormat.JPEG,100,fos); } catch (FileNotFoundException e) { e.printStackTrace(); } return bitmap; } } @Override protected void onPostExecute(Bitmap bitmap) { if(bitmap!=null){ //保存到内存中 cache.put(image_url, bitmap); String url = (String) image.getTag(); if(url.equals(image_url)){ image.setImageBitmap(bitmap); } } super.onPostExecute(bitmap); }}