亲宝软件园·资讯

展开

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

DeMon辉 人气:0
想了解Android仿新浪微博自定义ListView下拉刷新(4)的相关内容吗,DeMon辉在本文为您仔细讲解Android ListView下拉刷新的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Android,新浪微博,ListView,下拉刷新,下面大家一起来学习吧。

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

该自定义Listview代码详解具体可参考:

此处详细介绍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);
 }
}

效果图:

加载全部内容

相关教程
猜你喜欢
用户评论