Home > Blockchain >  How to add onclickListener on images using Glide
How to add onclickListener on images using Glide

Time:04-14

I am trying to attach onclick listener on images in this code, using Glide framework but are not able to find solution for that. Is is somebody who can help with this?

Images are showing in emulator allready.

Thanks in advance

package com.example.andoridlifecycle.adapters

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.Toast
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.example.andoridlifecycle.R
import kotlinx.android.synthetic.main.item_custom_row.view.*

class ItemAdapter(private val context: Context, private val urls: ArrayList<String>) :
    RecyclerView.Adapter<ItemAdapter.ViewHolder>() {


    /**
     * Inflates the custom view which is designed in xml layout file
     */
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        return ViewHolder(
            LayoutInflater.from(context).inflate(
                R.layout.item_custom_row,
                parent,
                false
            )
        )
    }


    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        // Array with urls
        val url = urls[position]

        Glide.with(context)
            .load(url)
            .diskCacheStrategy(DiskCacheStrategy.ALL)
            .placeholder(R.drawable.placeholder)
            .into(holder.imageView)
    }

    // Gets the number of items in the list
    override fun getItemCount(): Int {
        return urls.size
    }
     // A ViewHolder describes an item view and metadata about its place within the RecyclerView.

    class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {

        val imageView: ImageView = view.iv_image
     }
}

CodePudding user response:

I'm Java Developer so I cannot code with Kotlin but I'll show you how to set OnClickListener on your imageView. For using OnclickListener on imageView no need for Glide Library. Just add the below lines in your onBindViewHolder

holder.imageView.setOnClickListener(view -> {
            Toast.makeText(context, "this is number: " position " Image Selected", Toast.LENGTH_SHORT).show();
            // or your code for imageView
        });

Your new code looks like the below:-

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        // Array with urls
        val url = urls[position]

//code for onClick on image
holder.imageView.setOnClickListener(view -> {
            Toast.makeText(context, "this is number: " position " Image Selected", Toast.LENGTH_SHORT).show();
            // or your code for imageView
        });


        Glide.with(context)
            .load(url)
            .diskCacheStrategy(DiskCacheStrategy.ALL)
            .placeholder(R.drawable.placeholder)
            .into(holder.imageView)
    }

Make sure to change JAVA to Kotlin in my code. If you any problem is causing, Let me know

  • Related