未显示将imageDrawable设置为ImageView

时间:2015-10-14 10:03:01

标签: android android-listview

在imageview上设置drawable时,我在ArrayAdapter类中遇到问题。首先,只有我在列表中滚动才会出现。

    public View getView(int position, View convertView, ViewGroup parent) {

    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.item, parent, false);
        holder = new Holder();
        convertView.setTag(holder);

        loadUI(holder, convertView);
    } else {
        holder = (Holder) convertView.getTag();
    }

    final MyObject obj = getItem(position);
    if(obj != null) {
    setAvatar(obj.getModerator(), holder);
    }

    return convertView;
}

private void setAvatar(Moderator moderator, Holder holder) {
        String avatar = moderator.getAvatar();
        if (avatar != null) {
            holder.icon.setImageUrl(avatar, ImageCacheManager.getInstance().getImageLoader());
        } else {

            TextDrawable drawable = TextDrawable.builder()
                    .beginConfig()
                    .width(60)
                    .height(60)
                    .endConfig()
                    .buildRoundRect(moderator.getAlias() != null ? moderator.getAlias() : "G", Color.BLUE, 8);

            Log.d(Constants.TAG, "Drawable: " + drawable);
            holder.icon.setImageDrawable(drawable);

        }
    }

至于TextDrawable我正在使用这个开源类:https://github.com/amulyakhare/TextDrawable/blob/master/library/src/main/java/com/amulyakhare/textdrawable/TextDrawable.java

持有人类:

static class Holder {
        TextView authorNameTxt;
        TextView msgTxt;
        TextView timeTxt;
        com.pubble.mod.utils.CircleImageView icon;
        com.android.volley.toolbox.NetworkImageView msgPhoto;
    }

基本上它填充了NetworkImageView,但只有当我滚动并且我不明白为什么时。

1 个答案:

答案 0 :(得分:0)

使用此代码

 TextDrawable drawable = mDrawableBuilder.build("G", mColorGenerator.getColor(item.data));
 holder.imageView.setImageDrawable(drawable);