Make On click listener to on Swipe

283 Views Asked by At

I am a beginner in the android studio. I have created a button which opens a new activity which shows the full details of a specific recycler view item. How I can make this on left swipe. I need on left swipe a new activity should be open and the extra values should be passed to it. Please help. My On click listener code

public void onItemClick(int position) {
        Intent detailintent = new Intent(this, DetailActivity.class);
        ExampleItem clickeditem = mExampleList.get(position);
        detailintent.putExtra(EXTRA_URL, clickeditem.getmDate());
startActivity(detailintent);
    }
2

There are 2 best solutions below

0
On

You could override onTouchEvent like this:

@Override
public boolean onTouchEvent(MotionEvent event)
{     
    switch(event.getAction())
    {
      case MotionEvent.ACTION_DOWN:
          x1 = event.getX();                          
          break;          
      case MotionEvent.ACTION_UP:
          x2 = event.getX();
          float deltaX = x2 - x1;
          if (Math.abs(deltaX) > DISTANCE)
          {
              //do something
          }                                
          break;    
    }            
    return super.onTouchEvent(event);        
}

x1,x2,and deltaX are fields of activity class, such as:

private float x1,x2;
private final static int DISTANCE = 200;
0
On

You should use OnTouchListener for each item and check the swipe movement direction with 'MotionEvent` object for detecting right left swipe, check this link