使用Llbrary Glide时显示Toast

时间:2017-11-10 12:44:27

标签: android android-recyclerview toast

我想使用RecyclerView并使用Library Glide显示图像。

 public void onBindViewHolder(CustomViewHolder holder, int position) {
    Sisop current = sisop.get(position);
    holder.namaItemView.setText(current.nama);
    Glide.with(context)
            .load(sisop.get(position).gambar)
            .placeholder(R.mipmap.ic_launcher)
            .into(holder.img_icon);
}

如果点击图片时会出现包含所点击项目名称的烤面包怎么办?

2 个答案:

答案 0 :(得分:3)

尝试将此 ClickListener 设置为您的 holder.img_icon

public void onBindViewHolder(CustomViewHolder holder, int position) {
    Sisop current = sisop.get(position);
    holder.namaItemView.setText(current.nama);
    Glide.with(context)
            .load(sisop.get(position).gambar)
            .placeholder(R.mipmap.ic_launcher)
            .into(holder.img_icon);
      holder.img_icon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(view.getContext(),sisop.get(position).gambar +" is Clicked", Toast.LENGTH_SHORT).show();
            }
        });
}
根据 @ v-rund-puro-hit

的建议

更新

class YourViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        ImageView img_icon;
        public CustomViewHolder(View itemView) {
            super(itemView);
            img_icon = (ImageView) itemView.findViewById(R.id.img_icon);
            img_icon.setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {
            if(view.getId() == R.id.img_icon){
                Toast.makeText(view.getContext(), sisop.get(getAdapterPosition()).nama+" is Clicked", Toast.LENGTH_SHORT).show();
            }
        }
    }

答案 1 :(得分:2)

我认为问题与滑行无关,这就是你可以在视图上设置onclick监听器的方法。

class CustomViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        ImageView img_icon;
        public CustomViewHolder(View itemView) {
            super(itemView);
            img_icon = (ImageView) itemView.findViewById(R.id.img_icon);
            img_icon.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if(v.getId() == R.id.img_icon){
                Toast.makeText(v.getContext(), sisop.get(getAdapterPosition()).nama, Toast.LENGTH_SHORT).show();
            }
        }
    }