-1

It seems like a frequently asked question but it is not. So I want to execute a method on clicking recyclerview. I have nothing to do with recyclerview items. I want the app to recognize a screen tap to implement a method.

Here's the code:

xml

  <RelativeLayout
      android:layout_width="match_parent"
      android:layout_height="0dp"
      android:layout_weight="9"
     android:id="@+id/rl">
  <android.support.v7.widget.RecyclerView
      android:id="@+id/recyclerView"
      android:layout_width="match_parent"
        android:layout_height="match_parent"
      android:clipToPadding="false"
      android:divider="@null"
      android:paddingTop="8dp"
      android:visibility="visible"
      />
  </RelativeLayout>

Java

 mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
    mRecyclerView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Toast.makeText(MainActivity.this,"sent",Toast.LENGTH_SHORT).show();
        }
    });

Now on tapping on recyclerview area, the toast doesn't show up.

halfer
  • 19,824
  • 17
  • 99
  • 186
Ticker
  • 15
  • 1
  • 4
  • 1
    Possible duplicate of [RecyclerView onClick](https://stackoverflow.com/questions/24471109/recyclerview-onclick) – Satan Pandeya Feb 09 '19 at 13:40
  • No it is not, I dont want to click recyclerview items. I have a method in main class and I want it to execute on click recyclerview area – Ticker Feb 09 '19 at 13:50
  • Create an interface in the RecyclerViewAdapter and initialise it in the constructor of the recyclerView. Create an abstract method in the interface and pass View, and int position as parameters to it. Now in the ViewHolder, implement the OnClickListener and pass the view and position of the adapter to it. You can then create an object of the interface in the MainActivity and use it from there. See [this](https://qr.ae/TUrdQW) for help –  Feb 09 '19 at 14:03

2 Answers2

0

I would imagine you would do the onItemClick but do the same thing to all of the items. So if you click on the area of the recyclerview, then you will definitely click on one of the items. So just call a method for any onItem

sixcookies
  • 357
  • 1
  • 7
  • 22
0

You can use something like this:

          mRecyclerView.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() {
            @Override
            public boolean onInterceptTouchEvent(@NonNull RecyclerView rv, @NonNull MotionEvent e) {
                Toast.makeText(MainActivity.this,"sent",Toast.LENGTH_SHORT).show();
                return false;
            }
            @Override
            public void onTouchEvent(@NonNull RecyclerView rv, @NonNull MotionEvent e) {
            }
            @Override
            public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
            }
        });
Felip Adell
  • 101
  • 1
  • 3