I am trying to receive clicks on RecyclerView
items for which I am using callback mechanism. I have created OnItemClickListener
Interface and inside custom Adapter, I have written logic for detecting click with the help of View.OnClickListener
. But I am getting a callback in myActivity after double clicking any item in the list. Not getting any clue what's happening here!
Code inside Activity:
mAdapter = new AppAdapter(this, mAppList, new OnItemClickListener() { @Override public void onItemClick(View v, int position) { Toast.makeText(SelectAppActivity.this, "Hello", Toast.LENGTH_SHORT).show(); } }); recyclerview.setAdapter(mAdapter);
Code For Interface:
public interface OnItemClickListener { void onItemClick(View v, int position); }
Code for Custom Adapter:
public class AppAdapter extends RecyclerView.Adapter<AppAdapter.ItemViewHolder> { List<App> mAppList; List<App> mFilterAppList; Activity context; OnItemClickListener onItemClickListener; public AppAdapter(Activity context, List<App> appList, OnItemClickListener onItemClickListener) { super(); this.context = context; this.mAppList = appList; this.mFilterAppList = appList; this.onItemClickListener = onItemClickListener; } @Override public ItemViewHolder onCreateViewHolder(final ViewGroup parent, int viewType) { final View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_app_list, parent, false); final ItemViewHolder viewHolder = new ItemViewHolder(view); view.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { onItemClickListener.onItemClick(v,4); } }); return viewHolder; } @Override public void onAttachedToRecyclerView(RecyclerView recyclerView) { super.onAttachedToRecyclerView(recyclerView); } @Override public void onBindViewHolder(ItemViewHolder holder, int position) { final App app = mAppList.get(position); holder.name.setText(app.getmName()); holder.icon.setImageDrawable(app.getmAppIcon()); } @Override public int getItemCount() { return mAppList.size(); } public void setFilter(List<App> appList) { mAppList = new ArrayList<>(); mAppList.addAll(appList); notifyDataSetChanged(); } public static class ItemViewHolder extends RecyclerView.ViewHolder { public TextView name; public ImageView icon; public ItemViewHolder(View view) { super(view); name = (TextView) itemView.findViewById(R.id.app_name); icon = (ImageView) itemView.findViewById(R.id.app_icon); } } }
The issue was related to android:focusable="true" in my layout, as I removed this it was working fine,all suggested answers are also working fine.Thanks, all.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With