You must not call setTag() on a view Glide is targeting when use Glide
以下代码是一个显示图片的RecyclerView 的Adapter用到的,当点击图片,跳到另一个Activity显示大图。RecyclerView 与ListView不同 然而没有setOnClickListener() 方法, 设置事件监听, 使用下面的方式。点击后获取到图片url 传递给另一个activity
@Override public ImageViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = mLayoutInflater.inflate(R.layout.item_layout, parent, false); final ImageViewHolder holder = new ImageViewHolder(view); holder.iv.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String url = (String)v.getTag(R.id.recycler_item); Intent intent = new Intent(mContext, ImageActivity.class); intent.putExtra(ImageActivity.EXTRA_URL, url); mContext.startActivity(intent); } }); return holder; } @Override public void onBindViewHolder(ImageViewHolder holder, int position) { String url = mDatas.get(position).getUrl(); holder.iv.setTag(url); Glide.with(mContext).load(url).placeholder(R.drawable.image_loading) .centerCrop() .thumbnail(0.1f) .into(holder.iv); }
然而这段代码报错
You must not call setTag() on a view Glide is targeting when use Glide
setTag(Object tag) 还有一个重载方法 setTag(int key, Object tag)
上面的红色代码 改成
holder.iv.setTag(1, url);
这样又报另外一个错
java.lang.IllegalArgumentException: The key must be an application-specific resource id
所以这个key 不要随便用一个int 数, 改用某个R.id.xxx 比如
holder.iv.setTag(R.id.recycler_item, url);
get的地方也相应改掉
这样问题得以解决