2
votes

My RecyclerView:

   <android.support.v7.widget.RecyclerView
                android:background="?android:selectableItemBackground"
                app:layoutManager="android.support.v7.widget.LinearLayoutManager"
                android:id="@+id/crash_course_item_dates"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="16dp"
                android:clickable="true"
               />

ClickListener:

  holder.crashCoursesDateRecyclerView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                crashCourseListener.onCrashCourseClick(crashCourse);
            }
        });

However it's not working when I click on the recyclerview. I thought it might be that the clicklistener is being triggered by the items inside the recyclerview. So what I tried is setting clickable=false in the recyclerview Items' layout and settings the layout's onClickListener(null) but it't not working.

update

Thanks for all your answers, I should have explained better, i have a recyclerview inside another recyclerview item, each item of the latter has a recyclerview inside. I fixed this problem by putting the recyclerview in a frame layout and adding another layout 'on top' of the recyclervjew and setting the onClickListener on that layout.

4
Simply call OnClickListener in your adapter class in which you have created your custom holder classPiyush

4 Answers

1
votes

As Recycler view doesnot support on itemClick Listener ,Implement a simplelistener interface and call that interface in the bindview and pass it to the main class.Please find the piece of code below for

public interface OnItemClickListener {
    public void onItemClick(View view, int position);
}

Adapter Constructor

    public ContentAdapter(List<ContentItem> items, OnItemClickListener listener) {
    this.items = items;
    this.listener = listener;
}

in Bind View Holder

holder.setOnClickListener(new View.OnClickListener() {
    @Override public void onClick(View v) {
        listener.onItemClick(item);
    }
});

Implement your activity with the OnItemClickListener and in the callbacks add your required code.

0
votes

You just simple call in your onBindViewHolder() method at where you have may be your own holder class.

So use holder.itemView.setOnClickListener(new setOn....){

At where itemView is the view which is inflated in onCreateViewHolder method.

0
votes

As per my understanding you want click of complete Recycler view, Try this,

  RecyclerView rv = (RecyclerView) findViewById(R.id.recycler_view);
    rv.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
          //your action  
        }
    });
-4
votes
    first of all your question is not clear to me but still what i have understood u want you perform click listener on the each item of the try this

in your recyclerview's item layout

    <LinearLayout
            android:id="@+id/linearlayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
           ><TextView
            android:id="@+id/linearlayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="Demo"
            android:clickable="false"
           />

        </LinearLayout>

Now in item ItemHolder class

public class Demo extends ItemHolder {
LinearLayout ll;

public Demo(View view) {

ll=(LinearLayout)view.findViewById(R.id.linearlayout);
ll.setOnClickListener(View View){

}