【问题标题】:ImageView.setImageBitmap not displaying image in RecyclerViewImageView.setImageBitmap 不在 RecyclerView 中显示图像
【发布时间】:2016-12-23 01:31:38
【问题描述】:

我在RecyclerView 中显示图像,其来源是从彩信中获取的位图。问题是图像没有显示。绝对没有任何显示。这是我的 onBindView:

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    // - get element from your dataset at this position
    // - replace the contents of the view with that element
    final String name = mDataset.get(position).getContact() ;
    final MMSMessage message = mDataset.get(position);
    holder.txtHeader.setText(name);
    DateTime dateTime = new DateTime(message.getDate());
    holder.txtDate.setText(dateTime.toString(Globals.generalSQLFormatterDT));
    holder.txtText.setText(message.getBody());
    holder.txtText.setVisibility(View.VISIBLE);
    Bitmap bitmap = message.getBitmap();
    if (bitmap != null) {
        //bitmap is not null and I can see an image using Android Studio
        bitmap =Bitmap.createScaledBitmap(bitmap, 120, 120, false); 
        holder.imgMMS.setImageBitmap(bitmap);

    } else {
        holder.imgMMS.setVisibility(View.GONE);
    }

}

ImageView 的 xml:

    <ImageView
    android:layout_below="@+id/thirdLine"
    android:id="@+id/imageMMS"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginRight="6dip"
    android:contentDescription="TODO"
     />

我查看了here 并尝试将图像缩小到任意小尺寸。我认为这不是内存不足错误 - 我尝试将启动器图标放入作为测试。我做错了什么?

【问题讨论】:

  • 为什么在if 中没有针对else 的操作?
  • @Selvin 不确定你的意思?
  • else 中只有一个“动作”...if 中的相反动作在哪里...我想为每个类似的问题获得一分钱...

标签: android bitmap imageview


【解决方案1】:
if (bitmap != null) {
        //bitmap is not null and I can see an image using Android Studio
        bitmap =Bitmap.createScaledBitmap(bitmap, 120, 120, false); 
        holder.imgMMS.setImageBitmap(bitmap);
holder.imgMMS.setVisibility(View.GONE);
    } else {
        holder.imgMMS.setVisibility(View.GONE);
    }

您正在将可见性设置为 GONE。我的猜测是 RecyclerView 正在回收视图,并且当它执行时视图已消失,因为您没有将其设置为可见。尝试添加 holder.imgMMS.setVisibility(View.VISIBLE);当位图不为空时,像这样:

if (bitmap != null) {
        //bitmap is not null and I can see an image using Android Studio
        bitmap =Bitmap.createScaledBitmap(bitmap, 120, 120, false); 
        holder.imgMMS.setImageBitmap(bitmap);
        holder.imgMMS.setVisibility(View.VISIBLE);
    } else {
        holder.imgMMS.setVisibility(View.GONE);
    }

【讨论】:

    猜你喜欢
    • 2021-03-10
    • 2021-12-11
    • 1970-01-01
    • 1970-01-01
    • 2021-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-06
    相关资源
    最近更新 更多