我有一个ListView的问题,或更确切地说 - ImageView放在它上面。我的应用程序正在从Youtube下载缩略图。到那个地方的一切都很好。稍后,在使用getView()时会出现奇怪的行为。当我在if条件下将数据加载到持有者时:
if(convertView == null){
convertView = mInflater.inflate(R.layout.list_item_user_video, null);
holder = new ViewHolder();
holder.title = (TextView) convertView.findViewById(R.id.userVideoTitleTextView);
holder.thumb = (UrlImageView) convertView.findViewById(R.id.userVideoThumbImageView);
Video video = videos.get(position);
holder.title.setText(video.getTitle());
holder.thumb.setImageDrawable(video.getThumbUrl());
convertView.setTag(holder);
}
我的应用程序很快,但每对ImageView和TextView的位置在快速滚动时被洗牌。当部分:
Video video = videos.get(position);
holder.title.setText(video.getTitle());
holder.thumb.setImageDrawable(video.getThumbUrl());
超出if(convertView == null)ImageView和TextView应该是它们的位置,但应用程序非常慢。我检查了一切,我完全不知道如何解决这个问题。你可以帮帮我吗?如果您需要额外的信息,请询问。
感谢。
VideosActivity.java
public class VideosAdapter extends BaseAdapter{
// The list of videos to display
List<Video> videos;
// An inflator to use when creating rows
private LayoutInflater mInflater;
public VideosAdapter(Context context, List<Video> videos) {
super();
this.videos = videos;
this.mInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return videos.size();
}
@Override
public Object getItem(int position) {
return videos.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView == null){
convertView = mInflater.inflate(R.layout.list_item_user_video, null);
holder = new ViewHolder();
holder.title = (TextView) convertView.findViewById(R.id.userVideoTitleTextView);
holder.thumb = (UrlImageView) convertView.findViewById(R.id.userVideoThumbImageView);
Video video = videos.get(position);
holder.title.setText(video.getTitle());
holder.thumb.setImageDrawable(video.getThumbUrl());
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
return convertView;
}
static class ViewHolder{
UrlImageView thumb;
TextView title;
Video video;
int id;
}
}
UrlImageView.java
public class UrlImageView extends LinearLayout {
private Context mContext;
private Drawable mDrawable;
private ProgressBar mSpinner;
private ImageView mImage;
public UrlImageView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public UrlImageView(Context context) {
super(context);
init(context);
}
private void init(final Context context) {
mContext = context;
mImage = new ImageView(mContext);
mImage.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
mImage.setVisibility(View.GONE);
mSpinner = new ProgressBar(mContext);
mSpinner.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
mSpinner.setIndeterminate(true);
addView(mSpinner);
addView(mImage);
}
public void setImageDrawable(final String url) {
mDrawable = null;
mSpinner.setVisibility(View.VISIBLE);
mImage.setVisibility(View.GONE);
new Thread() {
public void run() {
try {
mDrawable = getDrawableFromUrl(url);
imageLoadedHandler.sendEmptyMessage(RESULT_OK);
} catch (MalformedURLException e) {
imageLoadedHandler.sendEmptyMessage(RESULT_CANCELED);
} catch (IOException e) {
imageLoadedHandler.sendEmptyMessage(RESULT_CANCELED);
}
};
}.start();
}
private final Handler imageLoadedHandler = new Handler(new Callback() {
@Override
public boolean handleMessage(Message msg) {
switch (msg.what) {
case RESULT_OK:
mImage.setImageDrawable(mDrawable);
mImage.setVisibility(View.VISIBLE);
mSpinner.setVisibility(View.GONE);
break;
case RESULT_CANCELED:
default:
// Could change image here to a 'failed' image
// otherwise will just keep on spinning
break;
}
return true;
}
});
private static Drawable getDrawableFromUrl(final String url) throws IOException, MalformedURLException {
return Drawable.createFromStream(((InputStream) new URL(url).getContent()), "name");
}
}
list_item_user_video.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal" >
<com.example.example.ui.widget.UrlImageView
android:id="@+id/userVideoThumbImageView"
android:layout_width="60dp"
android:layout_height="45dp"
android:layout_marginRight="10dp"
android:contentDescription="YouTube video thumbnail"
android:src="@drawable/ic_launcher"/>
<TextView
android:id="@+id/userVideoTitleTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Video Title Not Found" />
</LinearLayout>
答案 0 :(得分:2)
在VideosAdapter的getView()方法
中移动if / else中的以下行holder.title.setText(video.getTitle());
holder.thumb.setImageDrawable(video.getThumbUrl());
正好在
之上return convertView;
答案 1 :(得分:1)
您没有修改现有视图。如果convertView
不为空,则表示Android正在回收视图而不是对其进行充气,但您仍需要使用新值更新它。这样的事情会起作用:
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView == null) {
convertView = mInflater.inflate(R.layout.list_item_user_video, null);
holder = new ViewHolder();
}
else {
holder = (ViewHolder) convertView.getTag();
}
holder.title = (TextView) convertView.findViewById(R.id.userVideoTitleTextView);
holder.thumb = (UrlImageView) convertView.findViewById(R.id.userVideoThumbImageView);
Video video = videos.get(position);
holder.title.setText(video.getTitle());
holder.thumb.setImageDrawable(video.getThumbUrl());
convertView.setTag(holder);
return convertView;
}
P.S。使用 Ctrl + Shift + F 可以很好地格式化代码。 :)