Buenas, tengo el siguiente codigo donde se implementa un RecyclerView
y eventos a los botones del card_view.xml
que compone cada elemento del listado del Recycler, sin embargo, quisiera saber como saber a que card_view de la lista pertenece el evento, por ejemplo darle click a un boton de "Comprar" e identificar el compoente padre al que pertenece (para diferenciar la compra).
Aquí el codigo:
public class ViewCoursesActivity extends AppCompatActivity {
RecyclerView recyclerView;
RecyclerView.LayoutManager layoutManager;
private CourseAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_courses);
recyclerView = findViewById(R.id.recyclerViewCourses);
recyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
ArrayList<Course> datos = new ArrayList<>();
datos.add(new Course("Relational databases", 500,
"A complete course about relational databases.", R.drawable.bbdd));
datos.add(new Course("OOP", 500,
"A complete course about objects oriented programming.", R.drawable.objetos));
datos.add(new Course("Android", 0,
"A complete course about Android.", R.drawable.android));
adapter = new CourseAdapter(datos, R.layout.card_course,
this, new CourseAdapter.OnItemClickListener() {
@Override
public void onItemClickListener(View view) {
switch (view.getId()){
case R.id.buttonBuy:{
Toast.makeText(getApplicationContext(), "Funciona", Toast.LENGTH_SHORT).show();
break;
}
}
}
});
recyclerView.setAdapter(adapter);
}
}
Y el adapter así, donde implementé el OnItemClickListener en el ViewHolder, etc.
public class CourseAdapter extends RecyclerView.Adapter<CourseAdapter.ViewHolder>{
ArrayList<Course> dataSet;
private int layout;
private Activity activity;
private static OnItemClickListener listener;
public CourseAdapter(ArrayList<Course> dataSet, int layout, Activity activity, OnItemClickListener listener) {
this.dataSet = dataSet;
this.layout = layout;
this.activity = activity;
this.listener = listener;
}
@Override
public CourseAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(layout, parent, false);
return new ViewHolder(v);
}
@Override
public void onBindViewHolder(CourseAdapter.ViewHolder holder, int position) {
holder.title.setText(dataSet.get(position).getName());
if(dataSet.get(position).getCost() == 0){
holder.cost.setText("FREE");
}else{
holder.cost.setText("$"+dataSet.get(position).getCost());
}
holder.description.setText(dataSet.get(position).getDescription());
holder.image.setImageResource(dataSet.get(position).getCover());
//events
}
@Override
public int getItemCount() {
return dataSet.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
TextView title;
TextView description;
TextView cost;
ImageView image;
Button buttonBuy, buttonSeeMore;
Context context;
public ViewHolder(View itemView) {
super(itemView);
context = itemView.getContext();
title = itemView.findViewById(R.id.textViewTitle);
description = itemView.findViewById(R.id.textViewDescription);
cost = itemView.findViewById(R.id.textViewCost);
image = itemView.findViewById(R.id.imageViewCover);
buttonBuy = itemView.findViewById(R.id.buttonBuy);
buttonSeeMore = itemView.findViewById(R.id.buttonSeeMore);
buttonBuy.setOnClickListener(this);
buttonSeeMore.setOnClickListener(this);
}
@Override
public void onClick(View view) {
// Llamas el método onItemClickListener() de la interfaz OnItemClickListener
listener.onItemClickListener(view);
}
}
public interface OnItemClickListener{
// Este método recibe como parámetro la vista del elemento seleccionado
void onItemClickListener(View view);
}
}