Created
June 11, 2019 08:07
-
-
Save rezaiyan/2d54879c81a1daf4d61186bd46bd5810 to your computer and use it in GitHub Desktop.
This is a sample to swipe a layout to left and right
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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