programmingandroid
programmingandroid

Reputation: 340

How to set Onclick for ImageViews generated dynamically in Adapter for RecyclerView

The ImageView is generated for every RecyclerView item and every item will have a different number of ImageViews. I have generated the ImageViews but now i have to set OnClick for every image generated in every item. I have no idea on how to do this. Any help would be appreciated.

ImageView Generating Code in Adapter :

 @Override
public void onBindViewHolder(ListHolder holder, final int position) {

    holder.name.setText(names[position]);
    // BELOW CODE IS THERE TO DYNAMICALLY ADD IMAGES
    LinearLayout ll = new LinearLayout(c);
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    ll.setLayoutParams(params);
    for(int i=0;i<num[position];i++)
    {
        holder.image = new ImageView(c);
        holder.image.setLayoutParams(new ViewGroup.LayoutParams(200,
                200));
        holder.image.setImageResource(R.mipmap.ic_launcher);
        ll.addView(holder.image);
    }
    holder.horizontalScrollView.addView(ll);
}

The above code is inside the adapter, i will be more than happy to provide you all of the code in case you need it. Please tell me how to Handle the click events of these images generated. Thanks.

Upvotes: 0

Views: 739

Answers (2)

Mohammed Sameer Ahmad
Mohammed Sameer Ahmad

Reputation: 437

 @Override
public void onBindViewHolder(ListHolder holder, final int position) {

holder.name.setText(names[position]);
// BELOW CODE IS THERE TO DYNAMICALLY ADD IMAGES
LinearLayout ll = new LinearLayout(c);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
ll.setLayoutParams(params);
for(int i=0;i<num[position];i++)
{
    ImageView img = new ImageView(c);
    img.setLayoutParams(new ViewGroup.LayoutParams(200,
            200));
    img.setImageResource(R.mipmap.ic_launcher);
    img.setId(i);

  ll.addView(img);

    img.setOnClickListner(OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    }));


}
holder.horizontalScrollView.addView(ll);
}

Upvotes: 1

ishmaelMakitla
ishmaelMakitla

Reputation: 3812

You can simply do that on the holder.image object. Here is an example based on your code:

for(int i=0;i<num[position];i++)
    {
        holder.image = new ImageView(c);
        holder.image.setLayoutParams(new ViewGroup.LayoutParams(200,
                200));
        holder.image.setImageResource(R.mipmap.ic_launcher);
        //setting the onClickListener to image
        holder.image.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                //image clicked, do stuff
            }
        });
        ll.addView(holder.image);
    }

I hope this helps. Give it a try and let me know.

Upvotes: 1

Related Questions