Android仿新浪微博自定義ListView下拉刷新(4)
自定義PullToRefreshListView繼承ListView,在ListView頭部添加一個下拉的頭部布局。跟ListView用法完全一致。
該自定義Listview代碼詳解具體可參考: http://chabaoo.cn/article/97845.htm
此處詳細介紹Adapter的詳細代碼。
1.首先給Adapter綁定ListView布局。
2.其次創(chuàng)建一個層次對應組件的類,將對應的組件和對象進行關聯(lián),提高效率。
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); //創(chuàng)建一個層次對應組件的類 WeiboHolder wh = new WeiboHolder(); //將對應的組件和對象進行關聯(lián),提高效率 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是軟引用,是為了系統(tǒng)更好的回收變量;從緩存中獲取圖片路徑后,建立新一個新的線程下載圖片。
** * Created by D&LL on 2016/6/2. */ public class AsyncImageLoader { //SoftReference是軟引用,是為了更好的為了系統(tǒng)回收變量 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應用的內存優(yōu)化及Handler的內存泄漏問題
這篇文章主要介紹了Android應用的內存優(yōu)化及Handler的內存泄漏問題,文中對Activity無法被回收而造成的內存泄漏給出了通常的解決方案,需要的朋友可以參考下2016-02-02Android基礎總結篇之三:Activity的task相關介紹
這篇文章主要介紹了Android基礎總結篇之三:Activity的task相關介紹,具有一定的參考價值,有需要的可以了解一下。2016-11-11Android自定義控件案例匯總1(菜單、popupwindow、viewpager)
這篇文章主要介紹了Android自定義控件案例匯總,優(yōu)酷菜單、popupwindow實現(xiàn)下拉列表、viewpager實現(xiàn)輪播圖,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-12-12Android實現(xiàn)一個帶粘連效果的LoadingBar
Loading效果相信大家應該都實現(xiàn)過,最近發(fā)現(xiàn)了一個不錯的效果,決定分享給大家,所以下面這篇文章主要給大家介紹了關于利用Android實現(xiàn)一個帶粘連效果的LoadingBar的相關資料,需要的朋友可以參考借鑒,下面來一起看看吧。2017-12-12解決在eclipse中將android項目生成apk并且給apk簽名的實現(xiàn)方法詳解
本篇文章是對在eclipse中將android項目生成apk并且給apk簽名的實現(xiàn)方法進行了詳細的分析介紹,需要的朋友參考下2013-05-05