问题
What I have: A RecyclerView with pictures of places like bars, cofee stores, etc.
What I want: That when you click on one of these images I show you the info of the selected place
My question: How can i set the OnCLickListener for the third picture for example?
Many people have told me to do it on my "onBindViewHolder()" method, but it doesn't allow me to implement the "startActivity" so i can pass to the "Intent" Please, if you can explain with code it would be great, I am not good programming yet, so I would really appreciate
My Adapter
public class AdapterDatos extends RecyclerView.Adapter<AdapterDatos.ViewHolder> implements View.OnClickListener {
ArrayList<ClaseNueva> listalugares;
private View.OnClickListener listener;
public AdapterDatos(ArrayList<ClaseNueva> listaLugares) {
listalugares = listaLugares;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {//esto es lo que hacereferencia al xml donde vamos a meter la info
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_list,null,false);//aqui le asignamos el valor del view al viewHolder
view.setOnClickListener(this);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {// este metodo es el que se encarga de establecer la conexion entre el adaptador y la clase Viewholder ( a la cual le asignamos el xml)
holder.etiLugares.setText(listalugares.get(position).getLugares());// asi se asignan los textos
holder.Foto.setImageResource(listalugares.get(position).getFoto());//asi se asignan las fotos
}
@Override
public int getItemCount() {//este metodo va a decir el tamaño del viewHolder, en este caso de tamaño del array listalugares
return listalugares.size();//se hace asi
}
public void setOnClickListener(View.OnClickListener listener){
this.listener = listener;
}
@Override
public void onClick(View view) {
if(listener!= null){
listener.onClick(view);
}
}
public class ViewHolder extends RecyclerView.ViewHolder{
TextView etiLugares;
ImageView Foto;
Context context;
public ViewHolder(View itemView) {
super(itemView);
etiLugares = (TextView) itemView.findViewById(R.id.idNombre);//esto hace referencia a los elementos donde queremos meter la info
Foto = (ImageView) itemView.findViewById(R.id.idImagen);
}
}
}
My Java class
public class foodAndGo extends Activity {
ArrayList<ClaseNueva> listalugares;
RecyclerView recyclerLugares;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_food_and_go);
listalugares = new ArrayList<>();
recyclerLugares = (RecyclerView) findViewById(R.id.RecyclerID);
recyclerLugares.setLayoutManager(new LinearLayoutManager(this));
llenarNombres();
AdapterDatos adapter = new AdapterDatos(listalugares);
adapter.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),"Seleccion"+listalugares.get(recyclerLugares.getChildAdapterPosition(v)).getLugares(),Toast.LENGTH_SHORT).show();
Intent myIntent = new Intent(foodAndGo.this, MapsActivity.class);
startActivity(myIntent);
}
});
recyclerLugares.setAdapter(adapter);
}
private void llenarNombres() {
listalugares.add(new ClaseNueva("Restaurantes", R.drawable.carnemejor));
listalugares.add(new ClaseNueva("Bares", R.drawable.beers));
listalugares.add(new ClaseNueva("Cafeterías",R.drawable.desayunosmejor));
listalugares.add(new ClaseNueva("Pizzerias",R.drawable.pizzaamejor));
listalugares.add(new ClaseNueva("Favoritos",R.drawable.favoritosmejo));
}
}
回答1:
Inside foodAndGo activity change,
AdapterDatos adapter = new AdapterDatos(listalugares); to AdapterDatos adapter = new AdapterDatos(listalugares,foodAndGo.this);
and inside adapter class add this
private Context context;
public AdapterDatos(ArrayList<ClaseNueva> listaLugares,Context context) {
this.context=context;
listalugares = listaLugares;
}
Now, you can call startactivity in adapter's onBindViewHolder like this
Intent myIntent = new Intent(context, MapsActivity.class);
context.startActivity(myIntent);
来源:https://stackoverflow.com/questions/49411183/recyclerview-with-imageview-and-differents-activities