I'm setting up a RecyclerView who contains mulptiple ImageButtons, Each ImageButton has a OnClick action (launch an Activity) My problem is, the OnClick action is not working on ImageButtons, but in a little space between each ImageButton (if I want to click on 1st button, I have to click on the space between 1st and 2nd Button)
I've tried changing image size
RecyclerView Adapter:
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder>
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = mInflater.inflate(R.layout.recycler_view, parent, false);
return new ViewHolder(view);
}
// binds the data to the ImageButton in each row
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
int currentCategory = mData.get(position).getImage();
holder.categoriesButton.setImageResource(currentCategory);
}
// stores and recycles views as they are scrolled off screen
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
ImageButton categoriesButton;
ViewHolder(View itemView) {
super(itemView);
categoriesButton = itemView.findViewById(R.id.image_Button_Category_id);
itemView.setOnClickListener(this);
}
@Override
public void onClick(View view) {
if (mClickListener != null) mClickListener.onItemClick(view, getAdapterPosition());
}
}
// convenience method for getting data at click position
public int getItem(int id) {
return mData.get(id).getImage();
}
// allows clicks events to be caught
public void setClickListener(ItemClickListener itemClickListener) {
this.mClickListener = itemClickListener;
}
// parent activity will implement this method to respond to click events
public interface ItemClickListener {
void onItemClick(View view, int position);
}
RecyclerLayout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageButton
android:id="@+id/image_Button_Category_id"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
And this is in my MainActivity:
private void setRecyclerViewOn(ArrayList<Category> list) {
RecyclerView recyclerView = findViewById(R.id.recyclerView_id);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
adapter = new RecyclerViewAdapter(this, list);
adapter.setClickListener(this);
recyclerView.setAdapter(adapter);
}
@Override
public void onItemClick(View view, int position) {
Toast.makeText(this, "You clicked " + adapter.getItem(position) + " on row number " + position, Toast.LENGTH_SHORT).show();
startActivity(createIntent(position));
}
Expected results: Click listener have to be on each ImageButton
Try adding on click listener on onBindView() instead
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
int currentCategory = mData.get(position).getImage();
holder.categoriesButton.setImageResource(currentCategory);
holder.vategoriesButton.setOnClickListener(...)
}