Aller au contenu

probleme gesture


vincetreize

Recommended Posts

Bonjour a tous alors voila j'ai un gros souci,

j'ai codé un début de programme, avec une mise en place des gesture,

a la compilation aucune érreur,

seulement aucune action lors d'un onFling ni même longpress voici mon code :

package com.m.h;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;
import android.widget.ViewFlipper;

public class mh extends Activity {

static String[] items ={"Ajoupa Bouillon", "Anses d'Arlet", "Basse Pointe", "	Bellefontaine", "Carbet", "Case Pilote", "Diamant", "Ducos", "Fond Saint Denis", "Fort De France", "François", "Grand Rivière", "Gros Morne", "Lamentin", "Lorrain", "Macouba", "Marigot", "Marin", "Morne Rouge", "Morne Vert", "Prêcheur", "Rivière Pilote", "Rivière Salée", "Robert", "Sainte Anne", "Sainte Luce", "Sainte Marie", "Saint Esprit", "Saint Joseph", "Saint Pierre"};
private static final int SWIPE_MIN_DISTANCE = 120;
private static final int SWIPE_MAX_OFF_PATH = 250;
private static final int SWIPE_THRESHOLD_VELOCITY = 200;
private GestureDetector gestureDetector;
View.OnTouchListener gestureListener;
private int currentIndex = 0;
private int maxIndex = 30;

ViewFlipper flipper;

Animation slideLeftIn;
Animation slideLeftOut;
Animation slideRightIn;
Animation slideRightOut;	


   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.main);

       flipper=(ViewFlipper)findViewById(R.id.details);

       slideLeftIn = AnimationUtils.loadAnimation(this, R.anim.slide_left_in);
       slideLeftOut = AnimationUtils.loadAnimation(this, R.anim.slide_left_out);
       slideRightIn = AnimationUtils.loadAnimation(this, R.anim.slide_right_in);
       slideRightOut = AnimationUtils.loadAnimation(this, R.anim.slide_right_out);

       flipper.setInAnimation(AnimationUtils.loadAnimation(this,android.R.anim.fade_in));
       flipper.setOutAnimation(AnimationUtils.loadAnimation(this,android.R.anim.fade_out));

       if (items != null)
       {
       Button btn =new Button(this);
       Button btn2 = new Button(this);

       btn.setText("categorie");
       btn2.setText(items[currentIndex]);
       LinearLayout mylayout = new LinearLayout(this);
       mylayout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
       mylayout.addView(btn);
       mylayout.addView(btn2);

       flipper.addView(mylayout, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
       }



	gestureDetector = new GestureDetector(new MyGestureDetector());

   }
public boolean onTouch(View v, MotionEvent event) {
	if (gestureDetector.onTouchEvent(event)) {
	return gestureDetector.onTouchEvent(event);
	}
	return false;
}



class MyGestureDetector extends SimpleOnGestureListener {
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
     if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
				return false;
			// right to left swipe
			if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE
					&& Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
				flipper.setInAnimation(slideLeftIn);
				flipper.setOutAnimation(slideLeftOut);

				if (currentIndex == maxIndex) {
					currentIndex = 0;
				}
				else {
					currentIndex = currentIndex + 1;
				}

				Toast.makeText(mh.this, "Left Swipe", Toast.LENGTH_SHORT).show();

				flipper.showNext();
			}
			else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
					&& Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
				flipper.setInAnimation(slideRightIn);
				flipper.setOutAnimation(slideRightOut);

				if (currentIndex == maxIndex) {
					currentIndex = 0;
				}
				else {
					currentIndex = currentIndex - 1;
				}
				Toast.makeText(mh.this, "Right Swipe", Toast.LENGTH_SHORT).show();
				flipper.showPrevious();
			}
		return false;
	}

@Override
public void onLongPress(MotionEvent e)
{
Toast mToast = Toast.makeText(getApplicationContext(), "Long Press", Toast.LENGTH_SHORT);
mToast.show();
}

}




       }

quelqu'un aurait il une idée ?

Lien vers le commentaire
Partager sur d’autres sites

Archivé

Ce sujet est désormais archivé et ne peut plus recevoir de nouvelles réponses.

×
×
  • Créer...