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

所属分类: 软件编程 / Android 阅读数: 57
收藏 0 赞 0 分享

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

该自定义Listview代码详解具体可参考: https://www.jb51.net/article/97845.htm

此处详细介绍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, 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, ImageView imageView, 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, ImageView imageView, String imageUrl);
 }
}

效果图:

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

更多精彩内容其他人还在看

android byte[] 和short[]转换的方法代码

这篇文章主要介绍了android byte[] 和short[]转换的方法代码,有需要的朋友可以参考一下
收藏 0 赞 0 分享

Android获取应用程序大小的方法

这篇文章主要介绍了Android获取应用程序大小的方法,有需要的朋友可以参考一下
收藏 0 赞 0 分享

Android获取其他包的Context实例代码

这篇文章主要介绍了Android获取其他包的Context实例代码,有需要的朋友可以参考一下
收藏 0 赞 0 分享

Android放大镜的实现代码

这篇文章主要介绍了Android放大镜的实现代码,有需要的朋友可以参考一下
收藏 0 赞 0 分享

Android 读取Properties配置文件的小例子

这篇文章主要介绍了Android 读取Properties配置文件的小例子,有需要的朋友可以参考一下
收藏 0 赞 0 分享

Android通讯录开发之删除功能的实现方法

这篇文章主要介绍了Android通讯录开发之删除功能的实现方法,有需要的朋友可以参考一下
收藏 0 赞 0 分享

使用ViewPager实现android软件使用向导功能实现步骤

现在的大部分android软件,都是使用说明,就是第一次使用该软件时,会出现向导,可以左右滑动,然后就进入应用的主界面了,下面我们就实现这个功能
收藏 0 赞 0 分享

android在异步任务中关闭Cursor的代码方法

android在异步任务中如何关闭Cursor?在我们开发应用的时候,很多时候会遇到这种问题,下面我们就看看代码如何实现
收藏 0 赞 0 分享

Android自定义桌面功能代码实现

android自定义桌面其实很简单,看一个例子就明白了
收藏 0 赞 0 分享

android将图片转换存到数据库再从数据库读取转换成图片实现代码

有时候我们想把图片存入到数据库中,尽管这不是一种明智的选择,但有时候还是不得以会用到,下面说说将图片转换成byte[]数组存入到数据库中去,并从数据库中取出来转换成图像显示出来
收藏 0 赞 0 分享
查看更多