ANDROID中自訂ADAPTER實現LISTVIEW動態重新整理進度條

來源:互聯網
上載者:User

http://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>();@Overridepublic 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(){@Overridepublicvoid 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);}}

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.