我对 Android 开发很感兴趣,我正在尝试使用在 OnCreate 期间从 twitter 解析的图像填充列表视图。我可以毫无问题地填充列表视图,但列表视图滚动生涩而不是平滑。起初我认为这是一个模拟器问题,但它在我的设备上也很生涩。令人沮丧的是,我使用的是 stackoverflow 上的所有教程和帖子都推荐的 viewholder 方法。任何帮助是极大的赞赏。这是我的代码...
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.listitem, null);
ViewHolder holder = new ViewHolder();
holder.username = (TextView) v.findViewById(R.id.username);
holder.message = (TextView) v.findViewById(R.id.message);
holder.image = (ImageView) v.findViewById(R.id.avatar);
v.setTag(holder);
}
Tweet tweet = tweets.get(position);
if (tweet != null) {
ViewHolder holder = (ViewHolder)v.getTag();
holder.username = (TextView) v.findViewById(R.id.username);
holder.message = (TextView) v.findViewById(R.id.message);
holder.image = (ImageView) v.findViewById(R.id.avatar);
if (holder.username != null) {
holder.username.setText(tweet.username);
}
if(holder.message != null) {
holder.message.setText(tweet.message);
}
if(holder.image != null) {
holder.image.setImageBitmap(getBitmap(tweet.image_url));
}
}
return v;
}
}
static class ViewHolder {
TextView username;
TextView message;
ImageView image;
}
public Bitmap getBitmap(String bitmapUrl) {
try {
URL url = new URL(bitmapUrl);
return BitmapFactory.decodeStream(url.openConnection() .getInputStream());
}
catch(Exception ex) {return null;}
}