2012-04-29 44 views
3

目前,我使用Handler將15條鳴叫拖入ArrayList中,然後將完成的ArrayList傳遞給updateUI方法。到目前爲止它工作得很好,但是當我的ListView被填充時,滾動體驗非常不穩定。當我從我的TwitterAdapter中刪除圖像引用時,ListView具有快速/流暢的用戶體驗。我懷疑當我滾動時,我的Adapter正試圖重新下載每個圖形,但無法讓調試器搭上該方法來證明它。Android ListView使用鳴叫顯示Profile Avatar時緩慢

下面是活動中的關鍵代碼:

@Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setTheme(R.style.Theme_BambooZen); 
     this.setContentView(R.layout.twitter_layout); 

     myListView = (ListView) findViewById(R.id.TwitterListView);   

     /* 
     * Remove any existing callbacks to the handler before adding the new handler, 
     * to make absolutely sure we don't get more callback events than we want. 
     */ 
     mHandler.removeCallbacks(mUpdateAdapterTask); 
     mHandler.postDelayed(mUpdateAdapterTask, 1000); //Fire the Handler once, the handler will manage self-calls   

    } 

    private Runnable mUpdateAdapterTask = new Runnable() { 
      public void run() { 

       try{ 

       //Get Twitter Tweets on separate thread (Requires Network)     
       ArrayList<Tweet> tweets = getTweets("Formula1", 1);     
       updateUI(tweets); 

       //Refresh every 45 seconds = 45000 
       mHandler.postDelayed(this, 45000); 

       } 
       catch (Exception e) 
       { 
        e.toString(); 
       } 
      } 
    }; 

    /* 
    * Rebuilds the Tweets on UIThread 
    */ 
    public void updateUI(ArrayList<Tweet> tweets){ 
     adapter = new TwitterAdapter(this, R.layout.twitter_listitem, tweets); 
     myListView.setAdapter(adapter); 
    } 

的項目適配器:

public class TwitterItemAdapter extends ArrayAdapter<Tweet> { 

    public TwitterItemAdapter(Context context, int textViewResourceId) { 
     super(context, textViewResourceId); 
     // TODO Auto-generated constructor stub 
    } 

    public static Bitmap getBitmap(String bitmapUrl) { 
     try { 
      URL url = new URL(bitmapUrl); 
      return BitmapFactory.decodeStream(url.openConnection() .getInputStream()); 
     } 
     catch(Exception ex) {return null;} 
    } 
} 

Twitter的適配器:

public class TwitterAdapter extends ArrayAdapter<Tweet>{ 

    private ArrayList<Tweet> tweets; 

    public TwitterAdapter(Context context, int textViewResourceId, ArrayList<Tweet> tweets) { 
     super(context, textViewResourceId, tweets); 
     this.tweets = tweets; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 


     View v = convertView; 
     if (v == null) { 
      LayoutInflater vi = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      v = vi.inflate(R.layout.twitter_listitem, null); 
     } 

     Tweet tweet = tweets.get(position); 
     if (tweet != null) { 
      TextView username = (TextView) v.findViewById(R.id.username); 
      TextView message = (TextView) v.findViewById(R.id.message); 
      ImageView image = (ImageView) v.findViewById(R.id.avatar); 

      if (username != null) { 
       username.setText(tweet.username); 
      } 

      if(message != null) { 
       message.setText(tweet.message); 
      } 

      if(image != null) { 
       image.setImageBitmap(TwitterItemAdapter.getBitmap(tweet.image_url));     
      } 
     } 
     return v; 
    } 
} 
+0

是的,它一直下載它們,因爲你在getView中設置image.setImageBitmap(TwitterItemAdapter.getBitmap()) – zapl 2012-04-29 15:26:46

+0

謝謝你,你是正確的。ListView試圖下載所有的時間。 – CampbellGolf 2012-05-01 21:35:53

回答

0

我懷疑我的適配器嘗試當我滾動時重新下載每個圖形

是的。

getView()TwitterAdapter在主應用程序線程上被調用。在那裏,你打電話getBitmap()(在一個完全沒有意義的TwitterItemAdapter類),這是從互聯網上下載圖像。

可能有二十幾種背景圖像加載器的實現可用,具有不同程度的複雜性,從an SDK sampleblog postswhole app frameworks

+0

固體來源,我結束了與SDK的例子,它通過將下面的代碼放入我的適配器並刪除無意義的List適配器,它就像一個魅力工作,我正在與其他地方。 AvatarDownloader av = new AvatarDownloader(); \t \t \t \t av.download(tweet.image_url,image); – CampbellGolf 2012-05-01 21:37:12