I have created a interface for extending functionality of Recycler View and making possible to listen click on every item of list, but i can't make it to work. Nothing happens when i click on any item. Here is my code:
// METHOD FOR INITIALIZATION OF RECYCLER VIEW
public void initRecyclerView() {
// SETTING ARRAY FOR CATEGORIES
final CategoryItem categoryItem[] = { new CategoryItem("Birthday", R.drawable.birthday_image),
new CategoryItem("Anniversary", R.drawable.anniversary_image),
new CategoryItem("Anniversary", R.drawable.anniversary_image),
new CategoryItem("Anniversary", R.drawable.anniversary_image),
new CategoryItem("Anniversary", R.drawable.anniversary_image),
new CategoryItem("Anniversary", R.drawable.anniversary_image),
new CategoryItem("Anniversary", R.drawable.anniversary_image),
new CategoryItem("Anniversary", R.drawable.anniversary_image)};
// FINDING RECYCLER VIEW IN LAYOUT
recyclerView = (RecyclerView) findViewById(R.id.list);
recyclerView.setLayoutManager(new GridLayoutManager(this, 2));
adapter = new CategoriesListAdapter(categoryItem);
recyclerView.setAdapter(adapter);
// SETTING ON CLICK LISTENER ON ADAPTER
adapter.setOnItemClickListener(new OnItemClickListener() {
@Override
public void setOnItemClick(View view, int position) {
String clickedItem = String.valueOf(position);
if ( clickedItem.equals("Birthday") ) {
Toast.makeText(MainActivity.this, "Clicked on Birthday!", Toast.LENGTH_SHORT).show();
}
}
});
recyclerView.setItemAnimator(new DefaultItemAnimator());
}
UPDATE:
adapter
public class CategoriesListAdapter extends RecyclerView.Adapter<CategoriesListAdapter.ViewHolder> {
private CategoryItem[] categoryItems;
private static OnItemClickListener onItemClickListener;
public CategoriesListAdapter(CategoryItem[] categoryItems) {
this.categoryItems = categoryItems;
}
@Override
public CategoriesListAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemLayoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_row, parent, false);
ViewHolder viewHolder = new ViewHolder(itemLayoutView);
return viewHolder;
}
@Override
public void onBindViewHolder(final ViewHolder viewHolder, final int position) {
viewHolder.txtViewTitle.setText(categoryItems[position].getTitle());
viewHolder.imgViewIcon.setImageResource(categoryItems[position].getImage());
}
// inner class to hold a reference to each item of RecyclerView
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView txtViewTitle;
public ImageView imgViewIcon;
public ViewHolder(View itemLayoutView) {
super(itemLayoutView);
txtViewTitle = (TextView) itemLayoutView.findViewById(R.id.title);
imgViewIcon = (ImageView) itemLayoutView.findViewById(R.id.image);
itemLayoutView.setOnClickListener(this);
}
@Override
public void onClick(View v) {
onItemClickListener.setOnItemClick(v, getAdapterPosition());
}
}
public void setOnItemClickListener (final OnItemClickListener onItemClickListener) {
this.onItemClickListener = onItemClickListener;
}
// Return the size of your itemsData (invoked by the layout manager)
@Override
public int getItemCount() {
return categoryItems.length;
}
}
INTERFACE:
public interface OnItemClickListener {
public void setOnItemClick(View view, int position);
}
It should display toast message if birthday is selected, but it's not displaying anything. I'm instancing array of class and i don't know how to listen clicked item. The answers there didn't help me solving my problem.
Currently you are checking your position against string "Birthday" in OnItemClickListener method setOnItemClick
String clickedItem = String.valueOf(position);
if ( clickedItem.equals("Birthday") ) {
...
}
which never gonna be true. Try getting item for position and checking its first parameter against "Birthday" or bring your Toast out from that if statement and just print out position number for beginning to understand what's going on in there
@Override
public void setOnItemClick(View view, int position) {
Toast.makeText(MainActivity.this, "Clicked item on position: " + position, Toast.LENGTH_SHORT).show();
}