2012-03-27 8 views
0

スワイプで変更するページのイメージビューコンポーネントを取得しようとしています。私はスワイプジェスチャーを受け取り、それをジェスチャー検出器に送ることさえできません。OnFlingとImageViewが正しく動作しない

OnFlingメソッドでは、これらのログステートメントにアクセスできません。私が紛失しているものがありますか?ここで

public class Detail extends Activity implements OnClickListener{ 

private GestureDetector gestureDetector; 
private ImageView wallpaper; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
      gestureDetector = new GestureDetector(new MyGestureDetector()); 
      wallpaper = (ImageView)findViewById(R.id.wallpaper); 
    wallpaper.setAdjustViewBounds(true); 

      wallpaper.setOnTouchListener(new OnTouchListener() { 

     @Override 
     public boolean onTouch(View v, MotionEvent event) { 
      Log.d("getting", "here"); 
          //return gestureDetector.onTouchEvent(event); 
      return true; 
        } 
      }); 
} 

class MyGestureDetector extends SimpleOnGestureListener { 
    private static final int SWIPE_MIN_DISTANCE = 50; 
    private static final int SWIPE_MAX_OFF_PATH = 250; 
    private static final int SWIPE_THRESHOLD_VELOCITY = 200; 

    @Override 
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { 
     try { 
      Log.d("MOTION", "STARTED"); 
      if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) { 

       Log.d("Moving", "Right"); 
       return true; 

      } 
      else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) { 
       Log.d("Moving", "Left"); 
       return true; 

      } 
     } catch (Exception e) { 

     } 
     return false; 
    } 
} 

答えて

0

良い例です: 私はGenesMotionDetector.javaというクラスを持っています。

package gene.com.motioneventssample; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.GestureDetector; 
import android.view.MotionEvent; 
import android.widget.ImageView; 
import android.widget.LinearLayout; 
import android.widget.TextView; 

//This works 
public class GenesMotionDetector extends Activity implements GestureDetector.OnGestureListener { 
    private GestureDetector gestureScanner; 
    LinearLayout mView1; 
    TextView mView2; 
    ImageView mView3; 


    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.nothing); 
     gestureScanner= new GestureDetector(getBaseContext(),this); 
    } 

    @Override 
    public boolean onTouchEvent(MotionEvent me) { 
     System.out.println("Inside onTouchEvent() of GenesMotionDetector.java"); 
     return gestureScanner.onTouchEvent(me); 
    } 

    @Override 
    public boolean onDown(MotionEvent e) { 
     System.out.println("Inside onDown() of GenesMotionDetector.java"); 
     return true; 
    } 

    @Override 
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { 
     System.out.println("Inside onFling() of GenesMotionDetector.java"); 
     return true; 
    } 

    @Override 
    public void onLongPress(MotionEvent e) { 
     System.out.println("Inside onLongPress() of GenesMotionDetector.java"); 
    } 

    @Override 
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { 
     System.out.println("Inside onScroll() of GenesMotionDetector.java"); 
     return true; 
    } 

    @Override 
    public void onShowPress(MotionEvent e) { 
     System.out.println("Inside onShowPress() of GenesMotionDetector.java"); 
    } 

    @Override 
    public boolean onSingleTapUp(MotionEvent e) { 
     System.out.println("Inside onSingleTapUp() of GenesMotionDetector.java"); 
     return true; 
    } 
} 

このクラスの対応するXMLレイアウトファイルはnothing.xmlです。ここではそのためのコードは次のとおりです。

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/screen" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <TextView 
     android:id="@+id/text" 
     android:background="#17528c" 
     android:text="testing" 
     android:layout_width="100dp" 
     android:layout_height="200dp" /> 

    <ImageView 
     android:id="@+id/image" 
     android:background="#f8af20" 
     android:layout_width="100dp" 
     android:layout_height="200dp" /> 
</LinearLayout> 
0

あなたは

mDetector.setIsLongpressEnabled(false); 

Longpressはデフォルトで有効(true)とどのようなことがないことだけlongpressイベントをリッスンし、破棄スクロール、投げつけおよびその他のですが設定する必要があります複雑な出来事。

ちょうどあなたの設定あなたのジェスチャーの検出オブジェクトの後にそのコードを置く:

mDetector = new GestureDetectorCompat(context, this); 
mDetector.setOnDoubleTapListener(this); 
mDetector.setIsLongpressEnabled(false); 

平和