Tengo el siguiente codigo en el onCreate()
de la actividad que contiene al RecyclerView
:
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 CoursesAdapter(datos);
adapter.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
switch(view.getId()){
case R.id.buttonBuy:{
Toast.makeText(getApplicationContext(),
"Seleccion: "+recyclerView.getChildAdapterPosition(view)
,Toast.LENGTH_SHORT).show();
break;
}
}
}
});
Y el codigo de mi adaptador con el View.OnClickListener
implentado es este:
public class CoursesAdapter
extends RecyclerView.Adapter<CoursesAdapter.ViewHolder>
implements View.OnClickListener{
ArrayList<Course> dataSet;
private View.OnClickListener listener;
public void setOnClickListener(View.OnClickListener l){
this.listener = l;
}
@Override
public void onClick(View view) {
if(listener != null){
listener.onClick(view);
}
}
static class ViewHolder extends RecyclerView.ViewHolder{
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);
}
}
public CoursesAdapter(ArrayList<Course> dataSet){
this.dataSet = dataSet;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.
from(parent.getContext()).inflate(R.layout.card_course, parent, false);
view.setOnClickListener(this);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(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();
}
}
El adapter funciona bien, pero intento decirle un elemento especifico (R.id.buttonBuy
) en el setOnClickListener
pero no funciona.
Acabo de comprobar que si sirve el onClick
, pero quiero especificar un elemento (como un TexView
, etc) dentro del CardView que es el item.xml
asociado a los elementos del RecyclerView
.
//events
agrega el onClick listenera los elementos que quieras en especificoholder.title.setOnClickListener(this)
, esto te agregaria un listener alTextView title