J'essaie de déplacer un objet entre max_height
et min_height
valeur, j'ai trouvé un morceau de code et j'ai essayé de l'adapter, mais l'objet (CardView
) se déplace de toute sa hauteur de l'écran, et quand j'essaie de déplacer l'objet réapparaît dans un autre endroit avant de bouger, je ne sais pas comment l'adapter à mes besoins, à des idées?Déplacer l'objet vers le haut/bas avec le geste de balayage
public interface OnLayoutCloseListener {
void OnLayoutClosed();
}
enum Direction {
UP_DOWN,
LEFT_RIGHT,
NONE
}
private Direction direction = Direction.NONE;
private int previousFingerPositionY;
private int previousFingerPositionX;
private int baseLayoutPosition;
private boolean isScrollingUp;
private boolean isLocked = false;
private OnLayoutCloseListener listener;
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
if (isLocked) {
return false;
} else {
final int y = (int) ev.getRawY();
final int x = (int) ev.getRawX();
if (ev.getActionMasked() == MotionEvent.ACTION_DOWN) {
previousFingerPositionX = x;
previousFingerPositionY = y;
} else if (ev.getActionMasked() == MotionEvent.ACTION_MOVE) {
int diffY = y - previousFingerPositionY;
int diffX = x - previousFingerPositionX;
if (Math.abs(diffX) + 50 < Math.abs(diffY)) {
return true;
}
}
return false;
}
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
if (!isLocked) {
final int y = (int) ev.getRawY();
final int x = (int) ev.getRawX();
if (ev.getActionMasked() == MotionEvent.ACTION_DOWN) {
previousFingerPositionX = x;
previousFingerPositionY = y;
baseLayoutPosition = (int) this.getY();
} else if (ev.getActionMasked() == MotionEvent.ACTION_MOVE) {
int diffY = y - previousFingerPositionY;
int diffX = x - previousFingerPositionX;
if (direction == Direction.NONE) {
if (Math.abs(diffX) > Math.abs(diffY)) {
direction = Direction.LEFT_RIGHT;
} else if (Math.abs(diffX) < Math.abs(diffY)) {
direction = Direction.UP_DOWN;
} else {
direction = Direction.NONE;
}
}
if (direction == Direction.UP_DOWN) {
isScrollingUp = diffY <= 0;
this.setY(baseLayoutPosition + diffY);
requestLayout();
return true;
}
} else if (ev.getActionMasked() == MotionEvent.ACTION_UP) {
if (direction == Direction.UP_DOWN) {
if (isScrollingUp) {
//Calculates height according to my needs
int max_height = height - (card.getHeight() + toolbar.getHeight());
if (Math.abs(this.getY()) > max_height) {
if (listener != null) {
listener.OnLayoutClosed();
}
}
} else {
//Calculates height according to my needs
int min_height = height - ((int)(toolbar.getHeight() * 1.7));
if (Math.abs(this.getY()) > min_height) {
if (listener != null) {
listener.OnLayoutClosed();
}
}
}
ObjectAnimator positionAnimator = ObjectAnimator.ofFloat(card, "y", this.getY(), 0);
positionAnimator.setDuration(0);
positionAnimator.start();
direction = Direction.NONE;
return true;
}
direction = Direction.NONE;
}
return true;
}
return false;
}
public void setOnLayoutCloseListener(OnLayoutCloseListener closeListener) {
this.listener = closeListener;
}
public void lock() {
isLocked = true;
}
public void unLock() {
isLocked = false;
}
SOLUTION MISE À JOUR:
Réinitialiser LayoutParam
à une instance de la carte:
card.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
que d'utiliser ce code pour une vue de défilement entre min_height
et max_height
private int previousFingerPositionY;
private int previousFingerPositionX;
int min_height = 500;
int max_height = 100;
int pressedy;
int viewMariginY;
private boolean isLocked = false;
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
if (isLocked) {
return false;
} else {
final int y = (int) ev.getRawY();
final int x = (int) ev.getRawX();
if (ev.getActionMasked() == MotionEvent.ACTION_DOWN) {
previousFingerPositionX = x;
previousFingerPositionY = y;
} else if (ev.getActionMasked() == MotionEvent.ACTION_MOVE) {
int diffY = y - previousFingerPositionY;
int diffX = x - previousFingerPositionX;
if (Math.abs(diffX) + 25 < Math.abs(diffY)) {
return true;
}
}
return false;
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
int currenty=(int) event.getRawY();
FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) card.getLayoutParams();
switch(event.getAction())
{
case MotionEvent.ACTION_DOWN :
pressedy=currenty;
viewMariginY=layoutParams.topMargin;
break;
case MotionEvent.ACTION_MOVE :
int diffy=currenty-pressedy;
int marginy=viewMariginY+diffy;
layoutParams.topMargin=marginy;
if(marginy >= max_height && marginy <= min_height)
{
ObjectAnimator positionAnimator = ObjectAnimator.ofFloat(card, "y", this.getY(), marginy);
positionAnimator.setDuration(0);
positionAnimator.start();
}
break;
case MotionEvent.ACTION_UP :
int diffy2=currenty-pressedy;
int marginy2=viewMariginY+diffy2;
layoutParams.topMargin=marginy2;
if(marginy2 >= max_height && marginy2 <= min_height)
{
ObjectAnimator positionAnimator1 = ObjectAnimator.ofFloat(card, "y", this.getY(), marginy2);
positionAnimator1.setDuration(0);
positionAnimator1.start();
}
break;
}
return true;
}
Ce param ... –
Il va modifier les marges de votre carte Voir si vous ne voulez pas juste remplacer le code param avec votre code de traduction puisque vous avez la différence relative maintenant .. –
J'ai mis à jour la réponse –