17

我正在使用通用图像加载器从互联网下载图像。有些图片被设置为头像,所以我将它们四舍五入,而其他图片则被拉伸。问题是列表视图在滚动时显示错误的图像,一段时间后它显示正确的图像。我已经在 stackoverflow 上搜索了类似的问题,但我无法让它正常工作。

我正在使用自定义适配器,getview 代码和舍入和拉伸图像的代码如下:

    @Override
public View getView(final int position, View convertView, ViewGroup parent) {
    View view = convertView;

    final ViewHolder holder;
    if(view == null)
    {
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.whatsnew_listitem, null);

        holder = new ViewHolder();
        holder.playButton = (ImageButton) view.findViewById(R.id.btn_playVideo);
        holder.mediaThumbnail = (ImageView) view.findViewById(R.id.imageview_mediaImage);
        holder.avatar = (ImageView) view.findViewById(R.id.imageview_avatar);
        holder.artistName = (TextView) view.findViewById(R.id.textview_artistName);
        holder.timestamp = (TextView) view.findViewById(R.id.textview_timestamp);
        holder.status = (TextView) view.findViewById(R.id.textview_title);
        view.setTag(holder);

    } else {
        holder = (ViewHolder) view.getTag();
    }

    pos = position;
    Item item = items.get(position);

    if(item.getArtistName() != "")
    {
        // set thumbnail
        String thumbnailURL = "";
        holder.mediaThumbnail.setVisibility(View.INVISIBLE);
        if(item.getUpdateType() == Config.UPDATE_TYPE_1)
        { 
            holder.playButton.setVisibility(View.GONE);
            thumbnailURL = item.getPictureUri();

            if(thumbnailURL != null) 
            {

                ImageLoader.getInstance().loadImage(thumbnailURL, new SimpleImageLoadingListener() {
                    @Override
                    public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {

                        setImage(loadedImage, imageView, Config.MEDIA);
                        mediaImages[position] = true;
                        imageView.setVisibility(View.VISIBLE);

                    }
                });
            }

            holder.status.setText(item.getMessage());
        } 
        else if(item.getUpdateType() == Config.UPDATE_TYPE_2) 
        {
            //play button
            holder.playButton.setVisibility(View.VISIBLE);

            //set media image
            thumbnailURL = item.getVideoThumbnailUri();
            if(thumbnailURL != null)
            {
                ImageLoader.getInstance().loadImage(thumbnailURL, new SimpleImageLoadingListener() {
                    @Override
                    public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) 
                    {
                        setImage(loadedImage, imageView, Config.MEDIA);
                        mediaImages[position] = true;
                        imageView.setVisibility(View.VISIBLE);
                    }
                });
            }

            //set youtube status
            holder.status.setText(item.getTitle());
            holder.playButton.bringToFront();
            holder.playButton.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {

                    if(item.getVideoId()!= null) {
                        Intent intent = new Intent(Intent.ACTION_VIEW);
                        intent.setData(Uri.parse(Config.YOUTUBE_URI + item.getVideoId() ));
                        context.startActivity(intent);
                    }
                }
            });
        }

        //set avatar

        String avatarURL = item.getAvatarImageUri();
        holder.avatar.setVisibility(View.INVISIBLE);
        if(avatarURL != null)// && avatarImages[position] == false)
        {
            ImageLoader.getInstance().loadImage(avatarURL, new SimpleImageLoadingListener() {
                @Override
                public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {

                    setImage(loadedImage, imageView, Config.AVATAR);
                    avatarImages[position] = true;
                    imageView.setVisibility(View.VISIBLE);

                }
            });
        }

        //set artist name
        holder.artistName.setText(item.getArtistName());

        //set timestamp
        if(timerList[position] == false)
        {
            updateTime(holder.timestamp, item);
        }
    }


    view.setTag(holder);
    return view;
}

