Reputation: 1054
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);
}
}
}
Upvotes: 30
Views: 14429
Reputation: 61
If Using RecyclerView in NestedScrollView add this line to RecyclerView :
android:nestedScrollingEnabled="false"
it will help you .
Upvotes: 0
Reputation: 498
For anyone having this issue today, your solution may be to just implement this in your gradle:
implementation "androidx.recyclerview:recyclerview:1.2.0-alpha04" //latest version to date
It's a bug that's been fixed
Upvotes: 0
Reputation: 698
Need to remove following 2 attributes, if present, from the root layout in the list item XML file
android:focusable="true"
android:focusableInTouchMode="true"
and it will skip the focusing part, and accept the first touch as Click. No need to go for any workaround
Upvotes: 2
Reputation: 309
If Using RecyclerView in NestedScrollView add this line to RecyclerView :
android:nestedScrollingEnabled="false"
I hope it help you.
Upvotes: 14
Reputation: 531
In my app I really needed to keep android:focusable="true" so, instead of using a clickListener I'm, using focus listener:
dataItemViewHolder.itemView.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus){
System.out.println("onFocusChange");
}
}
});
In my root view:
android:focusable="true"
android:focusableInTouchMode="true"
android:clickable="true"
You should just know that you will lose click effects and animations.
Upvotes: 2
Reputation: 383
There is also a known bug, for those of you who land here check out this question which references this workaround.
Upvotes: 6
Reputation: 825
Add these to your parent element of R.layout.item_app_list
android:clickable="false"
android:focusable="false"
Upvotes: 4
Reputation: 763
Add the Click-listener in ItemViewHolder, it works fine.
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);
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//getLayoutPosition is a method of RecyclerView
onItemClickListener.onItemClick(v,getLayoutPosition());
}
});
}
}
Upvotes: 1
Reputation: 2048
Just move your onClick in onBindViewHolder and set click as below, Hope it'll solve your issue....
holder.icon.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onItemClickListener.onItemClick(v,4);
}
});
Upvotes: 1
Reputation: 1054
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.
Upvotes: 23
Reputation: 2163
Move your onClickListner from onCreateViewHolder to onBindViewHolder
@Override
public void onBindViewHolder(ItemViewHolder holder, int position) {
final App app = mAppList.get(position);
holder.name.setText(app.getmName());
holder.icon.setImageDrawable(app.getmAppIcon());
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onItemClickListener.onItemClick(v , position);
}
});
}
Upvotes: 0