30

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);
        }
    }
}
Richard
  • 560
  • 1
  • 8
  • 17
nlogn
  • 1,034
  • 4
  • 11
  • 17
  • `onClick ` method is executed on single click? – ρяσѕρєя K Jul 07 '16 at 09:59
  • sorry!! but did'nt get you – nlogn Jul 07 '16 at 10:02
  • i mean add a log inside onClick method of `view.setOnClickListener` and check it is calling every time or only on double click – ρяσѕρєя K Jul 07 '16 at 10:04
  • it is getting called every time on double click – nlogn Jul 07 '16 at 10:08
  • Why are you writing onClick for adapter's row view if you had already written its itemClick, write any one of them , either onClick or onItemClick and try. – Dhruvi Jul 07 '16 at 10:51
  • I am writing onClick on adapter's row to detect click on that row and as soon as it gets clicked I want the callback in my activity with the help of onItemClick – nlogn Jul 07 '16 at 11:08
  • can you debug and check does this onItemClickListener.onItemClick(v,4) get call on first click ? –  Jul 07 '16 at 12:18
  • try to have class level View.OnClickListener rather than creating anonymous listener for every new view. –  Jul 07 '16 at 12:21
  • i got onClick listener in adapter class of recyclerview and in onBindViewHolder itself, no Focus enabled in XML, but when i click it showing toast that is in onClick Event but its not working for setText and update the cell – Goblin シ Nov 03 '17 at 10:24
  • 1
    @nlogn I am having the same issue,but none of the solutions work for me. I didn't enable focus and clickable in my xml. Can you help me to solve it? – Visakh Sep 07 '18 at 09:39

11 Answers11

22

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.

nlogn
  • 1,034
  • 4
  • 11
  • 17
  • In my case I had `android:focusable="true"` inside the *row_item.xml* and removing it fixed it. Basically, I removed this `focusable` attribute from all xml that was related to the recyclerview *(parent xml, the tag itself, and the child row xml)* and it resolved the issue. – Gene Bo Jun 27 '19 at 20:54
14

If Using RecyclerView in NestedScrollView add this line to RecyclerView :

android:nestedScrollingEnabled="false"

I hope it help you.

Alireza K
  • 309
  • 3
  • 5
6

There is also a known bug, for those of you who land here check out this question which references this workaround.

mpkasp
  • 343
  • 3
  • 10
4

Add these to your parent element of R.layout.item_app_list

android:clickable="false"
android:focusable="false"
kunwar97
  • 775
  • 6
  • 14
2

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.

Johny
  • 501
  • 5
  • 16
1

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());
        }
    });

    }

}
Cgx
  • 753
  • 3
  • 6
1

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); 
    } 
});
Bhavnik
  • 2,020
  • 14
  • 21
1

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

Jitendra Sawant
  • 648
  • 7
  • 14
0

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);
            }
        });

    }
J.R
  • 2,113
  • 19
  • 21
  • careful, if you put the clickListener inside bindViewHolder your clicks will reference wrong positions when views recycle... – marcos E. Dec 26 '18 at 12:27
0

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

Lheonair
  • 468
  • 5
  • 14
0

If Using RecyclerView in NestedScrollView add this line to RecyclerView :

android:nestedScrollingEnabled="false"

it will help you .