Different onclick event for each part of a recyclerview

1

I have an app with a recyclerview which I have implemented View.OnClickListener in its respective adapter, all the functionality of OnClick works perfectly for each item in the recyclerview the problem is that I want it to behave differently depending on what part of the item it was "clicked"

To give an example with this image I want that if the user clicks the section (1) of the item a particular event occurs instead of that event happening if the item was clicked on any other part of it.

in conclusion if the user clicks the section (1) of the item, the app makes the Evento1 and so on with all the sections of the recyclerview item

    
asked by Juan Ar 28.10.2018 в 21:34
source

1 answer

0

I'm not sure if this is what you need.

When you want to give a section within a row in the recyclerView you must place a setOnClickListener for each section in which you want it to do something different, this must be done in your adapter.

In the example below you have two buttons (ITEM1 and ITEM2), so that each one performs a different action you must implement the OnClickListener within the onBinViewHolder(MyViewHolder holder, int position) method.

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {


public static class MyViewHolder extends RecyclerView.ViewHolder {


    public Button ITEM1;
    public Button ITEM1;

    public MyViewHolder(View v) {
        super(v);
        ITEM1 = (Button) v.findViewById(R.id.idITEM1);
        ITEM2 = (Button) v.findViewById(R.id.idITEM2);
    }
}



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

    holder.ITEM1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //Aquí va todo lo que necesites que haga cuando le des click en el ítem 1
        }
    });

    holder.ITEM2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //Aquí va todo lo que necesites que haga cuando le des click en el ítem 2
        }
    });
}}
    
answered by 29.10.2018 / 00:27
source