閱讀39 返回首頁    go 阿裏雲 go 技術社區[雲棲]


ANDROID中自定義ADAPTER實現LISTVIEW動態刷新進度條

https://www.cnblogs.com/xiaoQLu/archive/2011/05/10/2042124.html

這幾天做上傳圖片時,實現動態更新進度條,花費了我不少腦筋,一是android不是很熟悉,二是自己java基礎,或者說是編程基礎不紮實,不會學以致用,這兩發塊,是以後加強的重點!

  費話不多說!說說我用到的幾個知識,一是AsyncTask,實現異步上傳,二是自定義Adapter,繼承自BaseAdapter,activity使用的是ListActivity(這是費話哈……)

   這個小美女還不錯哈!

主代碼:

  前麵布局文件啊,獲得組件啊,什麼的就不寫了……(這幾天有點忙,代碼沒抽出來,有時間把代碼貼出來……)

  繼續自ArrayAdapter,主要看他重寫的getView()方法,其中這個imageLoader.loadDrawable()方法的調用就是動態刷新的重點,在方法的參數中用接口來實現了一個回調函數,看到這個想法,不僅讓我拍案叫絕啊(這個想法是在網上看到的),java中一直苦於不能像c++一樣傳函數,現在用接口完美解決。

publicclass MyImageAndTextListAdapter extends ArrayAdapter<NewsBean> {

public MyImageAndTextListAdapter(Activity activity,
List<NewsBean> newsList) {
super(activity, 0, newsList);
}

private AsyncImageLoader imageLoader =new AsyncImageLoader();

private Map<Integer, View> viewMap =new HashMap<Integer, View>();

@Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView =this.viewMap.get(position);

if (rowView ==null) {
LayoutInflater inflater = ((Activity) this.getContext())
.getLayoutInflater();
rowView = inflater.inflate(R.layout.news_row, null);

NewsBean newsBean =this.getItem(position);

TextView textView = (TextView) rowView.findViewById(R.id.title);
textView.setText(newsBean.getTitle());

final ImageView imageView = (ImageView) rowView.findViewById(R.id.image);

imageLoader.loadDrawable(newsBean.getImage(), new ImageCallback() {
public void imageLoaded(Drawable imageDrawable, String imageUrl) {
imageView.setImageDrawable(imageDrawable);
}
});
viewMap.put(position, rowView);
}
return rowView;
}

}

AsyncImageLoader,異步下載的實現類,在這個類中提供動態刷新頁麵的接口,重點在內部接口ImageCallback中的imageLoaded方法,主要是通過接口調用這個方法來實現刷新的,仔細看看,這個是不是很像觀察者的設計模式,其實這裏完全可以使用觀察者設計模式,把內部接口抽出來,然後AsyncImageLoader實現,我覺得原作者在這裏作為內部類的形式來處理是很有道理的,觀察者的設計模式,是用來告訴多個人數據改變了,你們可以“行動”了,而這裏,行動-->刷新界麵就一個地方調用,其他地方不需要也不應該調用,有內部類來隱藏掉這些處理是很有必要的。

publicclass AsyncImageLoader {
private Map<String, SoftReference<Drawable>> imageCache=new HashMap<String, SoftReference<Drawable>>();

public Drawable loadDrawable(final String imageUrl,final ImageCallback callback){
if(imageCache.containsKey(imageUrl)){
SoftReference<Drawable> softReference=imageCache.get(imageUrl);
if(softReference.get()!=null){
return softReference.get();
}
}
final Handler handler=new Handler(){
@Override
publicvoid handleMessage(Message msg) {
callback.imageLoaded((Drawable) msg.obj, imageUrl);
}
};
new Thread(){
publicvoid run() {
Drawable drawable=loadImageFromUrl(imageUrl);
imageCache.put(imageUrl, new SoftReference<Drawable>(drawable));
handler.sendMessage(handler.obtainMessage(0,drawable));
};
}.start();
returnnull;
}

protected Drawable loadImageFromUrl(String imageUrl) {
try {
return Drawable.createFromStream(new URL(imageUrl).openStream(), "src");
} catch (Exception e) {
thrownew RuntimeException(e);
}
}

publicinterface ImageCallback{
publicvoid imageLoaded(Drawable imageDrawable,String imageUrl);
}
}

源碼下載:https://files.cnblogs.com/xiaoQLu/ListViewAsynUpdate.rar

最後更新:2017-04-03 22:15:45

  上一篇:go apk反編譯總結(看雪論壇)
  下一篇:go Android藍牙開發