Je trouve difficile d'implémenter une fonctionnalité probablement simple. J'ai un FrameLayout contenant un menu déroulant (comme celui de WhatsApp, apparaissant lorsque vous cliquez sur l'icône Pièce jointe). Je veux le fermer quand je clique à l'extérieur de ce FrameLayout. J'essaye de l'implémenter en utilisant un événement d'onTouch, sans la chance.onTouchListener sur FrameLayout: fermez-le lorsque vous cliquez dessus
Mon code:
public class MainActivity extends AppCompatActivity {
private LinearLayout mRevealView;
private FrameLayout mRevealFrame;
...
@Override
protected void onCreate(Bundle savedInstanceState) {
...
mRevealView = (LinearLayout) findViewById(R.id.rows);
mRevealFrame = (FrameLayout) findViewById(R.id.reveal_frame);
...
}
// This method is called when I click on the right menu icon.
public void fuelTypes() {
// Looking for X and Y coordinates.
int cx = (mRevealView.getLeft() + mRevealView.getRight());
int cy = (mRevealView.getTop());
// Looking for radius when icon is tapped for showing layout.
int startRadius = 0;
int endRadius = Math.max(mRevealView.getWidth(), mRevealView.getHeight());
// Performing circular reveal when icon will be tapped.
Animator animator = ViewAnimationUtils.createCircularReveal(mRevealView, cx, cy, startRadius, endRadius);
animator.setInterpolator(new AccelerateDecelerateInterpolator());
animator.setDuration(400);
// Reverse animation to find radius when icon is tapped again for hiding layout.
// The starting radius will be the radius or the extent to which circular reveal animation is to be shown.
int reverseStartRadius = Math.max(mRevealView.getWidth(), mRevealView.getHeight());
// End - radius will be zero.
int reverseEndRadius = 0;
// Performing circular reveal for reverse animation.
Animator animate = ViewAnimationUtils.createCircularReveal(mRevealView, cx, cy, reverseStartRadius, reverseEndRadius);
if (hidden) {
// To show the layout when the icon is tapped.
mRevealView.setVisibility(View.VISIBLE);
mRevealFrame.setVisibility(View.VISIBLE);
animator.start();
hidden = false;
} else {
mRevealView.setVisibility(View.VISIBLE);
mRevealFrame.setVisibility(View.VISIBLE);
// To hide layout on animation end.
animate.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
mRevealView.setVisibility(View.INVISIBLE);
mRevealFrame.setVisibility(View.INVISIBLE);
hidden = true;
}
});
animate.start();
}
mRevealFrame.setOnTouchListener(new FrameLayout.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
Log.d("MOTION_EVENT", "getAction(): " + motionEvent.getAction());
switch (motionEvent.getAction()) {
case MotionEvent.ACTION_UP:
Log.d("REVEAL_FRAME", "motion event: " + "ACTION_UP");
fuelTypes();
return true;
}
return true;
}
});
}
Le premier Log.d()
travaille, montrant que l'auditeur est déclenchée. Pourtant, la méthode ne va pas à l'intérieur du switch
: j'ai déjà essayé beaucoup de drapeaux, sans chance. J'ai lu environ onInterceptTouchEvent
et onTouchEvent
, mais je ne sais pas comment les utiliser à la place du OnTouchListener
.
Nous vous remercions de votre aide.
essayer de vous déboguer programme et voir la différence entre MotionEvent.ACTION_CANCEL et valeur à l'intérieur motionEvent.getAction() –
ACTION_CANCEL a un id = 3, alors que je ne peux voir un identifiant pour la FrameLayout en cliquant à l'intérieur de lui (habituellement les ids sont égaux à 0 et 1). – Davide3i