2

item of my recycler view has some different views which has to have a click listener if i add a click listener to recycler view with view and position parameters it always take the layout behind the views on click. Therefore, i am setting my click listeners in onBindViewHolder like this:

holder.myTextView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // stuff
        }
    });

holder.myImageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // stuff
        }
    });

I wonder whether it is a correct method?

shanks
  • 342
  • 2
  • 4
  • 16

1 Answers1

1

Where should a click listener be in a recycler view?

There are many ways for achieve this, but ;

Both is correct as i see on your codes.Means, if you set those items with myTextView, the users will be able to click on TextView (f.g) for showing that RecyclerView item content or whatever and other stuffs like ImageView won't work for listener.

And this is correct for myImageView too, if you set those items with it for listener, the users will be able to click only in this myImageView.

By the way, i think it is better to use implements View.OnClickListener for whole of the Holder.

Something like this:

https://stackoverflow.com/a/24471410/4409113

or Jacob's answer

Community
  • 1
  • 1
ʍѳђઽ૯ท
  • 16,646
  • 7
  • 53
  • 108
  • thank u, also, is there a big performance difference between implementing a view.onclicklistener and setting listeners in onbindviewholder for each item? – shanks Jan 15 '16 at 18:25
  • Yes i hope so, i was doing the same thing for each item like you said, and i just thought about it, seems like that makes sense, imagine it, we could use whole of `RowHolder` instead of those duplicates or bad way.A lot of codes for handling an easy way sounds crazy, right? :) – ʍѳђઽ૯ท Jan 15 '16 at 18:30
  • in jacob's answer when i try onclick method with view.getClass() it always return linearlayout which is the background. It does not detect the buttons or other views in front of the layout. – shanks Jan 15 '16 at 20:05
  • it works thank you so much. I appreciate it if you check this question http://stackoverflow.com/questions/34818380/imagedetailsactivity-for-a-recycler-view – shanks Jan 15 '16 at 20:21