3

我对 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 应该在的地方,但应用程序非常慢。我已经检查了所有内容,但我完全不知道如何解决这个问题。你可以帮帮我吗?如果您需要额外的信息,请询问。

谢谢。

视频Activity.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>
4

2 回答 2

2

从 VideosAdapter 的 getView() 方法中的 if/else 中移动以下行

holder.title.setText(video.getTitle());
holder.thumb.setImageDrawable(video.getThumbUrl());

就在上面

return convertView;
于 2013-01-04T18:23:48.353 回答
1

您没有修改现有视图。If convertViewis not null 这意味着 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;

}

PS 使用Ctrl++Shift很好F地格式化您的代码。:)

于 2013-01-04T18:26:17.507 回答