我有一个包含图像和文本的自定义列表视图,并使用Glide图像库在listview中正确加载。现在我想在imageview上实现一个OnItemClickListener,以在另一个活动中显示图像。
适配器代码:
public class DescAdapter extends BaseAdapter {
private final listdisplay ds;
private ArrayList<descusers> dusers;
private ArrayList<descusers> orig;
public DescAdapter(listdisplay ds, ArrayList<descusers> dusers) {
this.ds = ds;
this.dusers = dusers;
}
@Override
public int getCount() {
return dusers.size();
}
@Override
public Object getItem(int position) {
return dusers.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
descusers du = dusers.get(position);
String username = du.loginname;
String descCrip=du.descCrip;
String Limage = du.image;
long Ptime = du.Ptime;
Viewholder viewholder = null;
if(convertView==null) {
viewholder = new Viewholder();
convertView = LayoutInflater.from(ds).inflate(R.layout.customlist, null);
viewholder.uname = (TextView) convertView.findViewById(R.id.username);
viewholder.desc = (TextView) convertView.findViewById(R.id.description);
viewholder.time = (TextView)convertView.findViewById(R.id.time);
viewholder.iview = (ImageView) convertView.findViewById(R.id.imageList);
convertView.setTag(viewholder);
}else {
viewholder = (Viewholder) convertView.getTag();
}
viewholder.uname.setText(username);
viewholder.desc.setText(descCrip);
if (Limage.trim().isEmpty()) {
viewholder.iview.setVisibility(View.GONE);
} else {
Glide.with(convertView.getContext()).load(Limage)
.diskCacheStrategy(DiskCacheStrategy.ALL).fitCenter().centerCrop().into(viewholder.iview);
viewholder.iview.setVisibility(View.VISIBLE);
}
return convertView;
}
OnItemClick代码:
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
final ImageView imageView = (ImageView) view.findViewById(R.id.imageList);
final BitmapDrawable bitmapDrawable = (BitmapDrawable) imageView.getDrawable();
final Bitmap yourBitmap = bitmapDrawable.getBitmap();
Intent intent = new Intent(this, cust.class);
intent.putExtra("BitmapImage", yourBitmap);
}
收到意图:
Intent intent = getIntent();
Bitmap bitmap = (Bitmap) intent.getParcelableExtra("BitmapImage");
imagedisplay = (ImageView)findViewById(R.id.imagedisplay);
imagedisplay.setImageBitmap(bitmap);
但是当我点击下面的图片时,会看到异常。
java.lang.ClassCastException:com.bumptech.glide.load.resource.bitmap.GlideBitmapDrawable无法强制转换为android.graphics.drawable.BitmapDrawable
答案 0 :(得分:1)
final BitmapDrawable bitmapDrawable = (BitmapDrawable) imageView.getDrawable();
将此BitmapDrawable转换更改为GlideBitmapDrawable。
final GlideBitmapDrawable bitmapDrawable = (GlideBitmapDrawable ) imageView.getDrawable();
我希望Picaso将drawable作为GlideBitmapDrawable格式。
答案 1 :(得分:1)
您应该传递图片网址,然后在详细活动中加载该图片网址,而不是传递 Bitmap
。 Glide
处理 caching
图片,以便快速加载并避免不必要的位图对象混乱。
答案 2 :(得分:0)
试试这段代码 在适配器类中传递intent:
Intent i = new Intent(context, S.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.putExtra("image",Limage);
context.startActivity(i);
在你的S.class中获得意图
Intent i = getIntent();
String image = i.getStringExtra("image");
Picasso.with(this).load(image).into(img_meme);//imageview name
希望它适用于你