3

現在、Handler を使用して 15 個のツイートを ArrayList に取り込み、完成した ArrayList を updateUI メソッドに渡しています。これまでのところ問題なく動作していますが、ListView が読み込まれると、スクロール エクスペリエンスが非常に不安定になります。TwitterAdapter から画像参照を削除すると、ListView のユーザー エクスペリエンスが高速でスムーズになります。スクロールしているときにアダプターが各グラフィックを再ダウンロードしようとしていると思われますが、デバッガーがその方法をヒッチしてそれを証明することはできません。

アクティビティ内のキー コードは次のとおりです。

@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;
    }   
}
4

1 に答える 1

0

スクロールしているときにアダプタが各グラフィックを再ダウンロードしようとしていると思われます

はい。

getView()ofTwitterAdapterはメイン アプリケーション スレッドで呼び出されます。そこでは、インターネットから画像をダウンロードしているgetBitmap()(完全に無意味なクラスで)呼び出しています。TwitterItemAdapter

SDK サンプルからブログ投稿アプリ フレームワーク全体に至るまで、さまざまな洗練度を備えた背景画像ローダーの実装がおそらく 20 ほどあります。

于 2012-04-29T16:19:06.960 に答える