0

I've been trying it for 2 days, implement the ItemClickListener in a recyclerView and it does not work. please help

How can I implement the ItemClickListener in this .class?

public class Platos_Adapter extends
    RecyclerView.Adapter<Platos_Adapter.ViewHolder> {

            private ImageLoader imageLoader;
            private Context context;

            List<Estadisticas> estadisticas;

            public Platos_Adapter(List<Estadisticas> comida,Context context){
                super();
                //Getting all the comida
                this.estadisticas = estadisticas;
                this.context = context;
            }

            @Override
            public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.platos_row,
    parent, false);
                ViewHolder viewHolder = new ViewHolder(v);
                return viewHolder;
            }

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

                holder.setOnClickListener((View.OnClickListener) this);

                Estadisticas superHero =  estadisticas.get(position);

                imageLoader = DecoracionLineaDivisoria.CustomVolleyRequest.getInstance(context).getImageLoader();

                imageLoader.get(superHero.getEscudo_Local(), ImageLoader.getImageListener(holder.escudo_local,
    R.mipmap.ic_launcher, android.R.drawable.ic_dialog_alert));

                holder.escudo_local.setImageUrl(superHero.getEscudo_Local(),
    imageLoader);
                holder.textViewEquipo_Local.setText(superHero.getEquipo_Local());
            }

            @Override
            public int getItemCount() {
                return estadisticas.size();
            }

            class ViewHolder extends RecyclerView.ViewHolder{
                public NetworkImageView escudo_local;
                public TextView textViewEquipo_Local;

                public ViewHolder(View itemView) {
                    super(itemView);

                    escudo_local = (NetworkImageView) itemView.findViewById(R.id.tv_esc_local);
                    textViewEquipo_Local= (TextView) itemView.findViewById(R.id.tv_ek_local);
        ;        }


                public void setOnItemClickListener(ViewHolder.OnItemClickListener
    onClickListener) {

                    public void onClick(View view) {

                        Toast.makeText(Platos_Adapter.this, "clicked" ,Toast.LENGTH_SHORT).show();

                        Estadisticas comida = estadisticas.get(getAdapterPosition());
                    }
                }
            }
        }
Horrorgoogle
  • 7,858
  • 11
  • 48
  • 81
Rafel C.F
  • 179
  • 1
  • 10
  • Instead of setting the `OnClickListener` to the `viewHolder`, you should set it to a `view` inside the ViewHolder i.e. either the `NetWorkImageView` or the `TextView`. You can also add the onClick listener to the whole view of the individual item by adding the onClickListener to the view after you have inflated it in `onCreateViewHolder` – harshithdwivedi Jan 03 '17 at 19:51
  • see this link: http://stackoverflow.com/questions/24885223/why-doesnt-recyclerview-have-onitemclicklistener-and-how-recyclerview-is-dif it has a detail implementation – rafsanahmad007 Jan 03 '17 at 20:01

3 Answers3

2

In RecyclerView, there is not direct support just like onItemClickListener, however, you have to on your adapter class just like this, which is may help you.

 public class ViewHolder extends RecyclerView.ViewHolder{
    View view;

    public ViewHolder(View itemView) {
       super(itemView);
       view=itemView;
 }
}

and you have to fire onclick method on onBindViewHolder,

 holder.view.setOnClickListener(new View.OnClickListener() {
        @Override
      public void onClick(View view) {
         Toast.makeText(Platos_Adapter.this, "clicked" ,Toast.LENGTH_SHORT).show();

         Estadisticas comida = estadisticas.get(getAdapterPosition());
  }
 });

I think , this is the way of we can click the view on recyclerview.

Horrorgoogle
  • 7,858
  • 11
  • 48
  • 81
  • Error:(47, 22) error: no suitable method found for makeText(Platos_Adapter,String,int) method Toast.makeText(Context,CharSequence,int) is not applicable (argument mismatch; Platos_Adapter cannot be converted to Context) method Toast.makeText(Context,int,int) is not applicable (argument mismatch; Platos_Adapter cannot be converted to Context)***Error:(48, 56) error: cannot find symbol method getAdapterPosition() – Rafel C.F Jan 04 '17 at 09:47
  • you can just use context rather than Context . – Horrorgoogle Jan 04 '17 at 09:53
0

You can use this code

public class Platos_Adapter extends RecyclerView.Adapter {
private ImageLoader imageLoader;
private Context context;
List<Estadisticas> estadisticas;

public Platos_Adapter(List<Estadisticas> comida,Context context){
    super();
    //Getting all the comida
    this.estadisticas = estadisticas;
    this.context = context;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.platos_row, parent, false);
    ViewHolder viewHolder = new ViewHolder(v);
    return viewHolder;
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    holder.root.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Toast.makeText(context, "clicked" ,Toast.LENGTH_SHORT).show();
            Estadisticas comida = estadisticas.get(getAdapterPosition());
        }
    });
    Estadisticas superHero =  estadisticas.get(position);
    imageLoader = DecoracionLineaDivisoria.CustomVolleyRequest.getInstance(context).getImageLoader();
    imageLoader.get(superHero.getEscudo_Local(), ImageLoader.getImageListener(holder.escudo_local, R.mipmap.ic_launcher, android.R.drawable.ic_dialog_alert));
    holder.escudo_local.setImageUrl(superHero.getEscudo_Local(), imageLoader);
    holder.textViewEquipo_Local.setText(superHero.getEquipo_Local());
}

@Override
public int getItemCount() {
    return estadisticas.size();
}

class ViewHolder extends RecyclerView.ViewHolder{
    public NetworkImageView escudo_local;
    public TextView textViewEquipo_Local;
    public View root;

    public ViewHolder(View itemView) {
        super(itemView);
        root=itemView;
        escudo_local = (NetworkImageView) itemView.findViewById(R.id.tv_esc_local);
        textViewEquipo_Local= (TextView) itemView.findViewById(R.id.tv_ek_local);
    }
}
Rahul Pareta
  • 786
  • 6
  • 18
  • In line:*Toast.makeText(Platos_Adapter.this, "clicked" , Toast.LENGTH_SHORT).show();*this is This is underlined in red. *Estadisticas comida = estadisticas.get(getAdapterPosition());* Implement creat method *getAdapterPosition* is in red. How is it done? – Rafel C.F Jan 04 '17 at 09:29
  • Error:(47, 22) error: no suitable method found for makeText(Platos_Adapter,String,int) method Toast.makeText(Context,CharSequence,int) is not applicable (argument mismatch; Platos_Adapter cannot be converted to Context) method Toast.makeText(Context,int,int) is not applicable (argument mismatch; Platos_Adapter cannot be converted to Context)***Error:(48, 56) error: cannot find symbol method getAdapterPosition() – Rafel C.F Jan 04 '17 at 09:50
0

I want to thank @Dharma Kshetri and @Rahul Pareta for their responses. Thank you This is the solution:

public class Platos_Adapter extends RecyclerView.Adapter<Platos_Adapter.ViewHolder> {
    private ImageLoader imageLoader;
    private Context context;
    List<Estadisticas> estadisticas;

    public Platos_Adapter(List<Estadisticas> estadisticas, Context context) {
        super();
        this.estadisticas = estadisticas;
        this.context = context;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from( parent.getContext() ).inflate( R.layout.platos_row, parent, false );
        ViewHolder viewHolder = new ViewHolder( v );
        return viewHolder;
    }

    public void onBindViewHolder(ViewHolder holder, final int position) {
        holder.root.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Toast.makeText(context, estadisticas.get(position).getEquipo_Local(), Toast.LENGTH_SHORT).show();

                Estadisticas estadisticas1 = estadisticas.get( getAdapterPosition() );
            }

            private int getAdapterPosition() {
                return 0;
            }
        } );
        Estadisticas superHero = estadisticas.get( position );
        imageLoader = DecoracionLineaDivisoria.CustomVolleyRequest.getInstance( context ).getImageLoader();
        imageLoader.get( superHero.getEscudo_Local(), ImageLoader.getImageListener( holder.escudo_local, R.mipmap.ic_launcher, android.R.drawable.ic_dialog_alert ) );
        holder.escudo_local.setImageUrl( superHero.getEscudo_Local(), imageLoader );
        holder.textViewEquipo_Local.setText( superHero.getEquipo_Local() );
    }

    @Override
    public int getItemCount() {
        return estadisticas.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        public NetworkImageView escudo_local;
        public TextView textViewEquipo_Local;
        public View root;

        public ViewHolder(View itemView) {
            super( itemView );
            root = itemView;
            escudo_local = (NetworkImageView) itemView.findViewById( R.id.tv_esc_local );
            textViewEquipo_Local = (TextView) itemView.findViewById( R.id.tv_ek_local );
            textViewCategoria = (TextView) itemView.findViewById( R.id.tv_ek_visi );
        }
    }
}

And activity_detail.xml

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="@dimen/detail_backdrop_height"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    android:fitsSystemWindows="true">

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/collapsing_toolbar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_scrollFlags="scroll|exitUntilCollapsed"
        android:fitsSystemWindows="true"
        app:contentScrim="?attr/colorPrimary"
        app:expandedTitleMarginStart="48dp"
        app:expandedTitleMarginEnd="64dp">

        <ImageView
            android:id="@+id/backdrop"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scaleType="centerCrop"
            android:fitsSystemWindows="true"
            app:layout_collapseMode="parallax" />

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            app:layout_collapseMode="pin" />

    </android.support.design.widget.CollapsingToolbarLayout>

