gpt4 book ai didi

使用推文显示个人资料头像时,Android ListView 变慢

转载 作者:太空狗 更新时间:2023-10-29 15:18:24 25 4
gpt4 key购买 nike

目前,我使用 Handler 将 15 条推文拉入 ArrayList,然后将完成的 ArrayList 传递给 updateUI 方法。到目前为止它运行良好,但是当我的 ListView 被填充时,滚动体验非常不稳定。当我从我的 TwitterAdapter 中删除图像引用时,ListView 具有快速/流畅的用户体验。我怀疑我的适配器在我滚动时试图重新下载每个图形,但无法让调试器使用该方法来证明这一点。

这是 Activity 中的关键代码:

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

最佳答案

I suspect my Adapter is attempting to re-dowload each graphic when I'm scrolling

是的。

TwitterAdapter

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

可能有两打可用的背景图像加载器实现,复杂程度各不相同,来自 an SDK sampleblog postswhole app frameworks .

关于使用推文显示个人资料头像时,Android ListView 变慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10373252/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com