//@Override
public void setImage(final Bitmap bitmap, final ImageView imageView, final String imagetype) {
    // TODO Auto-generated method stub

    Activity activity = (Activity) context;
    activity.runOnUiThread(new Runnable() {

        @Override
        public void run() {

            Bitmap croppedBmp = null;
            int imageHeight = bitmap.getHeight();
            int imageWidth = bitmap.getWidth();

            if(imagetype == Config.MEDIA) 
            {            
                if(imageHeight <imageWidth) {
                    croppedBmp = Bitmap.createScaledBitmap( bitmap, dp, dp, true);
                    imageView.setImageBitmap(croppedBmp);
                }
            } 
            else if(imagetype == Config.AVATAR) 
            {

                int radius;
                if(imageWidth >  imageHeight) 
                {
                    croppedBmp = Bitmap.createBitmap(imageHeight, imageHeight, Bitmap.Config.ARGB_4444);
                    radius = imageHeight/2;
                } 
                else 
                {
                    croppedBmp = Bitmap.createBitmap(imageWidth, imageWidth, Bitmap.Config.ARGB_4444);
                    radius = imageWidth/2;
                } 

                Canvas canvas = new Canvas(croppedBmp);      
                final int color = 0xff424242;
                final Paint paint = new Paint();
                final Rect rect = new Rect(0, 0, imageWidth, imageHeight);

                paint.setAntiAlias(true);
                canvas.drawARGB(0, 0, 0, 0);
                paint.setColor(color);
                canvas.drawCircle(radius, radius, radius, paint);
                paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
                canvas.drawBitmap(bitmap, rect, rect, paint);

                imageView.setImageBitmap(croppedBmp);
                ImageLoader.getInstance().displayImage(uri, imageView, listener)
                notifyDataSetChanged();

            }
        }
    });

}
4

4 回答 4

1

试试这个例子 http://android.amberfog.com/?p=296

使用这个方法

public int getItemViewType(int position) {
        return mSeparatorsSet.contains(position) ? TYPE_SEPARATOR : TYPE_ITEM;
}

并在 getView() 中获取如下类型

int type = getItemViewType(position);

我按照示例解决了我的问题

于 2013-11-27T04:06:19.893 回答
0

在每个时刻 ListView 显示几个元素(那些“适合”显示的元素),而不是创建一个新的,在轻弹期间,新元素替换了列表中的旧元素

尝试这个:

public class Info {
    private ImageView mediaThumbnail;
    private ImageView avatar; 
        ...

    public ImageView getMediaThumbnail() {
        return mediaThumbnail;
    }

    public void setMediaThumbnail(ImageView mediaThumbnail) {
        this.mediaThumbnail = mediaThumbnail;
    }
        ...
}

在适配器中添加到 ViewHolder 字段Info info并编写下一个代码

public View getView(int position, View convertView, ViewGroup parent) {
    row = convertView;
    Info info = getItem(position);

    if (null == row)
        setupRow(parent, info);
    else
        getRow(info);
    ...
}

private void setupRow(ViewGroup parent, Info info) {
    LayoutInflater inflater = (LayoutInflater) getContext()
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    view = inflater.inflate(R.layout.list_item, parent, false);

    holder = new ViewHolder();
    holder.playButton = (ImageButton) view.findViewById(R.id.btn_playVideo);
    holder.mediaThumbnail = (ImageView) view.findViewById(R.id.imageview_mediaImage);
    holder.avatar = (ImageView) view.findViewById(R.id.imageview_avatar);
    holder.artistName = (TextView) view.findViewById(R.id.textview_artistName);
    holder.timestamp = (TextView) view.findViewById(R.id.textview_timestamp);
    holder.status = (TextView) view.findViewById(R.id.textview_title);
    view.setTag(holder);
}

private void getRow(DownloadInfo info) {
        holder = (ViewHolder) row.getTag();

        holder.info.setMediaThumbnail(null);
        ...
        holder.info = info;

        holder.info.setMediaThumbnail(holder.mediaThumbnail);
        ...
    }
于 2013-10-17T13:30:58.693 回答
0

初始化 if else 条件之外的视图

if(view == null)
    {
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.whatsnew_listitem, null);

        holder = new ViewHolder();

        view.setTag(holder);

    } else {
        holder = (ViewHolder) view.getTag();
    }

 holder.playButton = (ImageButton) view.findViewById(R.id.btn_playVideo);
        holder.mediaThumbnail = (ImageView) view.findViewById(R.id.imageview_mediaImage);
        holder.avatar = (ImageView) view.findViewById(R.id.imageview_avatar);
        holder.artistName = (TextView) view.findViewById(R.id.textview_artistName);
        holder.timestamp = (TextView) view.findViewById(R.id.textview_timestamp);
        holder.status = (TextView) view.findViewById(R.id.textview_title);
于 2013-10-17T12:22:20.120 回答
0

使用 ImageLoader 的覆盖方法

    @Override
    public void onLoadingStarted(String imageUri, View view) {

        ImageLoader.getInstance().displayImage(imageUri, (ImageView) view);

    }

并在 displayOptions 中使用一个存根图像,该图像将一直显示,直到图像从 URL 加载。

DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
        .showStubImage(R.drawable.com_facebook_profile_default_icon).build();

ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getActivity()).defaultDisplayImageOptions(defaultOptions).build();
于 2013-10-17T12:27:53.200 回答