Skip to content

Instantly share code, notes, and snippets.

@rezaiyan
Created June 11, 2019 08:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rezaiyan/2d54879c81a1daf4d61186bd46bd5810 to your computer and use it in GitHub Desktop.
Save rezaiyan/2d54879c81a1daf4d61186bd46bd5810 to your computer and use it in GitHub Desktop.
This is a sample to swipe a layout to left and right
import android.view.MotionEvent;
import android.view.View;
/**
* @author ali (alirezaiyann@gmail.com)
* @since 6/11/19 12:24 PM.
*/
public class OnTouchListener implements View.OnTouchListener {
private View view = null;
@Override
public boolean onTouch(View v, MotionEvent event) {
this.view = v;
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: {
break;
}
case MotionEvent.ACTION_UP: {
if (view.getX() >= view.getWidth() / 1.5) {
setPosition((+view.getWidth()));
resetPosition();
} else if (-view.getX() >= view.getWidth() / 1.5) {
setPosition((-view.getWidth()));
resetPosition();
} else {
resetPosition();
}
break;
}
case MotionEvent.ACTION_MOVE: {
resetPosition();
break;
}
default: {
return false;
}
}
return true;
}
private void setPosition(int position) {
view.animate()
.x(position)
.setDuration(200)
.start();
}
private void resetPosition() {
view.animate().x(0F).setDuration(200).start();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment