-1

I have this code below which is implemeting a touch listener for recyclerView. But when touching the View on Touch is not called.

What am i doing wrong?

public class MainActivity extends AppCompatActivity {

private RecyclerView recyclerView;
private GestureDetectorCompat detector;

@Override
protected void onCreate(Bundle savedInstanceState)  {

super.onCreate(savedInstanceState);


 detector = new GestureDetectorCompat(this, new GestureDetector.SimpleOnGestureListener() {

    @Override
    public boolean onFling (MotionEvent e1, MotionEvent e2,float velocityX,
    float velocityY){


        return false;
    }

});

   recyclerView.setOnTouchListener(new OnTouchListener(){

       @Override
       public boolean onTouch(View v,MotionEvent event) {
        return detector.onTouchEvent(event);
      } 

   });

}

}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Nick
  • 2,818
  • 5
  • 42
  • 60
  • You should implement the onTouchEvent on Recyclerview items, not on the recyclerview itself. You cannot interact with it directly, but with its items. – deluxe1 Jul 25 '18 at 12:52
  • Assuming you don't need this to manage item clicks: The `RecyclerView` needs the `TouchListener` for itself to manage item touches and scrolls. I suggest you wrap it with another `View` and implement the `TouchListener` on this view rather than on the `RecyclerView`. Don't forget to return false so the recycler will get the touch event as well. – HedeH Jul 25 '18 at 12:52
  • @deluxe1 And why it offers the setOnTouchListener? – Nick Jul 25 '18 at 12:53
  • @HedShafran Can you give me an example? – Nick Jul 25 '18 at 12:55
  • @Nick - It's an inherited method from the View class. – deluxe1 Jul 25 '18 at 12:59
  • Posted an example.. Hope it helps :) – HedeH Jul 25 '18 at 13:46

1 Answers1

0

If you want to handle the click for the whole recycler view you have to call setOnClickListener(), but it will just produce the same result regardless of which item you click

If you want to produce a different result basing on the item that is clocked, you have to define your own Adapter

ci0ccarellia
  • 795
  • 9
  • 26