The following code is used by a Recycler View Adapter that displays pictures. When you click on the picture, jump to another Activity to display the big picture. RecyclerView is different from ListView, but there is no setOnClickListener() method. Set up event listeners, using the following method. Click to get the image url and pass it to another 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); }
However, this code reported an error.
You must not call setTag() on a view Glide is targeting when use Glide
setTag(Object tag) also has an overload method, setTag(int key, Object tag)
Change the red code above to
holder.iv.setTag(1, url);
That's another mistake.
java.lang.IllegalArgumentException: The key must be an application-specific resource id
So instead of just using an int, use a R.id.xxx, for example.
holder.iv.setTag(R.id.recycler_item, url);
The place where get is changed accordingly
So the problem can be solved.