I am creating a Relative Layout and want to add click as well as swipe(touch and move pointer over the layout to move it in real time) and detect left and right swipe. I have tried the following code so far.
public bool OnTouch(View v, MotionEvent e)
{
if (gestureDetector.OnTouchEvent(e))
{
//This is a Click
return true;
}
else
{
int initialTouchX = 0, initialTouchY = 0;
int newx = 0;
var x = v.Left;
switch (e.Action)
{
case MotionEventActions.Down:
{
_viewX = e.GetX();
_viewY = e.GetY();
initialTouchX = (int)e.RawX;
initialTouchY = (int)e.RawY;
break;
}
case MotionEventActions.Move:
{
var left = (int)(e.RawX - _viewX);
newx = left;
var right = (int)(left + v.Width);
var top = (int)(e.RawY - _viewY);
var bottom = (int)(top + v.Height);
v.Layout(left, top, right, bottom);
break;
}
case MotionEventActions.Up:
{
int lastX = (int)e.GetX();
int lastY = (int)e.GetY();
if ((x - newx) > 40)
{
//Detect Right Swipe
}
else if ((newx - x > 40))
{
//Detect Left Swipe
}
else
{
//Skip others
}
break;
}
}
}
return true;
}
My code for gestureDetector.OnTouchEvent(e)
gestureDetector = new GestureDetector(this, new SingleTapUp());
class SingleTapUp : Android.Views.GestureDetector.SimpleOnGestureListener
{
public override bool OnSingleTapUp(MotionEvent e) {
// Toast.MakeText(this,, ToastLength.Long).Show();
return true;
}
}
My code is working fine on some devices as well as emulator. But not working sometimes and onclick the layout moves automatically(The layout out centers itself at touch pointer). I think something is wrong and causing the issue. You can suggest me the best/standard way to do it. Any help will be greatly appreciated.