2013-07-24 4 views
8

제 앱에서 제스처를 구현하려고하는데 어떤 이유로 onfling()이 호출되지 않습니다. 나는 이것에 관한 수많은 게시물을 읽으려고 시도하고 내 코드를 수정하려고했지만 작동하지 않습니다. 아래는 내 코드입니다.어떤 이유로 든 onfling()이 호출되지 않습니다.

public class MainMenuSlider extends Activity implements OnClickListener{ 

protected MyGestureListener myGestureListener; 

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 ViewFlipper vf; 
private Animation animFlipInNext,animFlipOutNext, animFlipInPrevious, animFlipOutPrevious; 

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.mainmenuslider); 
    myGestureListener = new MyGestureListener(this); 

    //vf for viewflipper 
    vf=(ViewFlipper)findViewById(R.id.ViewFlipper01); 
    animFlipInNext = AnimationUtils.loadAnimation(this, R.anim.flipinnext); 
    animFlipOutNext = AnimationUtils.loadAnimation(this, R.anim.flipoutnext); 
    animFlipInPrevious = AnimationUtils.loadAnimation(this, R.anim.flipinprevious); 
    animFlipOutPrevious = AnimationUtils.loadAnimation(this, R.anim.flipoutprevious); 

    imageone = (ImageView) findViewById(R.id.imageone); 
    imagetwo = (ImageView) findViewById(R.id.imagetwo); 
    imageone.setOnClickListener(myGestureListener); 
    imagetwo.setOnClickListener(myGestureListener); 


} 


class MyGestureListener extends SimpleOnGestureListener implements OnTouchListener 
    { 
     Context context; 
     GestureDetector gDetector; 

    public MyGestureListener() 
    { 
     super(); 
    } 

    public GestureDetector getDetector() 
    { 
     return gDetector; 
    } 

    public MyGestureListener(Context context) 
    { 
     this(context, null); 
    } 

    public MyGestureListener(Context context, GestureDetector gDetector) 
    { 

     if(gDetector == null) 
      gDetector = new GestureDetector(context, this); 

     this.context = context; 
     this.gDetector = gDetector; 
    } 

    public boolean onDown(MotionEvent event) 
    { 
     return true; 
    } 


    @Override 
    public boolean onFling(MotionEvent e1,MotionEvent e2,float velocityX,float velocityY) 
    { 
     try { 
      if(e1.getX() > e2.getX() && Math.abs(e1.getX() - e2.getX()) > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) { 
       vf.setInAnimation(animFlipInPrevious); 
       vf.setOutAnimation(animFlipOutPrevious); 
       vf.showPrevious(); 
      }else if (e1.getX() < e2.getX() && e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) { 
       vf.setInAnimation(animFlipInNext); 
       vf.setOutAnimation(animFlipOutNext); 
       vf.showNext(); 
      } 
     } catch (Exception e) { 
      // nothing 
     } 
     return true; 

    } 

    @Override 
    public boolean onSingleTapConfirmed(MotionEvent e) 
    { 

     return super.onSingleTapConfirmed(e); 
    } 

    public boolean onTouch(View v, MotionEvent event) 
    { 

     // Within the MyGestureListener class you can now manage the event.getAction() codes. 

     // Note that we are now calling the gesture Detectors onTouchEvent. And given we've set this class as the GestureDetectors listener 
     // the onFling, onSingleTap etc methods will be executed. 
     return gDetector.onTouchEvent(event); 
    } 


    public void onClick(View v) 
    { 
     if (v == imageone) 
     { 
      Intent j = new Intent(getApplicationContext(), MainActivity.class); 
      j.putExtra("slideindex", 0); 
      startActivity(j); 
     } 

     if (v == imagetwo) 
     { 
      Intent j = new Intent(getApplicationContext(), MainActivity.class); 
      j.putExtra("slideindex", 1); 
      startActivity(j); 
     } 
    } 

    public boolean onScroll(MotionEvent e1,MotionEvent e2,float distanceX,float distanceY) 
    { 
     return true; 
    } 


    } 
} 

편집 : 여기

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:orientation="vertical" 
       android:background="#291E3D" > 

      <ScrollView android:layout_width="fill_parent" 
          android:layout_height="fill_parent" 
          android:orientation="vertical" > 

        <LinearLayout android:layout_width="fill_parent" 
            android:layout_height="fill_parent" 
            android:gravity="center" 
            android:orientation="vertical" > 

          <ViewFlipper android:id="@+id/ViewFlipper01" 
               android:layout_height="wrap_content" 
               android:layout_width="fill_parent" 
               android:gravity="center" > 

            <!-- adding views to ViewFlipper --> 

              <!-- First Slide --> 
              <LinearLayout android:layout_width="fill_parent" 
                  android:layout_height="fill_parent" 
                  android:gravity="center" 
                  android:orientation="vertical" > 


                <TextView android:id="@+id/title1" 
                   android:text="title1" 
                   android:textColor="#ffffff" 
                   android:layout_width="fill_parent" 
                   android:layout_height="wrap_content" 
                   android:gravity="center" 
                   android:layout_marginBottom="3dip" 
                   android:textSize="25dip"/> 


                <ImageView android:id="@+id/imageone" 
                   android:clickable="true" 
                   android:layout_width="wrap_content" 
                   android:layout_height="wrap_content" 
                   android:src="@drawable/splash_screen"> 
                </ImageView> 


              </LinearLayout> 

              <!-- Second Slide --> 
              <LinearLayout android:layout_width="fill_parent" 
                  android:layout_height="fill_parent" 
                  android:gravity="center" 
                  android:orientation="vertical" > 


                <TextView android:id="@+id/title2" 
                   android:text="title2" 
                   android:textColor="#ffffff" 
                   android:layout_width="fill_parent" 
                   android:layout_height="wrap_content" 
                   android:gravity="center" 
                   android:layout_marginBottom="3dip" 
                   android:textSize="25dip" /> 


                <ImageView android:id="@+id/imagetwo" 
                   android:clickable="true" 
                   android:layout_width="wrap_content" 
                   android:layout_height="wrap_content" 
                   android:src="@drawable/sunnahsplash"> 
                </ImageView> 


              </LinearLayout> 



            </ViewFlipper> 





          <!-- footer --> 
          <LinearLayout android:id="@+id/LinearLayout03" 
              android:layout_height="wrap_content" 
              android:layout_width="wrap_content" 
              android:orientation="horizontal" 
              android:layout_gravity="center" 
              android:layout_marginTop="10dip"> 

              <Button android:id="@+id/Button02" 
                android:layout_height="wrap_content" 
                android:text="&lt;Previous" 
                android:textSize="18dip" 
                android:layout_width="wrap_content" 
                android:layout_gravity="center" 
                android:layout_marginRight="15dip" 
                android:padding="5dp" 
                android:background="@drawable/background_button_slider"></Button> 
              <Button android:id="@+id/Select" 
                android:text=" Select " 
                android:textSize="18dip" 
                android:layout_height="match_parent" 
                android:layout_width="wrap_content" 
                android:layout_gravity="center" 
                android:padding="5dp" 
                android:background="@drawable/background_button_slider"></Button> 
              <Button android:id="@+id/Button01" 
                android:text=" Next&gt;" 
                android:textSize="18dip" 
                android:layout_height="match_parent" 
                android:layout_width="wrap_content" 
                android:layout_gravity="center" 
                android:layout_marginLeft="15dip" 
                android:padding="5dp" 
                android:background="@drawable/background_button_slider"></Button> 
          </LinearLayout> 

        </LinearLayout> 

      </ScrollView> 


</LinearLayout> 
+0

레이아웃 xml 파일을 게시 할 수 있습니까? –

+0

왜 imageone, imagetwo 등의 제스처 수신기를 seryp합니까? – pskink

+0

@AbhishekAgarwal : 메인 포스트를 편집하고 XML을 추가했습니다. 그것을 확인하십시오. 고맙습니다. – Azhar92

답변

6

날뛰는 때문에 스크롤 뷰의 작동하지 않는 날에 의하면 요청에 따라 mainmenuslider XML 파일 내용이 .... 보려면 다음을 보면 이 코드를 MainMenuSlider 클래스에 추가하십시오.

GestureDetector gestureDetector 
    = new GestureDetector(myGestureListener); 

    @Override 
    public boolean dispatchTouchEvent(MotionEvent e) 
    { 
     super.dispatchTouchEvent(e); 
     return gestureDetector.onTouchEvent(e); 
    } 

내가 클릭보다는 터치 이벤트가 다른 캡처하려면 ..이 코드가 제대로 작동

 public class MainMenuSlider extends Activity implements OnClickListener{ 



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 ViewFlipper vf; 
private Animation animFlipInNext,animFlipOutNext, animFlipInPrevious, a nimFlipOutPrevious; 

private ImageView imageone; 

private ImageView imagetwo; 

public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 

setContentView(R.layout.activity_main_menu_slider); 
//myGestureListener = new MyGestureListener(this); 

//vf for viewflipper 
vf=(ViewFlipper)findViewById(R.id.ViewFlipper01); 
animFlipInNext = AnimationUtils.loadAnimation(this, R.anim.flipinnext); 
animFlipOutNext = AnimationUtils.loadAnimation(this, R.anim.flipoutnext); 
animFlipInPrevious = AnimationUtils.loadAnimation(this, R.anim.flipinprevious); 
animFlipOutPrevious = AnimationUtils.loadAnimation(this, R.anim.flipoutprevious); 

imageone = (ImageView) findViewById(R.id.imageone); 
imagetwo = (ImageView) findViewById(R.id.imagetwo); 
imageone.setOnClickListener(myGestureListener); 
imagetwo.setOnClickListener(myGestureListener); 


} 

@Override 
public boolean onTouchEvent(MotionEvent event) { 
// TODO Auto-generated method stub 
return gestureDetector.onTouchEvent(event); 
    } 

SimpleOnGestureListener simpleOnGestureListener 
= new SimpleOnGestureListener(){ 

@Override 
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, 
     float velocityY) { 

    float sensitvity = 50; 
    if((e1.getX() - e2.getX()) > sensitvity){ 
     vf.showPrevious(); 
    }else if((e2.getX() - e1.getX()) > sensitvity){ 
     vf.showNext(); 
    } 

    return true; 
} 

}; 

GestureDetector gestureDetector 
= new GestureDetector(simpleOnGestureListener); 
@Override 
public boolean dispatchTouchEvent(MotionEvent e) 
{ 
super.dispatchTouchEvent(e); 
return gestureDetector.onTouchEvent(e); 
} 
    public void onClick(View v) { 
// TODO Auto-generated method stub 
Toast.makeText(getApplicationContext(), "Hello",Toast.LENGTH_SHORT 
     ).show(); 
    } 

+0

나는 scrollview를 제거했다. 그래도 작동이 안되는. – Azhar92

+0

코드를 삽입하셨습니까? –

+0

gestureDetector에서 '해결할 수 없습니다'라는 오류 메시지가 나타납니다. 나는 여기 gestureDetector가 GestureDetector 타입의 객체 인 것처럼 보이지만 내 메인 클래스에는 아무 것도 없다고 생각한다. 내 "MyGestureListener"클래스에 GestureDetector가 있습니다 (코드를 확인하는 경우) – Azhar92

1

을 클래스의 코드를 편집 한 사용자 지정 제스처 검출기를 사용할 필요, 이 액티비티의 하위 클래스는 다음과 같습니다.

private class CustomGestureListener extends GestureDetector.SimpleOnGestureListener { 
    @Override  
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { 
     boolean usedThisFling = false; 

     // Is it horizontal? 
     if (Math.abs(velocityX) >= 2.0*Math.abs(velocityY)) { 
      if (velocityX < 0.0)  leftSwipeActions(); 
      else      rightSwipeActions(); 
      usedThisFling = true; 
     } 
     return usedThisFling; 
    } 

그런 다음 인스턴스를 생성해야합니다. d에서 사용하도록 설정하고 onCreate() 메소드 :

// Create it. 
final GestureDetector myGestureListener = new GestureDetector(getApplicationContext(), new CustomGestureListener()); 

// Set it up for use: 
imageone.setOnTouchListener(new OnTouchListener() { 
    public boolean onTouch(View v, MotionEvent event) { 
     return myGestureListener.onTouchEvent(event); 
    } 
}); 

// And put an onClick method it to force it to work (this shouldn't be necessary but 
// it seems like sometimes it is) 
imageone.setOnClickListener(new OnClickListener() { 
    @Override 
    public void onClick(View v) { 
    } 
}); 
+0

하지만 OnFling() 작업과의 관계는 무엇입니까? – Azhar92

+0

대답은 –

+0

을 표시하도록 업데이트되었습니다. 귀하의 솔루션을 시도하고 알려 드리겠습니다. – Azhar92

관련 문제