Reputation: 43
I have a horizontal recycler view. There are some buttons. I will provide my code below. So the problem is that when I click on a button, does not happen anything. It just thinks that I did not click to it. I have another recycler view, but vertical and the code is pretty similar, but the second one works. I am traying to find a mistake for ages. I really hope you can help me. Thank you.
in my main:
adapter1 = new RecyclerViewAdapterContinentsList(this, idList,continentList);
recyclerViewForContinents.setAdapter(adapter1);
adapter1.setClickListener(new RecyclerViewAdapterContinentsList.ItemClickListener() {
@Override
public void onItemClick(View view, int position) {
Toast.makeText(getApplicationContext(),"name",Toast.LENGTH_SHORT).show();
}
});
recyclerView class:
public class RecyclerViewAdapterContinentsList extends RecyclerView.Adapter<RecyclerViewAdapterContinentsList.ViewHolder>{
private List<String> mId;
private List<String> mName;
private LayoutInflater mInflater;
private ItemClickListener mClickListener;
Context context;
RecyclerViewAdapterContinentsList(Context context, List<String> id, List<String> name) {
this.mInflater = LayoutInflater.from(context);
this.mId = id;
this.mName = name;
this.context = context;
}
// inflates the row layout from xml when needed
@Override
public ViewHolder onCreateViewHolder (ViewGroup parent, int viewType) {
View view = mInflater.inflate(R.layout.activity_recycler_view_adapter_countinent_list, parent, false);
return new ViewHolder(view);
}
// binds the data to the TextView in each row
@Override
public void onBindViewHolder(ViewHolder holder,final int position) {
String id = mId.get(position);
String name = mName.get(position);
holder.continentBtn.setText(name);
}
// total number of rows
@Override
public int getItemCount() {
return mName.size();
}
// stores and recycles views as they are scrolled off screen
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
Button continentBtn;
ViewHolder(View itemView) {
super(itemView);
continentBtn = itemView.findViewById(R.id.continentBtn);
itemView.setOnClickListener(this);
}
@Override
public void onClick(View view) {
if (mClickListener != null) {
Toast.makeText(context,"name",Toast.LENGTH_SHORT).show();
mClickListener.onItemClick(view, getAdapterPosition());}
}
}
// convenience method for getting data at click position
String getItem(int id) {
return mId.get(id);
}
void setClickListener(RecyclerViewAdapterContinentsList.ItemClickListener itemClickListener) {
Toast.makeText(context,"name",Toast.LENGTH_SHORT).show();
this.mClickListener = itemClickListener;
}
public interface ItemClickListener {
void onItemClick(View view, int position);
}
}
Upvotes: 0
Views: 688
Reputation: 13458
Change your ViewHolder code to attach the OnClickListener to the button and not the itemView.
ViewHolder(View itemView)
{
super(itemView);
continentBtn = itemView.findViewById(R.id.continentBtn);
continentBtn.setOnClickListener(this); // <-- button not itemView
}
In your ViewHolder, itemView is the root viewGroup of your layout R.layout.activity_recycler_view_adapter_countinent_list
, a RecyclerView can listen to click events on the entire row using setOnItemClickListener
.
Upvotes: 2
Reputation: 38
It is Easy ,
First you give id in your button view Than in your Adapter in onBindViewHolder
set listner in view and perform any event in listner.
@Override
public void onBindViewHolder(@NonNull final recycler_item_ads.ViewHolder holder, final int position) {
holder.installnow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//here print toast
}
}
});
holfer.install now is my button in layout file
Upvotes: 1