Android ListView在使用推特显示个人资料头像时速度较慢

Android ListView在使用推特显示个人资料头像时速度较慢,android,listview,android-listview,Android,Listview,Android Listview,目前,我正在使用处理程序将15条tweet拉入ArrayList,然后将完成的ArrayList传递给updateUI方法。到目前为止,它工作得很好,但是当我的ListView被填充时,滚动的体验非常不稳定。当我从TwitterAdapter中删除images引用时,ListView具有快速/流畅的用户体验。我怀疑我的适配器试图在我滚动时重新加载每个图形,但无法让调试器挂接该方法来证明它 以下是活动中的关键代码: @Override public void onCreate(Bundle

目前,我正在使用处理程序将15条tweet拉入ArrayList,然后将完成的ArrayList传递给updateUI方法。到目前为止,它工作得很好,但是当我的ListView被填充时,滚动的体验非常不稳定。当我从TwitterAdapter中删除images引用时,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);
    }
@覆盖
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setTheme(R.style.Theme_BambooZen);
this.setContentView(R.layout.twitter\u布局);
myListView=(ListView)findViewById(R.id.TwitterListView);
/*
*在添加新处理程序之前,请删除对处理程序的所有现有回调,
*绝对确保我们不会收到比我们想要的更多的回调事件。
*/
mHandler.removeCallbacks(mUpdateAdapterTask);
mHandler.postDelayed(mUpdateAdapterTask,1000);//启动处理程序一次,处理程序将管理自调用
}   
private Runnable mUpdateAdapterTask=new Runnable(){
公开募捐{
试试{
//在单独的线程上获取Twitter推文(需要网络)
ArrayList tweets=getTweets(“公式1”,1);
更新(推特);
//每45秒刷新一次=45000
mHandler.postdayed(这是45000);
}
捕获(例外e)
{
e、 toString();
}
}
};
/*
*在UIThread上重建tweet
*/
公共无效更新(ArrayList推文){
adapter=新的TwitterAdapter(这个,R.layout.twitter_列表项,tweets);
myListView.setAdapter(适配器);
}
项目适配器:

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;}
    }
}
公共类TwitterItemAdapter扩展了ArrayAdapter{
公共TwitterItemAdapter(上下文,int textViewResourceId){
super(上下文,textViewResourceId);
//TODO自动生成的构造函数存根
}
公共静态位图getBitmap(字符串bitmapUrl){
试一试{
URL URL=新URL(位图URL);
返回BitmapFactory.decodeStream(url.openConnection().getInputStream());
}
catch(异常ex){returnnull;}
}
}
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;
    }   
}
公共类TwitterAdapter扩展了ArrayAdapter{
私人ArrayList推文;
公共TwitterAdapter(上下文上下文、int textViewResourceId、ArrayList tweets){
super(上下文、textViewResourceId、tweets);
this.tweets=tweets;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
视图v=转换视图;
如果(v==null){
LayoutInflater vi=(LayoutInflater)getContext().getSystemService(Context.LAYOUT\u INFLATER\u SERVICE);
v=vi.充气(R.layout.twitter_列表项,空);
}
Tweet=tweets.get(位置);
if(tweet!=null){
TextView用户名=(TextView)v.findViewById(R.id.username);
TextView消息=(TextView)v.findViewById(R.id.message);
ImageView图像=(ImageView)v.findViewById(R.id.avatar);
如果(用户名!=null){
username.setText(tweet.username);
}
如果(消息!=null){
message.setText(tweet.message);
}
如果(图像!=null){
setImageBitmap(TwitterItemAdapter.getBitmap(tweet.image_url));
}
}
返回v;
}   
}
我怀疑我的适配器试图在我滚动时重新加载每个图形

在主应用程序线程上调用TwitterAdapter的
getView()
。在那里,您正在调用
getBitmap()
(在一个完全没有意义的
TwitterItemAdapter
类上),它正在从Internet下载图像


从到,可能有24种后台图像加载器的实现,其复杂程度各不相同。

是的,自从您在GetView中执行image.setImageBitmap(TwitterItemAdapter.getBitmap())以来,它一直在下载它们。谢谢,您的回答是正确的。。ListView一直在尝试下载。源代码可靠,我最终使用了SDK示例,它在我的适配器中添加了以下代码并删除了无意义的列表适配器,效果非常好,我打算用它去其他地方。AvatarDownloader av=新的AvatarDownloader();影音下载(tweet.image\u网址,图片);