<meter id="pryje"><nav id="pryje"><delect id="pryje"></delect></nav></meter>
          <label id="pryje"></label>

          新聞中心

          EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > android viewHolder處理listView滑動(dòng)

          android viewHolder處理listView滑動(dòng)

          作者: 時(shí)間:2016-10-08 來源:網(wǎng)絡(luò) 收藏

          在沒有用viewHolder的情況下,listView表現(xiàn)效率低下。如果加載的數(shù)量過多則會(huì)一點(diǎn)點(diǎn)的消耗內(nèi)存,直到拋出oom。開始異步加載圖片會(huì)出現(xiàn)圖片錯(cuò)位的問題,后來查閱資料將holder里邊的圖片地址和圖片一一對(duì)應(yīng)起來,在異步加載的回調(diào)函數(shù)中將其替換回來。

          本文引用地址:http://www.ex-cimer.com/article/201610/305655.htm

          holder.thumb_image.setTag(hotel.getHotelTitlePic()); //避免圖標(biāo)錯(cuò)位,在異步加載成功后替換回來

          ImageView imageView = (ImageView) listView.findViewWithTag(imageUrl);

          if(imageView != null){

          imageView.setImageDrawable(imageDrawable);

          imageView.setTag();

          }

          以上關(guān)鍵代碼解決圖片錯(cuò)位問題。

          下面是getView()方法

          public View getView(int position, View rowView, ViewGroup parent){

          final MHotelInfo hotel = this.getItem(position);

          if (rowView == null) {

          holder = new ViewHolder();

          LayoutInflater inflater = ((Activity) this.getContext())

          .getLayoutInflater();

          rowView = inflater.inflate(R.layout.hotel_item_view, null);

          holder.typeName = (TextView) rowView.findViewById(R.id.hotelType);

          holder.thumb_image=(ImageView)rowView.findViewById(R.id.img);// 縮略圖

          holder.distance = (TextView) rowView.findViewById(R.id.distance);

          rowView.setTag(holder);

          }else{

          holder = (ViewHolder) rowView.getTag();

          }

          // 設(shè)置ListView的相關(guān)值

          holder.thumb_image.setTag(hotel.getHotelTitlePic()); //避免圖標(biāo)錯(cuò)位,在異步加載成功后替換回來

          holder.typeName.setText(hotel.getTypeName());

          if(null == hotel.getHotelTitlePic() || hotel.getHotelTitlePic().equals()){ //如果沒有圖標(biāo)就顯示默認(rèn)圖標(biāo)

          holder.thumb_image.setImageResource(R.drawable.downloadfalse);

          }else{

          //異步加載圖片

          imageLoader.loadDrawable(hotel.getHotelTitlePic(), new ImageCallback() {

          public void imageLoaded(Drawable imageDrawable, String imageUrl) {

          ImageView imageView = (ImageView) listView.findViewWithTag(imageUrl);

          if(imageView != null){

          imageView.setImageDrawable(imageDrawable);

          imageView.setTag();

          }

          }

          });

          }

          return rowView;

          }

          static class ViewHolder {

          TextView typeName;

          ImageView thumb_image;

          }

          但是ViewHolder為什么是被設(shè)計(jì)成static類型的,本人一直沒弄清楚。在網(wǎng)上找了些資料但是一直沒能看到一個(gè)能說服我的理由。又說節(jié)省內(nèi)存,保持對(duì)象的唯一性,還有說保持圖片不錯(cuò)亂但是個(gè)人感覺都不怎么靠譜。今天我試了下將static去掉照常運(yùn)行沒感覺和以前有什么區(qū)別。后來我查了下static類的一些特性,首先ViewHolder是一個(gè)內(nèi)部類若是static類的話則不需要依賴外部類的對(duì)象。也就是說只有static的內(nèi)部類的成員變量才可以被聲明成static類型,若內(nèi)部類不是static類型的則不可以聲明靜態(tài)成員變量。不知道是不是static內(nèi)部類的這個(gè)特性吸引了設(shè)計(jì)者?如果有誰清楚這個(gè)問題還請(qǐng)不吝賜教。



          關(guān)鍵詞:

          評(píng)論


          相關(guān)推薦

          技術(shù)專區(qū)

          關(guān)閉
          看屁屁www成人影院,亚洲人妻成人图片,亚洲精品成人午夜在线,日韩在线 欧美成人 (function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })();