2017-01-03 3 views
0

Ich habe es seit 2 Tagen versucht, implementieren Sie den ItemClickListener in einem RecyclerView und es funktioniert nicht. bitte helfenImplementieren Sie den ItemClickListener in einem recyclerView

Wie kann ich den ItemClickListener in dieser .class implementieren?

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()); 
        } 
       } 
      } 
     } 
+0

Anstatt den 'OnClickListener' auf den' viewHolder' zu setzen, sollten Sie ihn innerhalb des ViewHolders auf eine 'view' setzen, also entweder' NetWorkImageView' oder 'TextView'. Sie können auch die onClick Zuhörer auf die ganze Sicht des einzelnen Artikel hinzufügen, indem Sie die OnClickListener zu der Ansicht hinzugefügt, nachdem Sie es aufgeblasen haben in 'onCreateViewHolder' –

+0

diesen Link zu sehen: http://stackoverflow.com/questions/24885223/why -doesnt-recyclerview-have-onitemclicklistener-und-how-recyclerview-is-dif es hat eine detail-implementierung – rafsanahmad007

Antwort

0

Sie können diesen Code verwenden

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); 
    } 
} 
+0

In line: * Toast.makeText (Platos_Adapter.this, "geklickt", Toast.LENGTH_SHORT) .show(); * Dies ist Dies ist rot unterstrichen. * Estadisticas comida = estadisticas.get (getAdapterPosition()); * Die Methode creat implementieren * getAdapterPosition * ist rot. Wie wird es gemacht? –

+0

Fehler: (47, 22) Fehler: keine geeignete Methode gefunden für makeText (Platos_Adapter, String, int) Methode Toast.makeText (Kontext, CharSequence, int) ist nicht anwendbar (Argument nicht übereinstimmen; Platos_Adapter kann nicht in Context konvertiert werden) Methode Toast .makeText (Kontext, int, int) ist nicht anwendbar (Argument stimmt nicht überein; Platos_Adapter kann nicht in Kontext konvertiert werden) *** Fehler: (48, 56) error: kann keine Symbolmethode finden getAdapterPosition() –

+0

Bitte ersetzen Sie den aktualisierten Code der Überprüfung –

2

In RecyclerView, gibt es keine direkte Unterstützung wie onItemClickListener jedoch müssen Sie auf Ihrem Adapter Klasse wie diese, die Sie kann helfen ist.

public class ViewHolder extends RecyclerView.ViewHolder{ 
    View view; 

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

und Sie haben auf onBindViewHolder Onclick Methode Feuer,

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()); 
    } 
}); 

Ich denke, das ist die Art und Weise der wir den Blick auf recyclerview klicken.

+0

Fehler : (47, 22) error: keine passende Methode gefunden für makeText (Platos_Adapter, String, int) Methode Toast.makeText (Kontext, CharSequence, int) ist nicht anwendbar (Argument nicht übereinstimmend; Platos_Adapter kann nicht in Kontext konvertiert werden) Methode Toast.makeText (Context, int, int) ist nicht anwendbar (Argument stimmt nicht überein; Platos_Adapter kann nicht in Contex konvertiert werden t) *** Fehler: (48, 56) error: kann die Symbolmethode getAdapterPosition() nicht finden –

+0

Sie können einfach Kontext anstelle von Context verwenden. –

0

Ich möchte @Dharma Kshetri und @Rahul Pareta für ihre Antworten danken. Danke Dies ist die Lösung:

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); 
     } 
    } 
} 

Und 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> 

Und 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); 
     } 
    } 
} 
Verwandte Themen