</android.support.design.widget.AppBarLayout>

<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:paddingTop="24dp">

        <android.support.v7.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="@dimen/card_margin">

            <LinearLayout
                style="@style/Widget.CardContent"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <TextView
                    android:id="@+id/tv_info_title"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="yyyyyyyyyyyyyyyyyy"
                    android:textAppearance="@style/TextAppearance.AppCompat.Title" />

                <TextView
                    android:id="@+id/tv_info"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="ddddddddddddddd" />

            </LinearLayout>

        </android.support.v7.widget.CardView>

        <android.support.v7.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="@dimen/card_margin"
            android:layout_marginLeft="@dimen/card_margin"
            android:layout_marginRight="@dimen/card_margin">

            <LinearLayout
                style="@style/Widget.CardContent"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <TextView
                    android:id="@+id/tv_categoria_title"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="mmmmmmmmmmmmmmmmmmm"
                    android:textAppearance="@style/TextAppearance.AppCompat.Title" />

                <TextView
                    android:id="@+id/tv_categoria"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="aaaaaaaaaaaaaaaaaaaaaa" />

            </LinearLayout>

        </android.support.v7.widget.CardView>

        <android.support.v7.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="@dimen/card_margin"
            android:layout_marginLeft="@dimen/card_margin"
            android:layout_marginRight="@dimen/card_margin">

            <LinearLayout
                style="@style/Widget.CardContent"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="xxxxxxxxxxxxxxxxx"
                    android:textAppearance="@style/TextAppearance.AppCompat.Title" />

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="ddddddddddd" />

            </LinearLayout>

        </android.support.v7.widget.CardView>

    </LinearLayout>

</android.support.v4.widget.NestedScrollView>

And PlatosAdapter.java

public class Platos_Adapter extends RecyclerView.Adapter<Platos_Adapter.ViewHolder> {

    private ImageLoader imageLoader;
    private Context context;

    List<Estadisticas> estadisticas;

    public Platos_Adapter(List<Estadisticas> estadisticas, Context context) {
        super();
        this.estadisticas = estadisticas;
        this.context = context;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from( parent.getContext() ).inflate( R.layout.platos_row, parent, false );
        ViewHolder viewHolder = new ViewHolder( v );
        return viewHolder;
    }

    public void onBindViewHolder(ViewHolder holder, final int position) {
        holder.root.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                //Toast.makeText(context, "this is my Toast message!!! =)",
                        //Toast.LENGTH_LONG).show();

                //Toast.makeText(context, estadisticas.get(position).getEquipo_Local(), Toast.LENGTH_SHORT).show();

                Intent intent= new Intent(context, DetailActivity.class);

                intent.putExtra("Nombre", estadisticas.get(position).getEquipo_Local());
                intent.putExtra("Categoria", estadisticas.get(position).getEquipo_Visitante());
                intent.putExtra("Imagen", estadisticas.get(position).getEscudo_Local());

                context.startActivity(intent);


                Estadisticas estadisticas1 = estadisticas.get( getAdapterPosition() );
            }

            private int getAdapterPosition() {
                return 0;
            }
        } );
        Estadisticas superHero = estadisticas.get( position );
        imageLoader = DecoracionLineaDivisoria.CustomVolleyRequest.getInstance( context ).getImageLoader();
        imageLoader.get( superHero.getEscudo_Local(), ImageLoader.getImageListener( holder.escudo_local, R.mipmap.ic_launcher, android.R.drawable.ic_dialog_alert ) );
        holder.escudo_local.setImageUrl( superHero.getEscudo_Local(), imageLoader );
        holder.textViewEquipo_Local.setText( superHero.getEquipo_Local() );
        //holder.textViewCategoria.setText( superHero.getEquipo_Visitante() );
    }

    @Override
    public int getItemCount() {
        return estadisticas.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        public NetworkImageView escudo_local;
        public TextView textViewEquipo_Local;
        //public TextView textViewCategoria;
        public View root;

        public ViewHolder(View itemView) {
            super( itemView );
            root = itemView;
            escudo_local = (NetworkImageView) itemView.findViewById( R.id.tv_esc_local );
            textViewEquipo_Local = (TextView) itemView.findViewById( R.id.tv_ek_local );
            //textViewCategoria = (TextView) itemView.findViewById( R.id.tv_ek_visi );
        }
    }
}
Rafel C.F
  • 179
  • 1
  • 10