Android仿新浪微博自定义ListView下拉刷新(4)

Android仿新浪微博自定义ListView下拉刷新(4),第1张

概述自定义PullToRefreshListView继承ListView,在ListView头部添加一个下拉的头部布局。跟ListView用法完全一致。

自定义PullToRefreshListVIEw继承ListVIEw,在ListVIEw头部添加一个下拉的头部布局。跟ListVIEw用法完全一致。

该自定义ListvIEw代码详解具体可参考: https://www.oudahe.com/p/23152/

此处详细介绍Adapter的详细代码。

1.首先给Adapter绑定ListVIEw布局。
2.其次创建一个层次对应组件的类,将对应的组件和对象进行关联,提高效率。
3.然后跟陆获得的图片路径异步下载图片,由于不知道该微博图片的数量,所以在ListvIEw中添加一个GirlVIEw控件或者GirlLayout布局,然后将得到的图片添加到其中,并按指定属性值排列好。

** * Created by D&LL on 2016/6/2. */public class WeiboAdapter extends BaseAdapter { /**  * 为提高效率,缓存数据准备的一个自定义类 对应一条微博数据  */ class WeiboHolder {  public ImageVIEw wbicon;  public TextVIEw wbtext,wbtime,wbuser; } private HomeActivity homeActivity = null; //数据集 public ArrayList<WeiBoInfo> weiboList = null; public WeiboAdapter(HomeActivity homeActivity,ArrayList<WeiBoInfo> weiboList) {  this.homeActivity = homeActivity;  this.weiboList = weiboList; } //微博图片的异步下载类 AsyncImageLoader asyncImageLoader; @OverrIDe public VIEw getVIEw(int position,VIEw convertVIEw,VIEwGroup parent) {  asyncImageLoader = new AsyncImageLoader();  //记载微博的每条需要显示在什么布局上的布局对象  convertVIEw = LayoutInflater.from(this.homeActivity.getApplicationContext()).inflate(R.layout.ListvIEw,null);  //创建一个层次对应组件的类  WeiboHolder wh = new WeiboHolder();  //将对应的组件和对象进行关联,提高效率  wh.wbicon = (ImageVIEw) convertVIEw.findVIEwByID(R.ID.wbicon);  wh.wbtext = (TextVIEw) convertVIEw.findVIEwByID(R.ID.wbtext);  wh.wbtime = (TextVIEw) convertVIEw.findVIEwByID(R.ID.wbtime);  wh.wbuser = (TextVIEw) convertVIEw.findVIEwByID(R.ID.wbuser);  /* wh.wbimage = (ImageVIEw) convertVIEw.findVIEwByID(R.ID.wbimage);*/  //获得一条微博数据  WeiBoInfo wb = this.weiboList.get(position);  if (wb != null) {   convertVIEw.setTag(wb.getID());   wh.wbuser.setText(wb.getUsername());   wh.wbtime.setText(wb.getTime());   wh.wbtext.setText(StringUtils.getEmotionContent(convertVIEw.getContext(),wh.wbtext,wb.getText()   ),TextVIEw.BufferType.SPANNABLE);   if (wb.getHaveImage()) {    // 是否有图片信息    //异步加载图片内容    Drawable[] wb_image = new Drawable[wb.getimage_context().length];    ImageVIEw[] wbimage = new ImageVIEw[wb.getimage_context().length];    GrIDLayout layout = (GrIDLayout) convertVIEw.findVIEwByID(R.ID.imagelayout);    //遍历下载所有图片 并添加到ListvIEw中    for (int i = 0; i < wb.getimage_context().length; i++) {     wbimage[i] = new ImageVIEw(this.homeActivity.getApplicationContext());     wbimage[i].setpadding(5,5,5);     wbimage[i].setScaleType(ImageVIEw.ScaleType.FIT_XY);     GrIDLayout.Spec row = GrIDLayout.spec(i / 3);     GrIDLayout.Spec colum = GrIDLayout.spec(i % 3);     GrIDLayout.LayoutParams params = new GrIDLayout.LayoutParams(row,colum);     params.setGravity(Gravity.FILL);     params.wIDth = 250;     params.height = 250;     wb_image[i] = asyncImageLoader.loadDrawable(wb.getimage_context()[i],wbimage[i],new AsyncImageLoader.ImageCallback() {        @OverrIDe        public voID imageLoaded(Drawable imageDrawable,ImageVIEw imageVIEw,String imageUrl) {         imageVIEw.setimageDrawable(imageDrawable);        }       });     wbimage[i].setimageDrawable(wb_image[i]);     layout.addVIEw(wbimage[i],params);    }   }   //异步加载用户头像数据   Drawable cachedImage = asyncImageLoader.loadDrawable(wb.getUserIcon(),wh.wbicon,new AsyncImageLoader.ImageCallback() {    @OverrIDe    public voID imageLoaded(Drawable imageDrawable,String imageUrl) {     imageVIEw.setimageDrawable(imageDrawable);    }   });   if (cachedImage == null) {   } else {    wh.wbicon.setimageDrawable(cachedImage);   }  }  return convertVIEw; } @OverrIDe public int getCount() {  return this.weiboList.size(); } @OverrIDe public Object getItem(int position) {  return this.weiboList.get(position); } @OverrIDe public long getItemID(int position) {  return position; }}

异步下载图片的方法。使用SoftReference是软引用,是为了系统更好的回收变量;从缓存中获取图片路径后,建立新一个新的线程下载图片。

** * Created by D&LL on 2016/6/2. */public class AsyncImageLoader { //SoftReference是软引用,是为了更好的为了系统回收变量 private HashMap<String,SoftReference<Drawable>> imageCache; public AsyncImageLoader() {  imageCache = new HashMap<String,SoftReference<Drawable>>(); } public Drawable loadDrawable(final String imageUrl,final ImageVIEw imageVIEw,final ImageCallback imageCallback) {  if (imageCache.containsKey(imageUrl)) {//从缓存中获取   SoftReference<Drawable> softReference = imageCache.get(imageUrl);   Drawable drawable = softReference.get();   if (drawable != null) {    return drawable;   }  }  final Handler handler = new Handler() {   public voID handleMessage(Message message) {    imageCallback.imageLoaded((Drawable) message.obj,imageVIEw,imageUrl);   }  };//建立新一个新的线程下载图片  new Thread() {   @OverrIDe   public voID run() {    Drawable drawable = loadImageFromUrl(imageUrl);    imageCache.put(imageUrl,new SoftReference<Drawable>(drawable));    Message message = handler.obtainMessage(0,drawable);    handler.sendMessage(message);   }  }.start();  return null; } public static Drawable loadImageFromUrl(String url) {  URL m;  inputStream i = null;  try {   m = new URL(url);   i = (inputStream) m.getContent();  } catch (MalformedURLException e1) {   e1.printstacktrace();  } catch (IOException e) {   e.printstacktrace();  }  Drawable d = Drawable.createFromStream(i,"src");  return d; } //回调接口 public interface ImageCallback {  public voID imageLoaded(Drawable imageDrawable,String imageUrl); }}

效果图:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。

总结

以上是内存溢出为你收集整理的Android仿新浪微博自定义ListView下拉刷新(4)全部内容,希望文章能够帮你解决Android仿新浪微博自定义ListView下拉刷新(4)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1141620.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存