0

Referring to these tutorials and examples, I tried to implement the swipe action. For that, I wrote below codes in onCreate method of MainActivity.

ActivitySwipeDetector swipe = new ActivitySwipeDetector(this, null);
RelativeLayout swipe_layout = (RelativeLayout) findViewById(R.id.mainll);
swipe_layout.setOnTouchListener(swipe);

Now I have few questions -

  1. is passing null as SwipeInterface activity is okay?
  2. What is the use of SwipeInterface activity argument in ActivitySwipeDetector class? Is it okay if I just implement all functions inside ActivitySwipeDetector class, deleting SwipeInterface class?

Thanks for help as usual.

Community
  • 1
  • 1
abdfahim
  • 2,523
  • 10
  • 35
  • 71

2 Answers2

1

Okay never mind, I think I got it ..

public class MainActivity extends Activity implements SwipeInterface {
    public static int CURR_BOOK_ID = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ActivitySwipeDetector swipe = new ActivitySwipeDetector(this, (SwipeInterface) this);
        RelativeLayout swipe_layout = (RelativeLayout) findViewById(R.id.mainll);
        swipe_layout.setOnTouchListener(swipe);
    }
......................
....................
}
abdfahim
  • 2,523
  • 10
  • 35
  • 71
1

You'll get a NullPointerException inside onRightToLeftSwipe and onLeftToRightSwipe because of the lines activity.onRightToLeft(v); and activity.onLeftToRight(v);. You can either implement everything in ActivitySwipeDetector class and get rid of those lines (but it's not a good solution) or implement on your own activity, mark this activity as implements SwipeInterface and pass this as second argument to ActivitySwipeDetector.