2013-09-21 1 views
0

인 텐트를 사용하여 이전 활동과 다음 활동 사이를 탐색해야하는 각 활동에 onFling()을 구현했습니다.OnFling 메소드가 앱에서 작동하지 않습니다.

하지만 앱을 실행해도 작동하지 않으며 로그 고양이에 나타나지 않습니다. 코드에서 메소드 또는 사소한 실수를해야하는 다른 방법이 있습니까?

public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, 
       float velocityY) { 
      // TODO Auto-generated method stub 
      Log.d(null,"Fling"); 
      int dx = (int) (e2.getX() - e1.getX()); 
      // don't accept the fling if it's too short 
      // as it may conflict with a button push 
      if (Math.abs(dx) > MAJOR_MOVE && Math.abs(velocityX) > Math.abs(velocityY)) { 
       if (velocityX > 0) { 
       //switch to activity on left fling 
       Intent intent = new Intent(this, MainActivity.class); 
       startActivity(intent); 

       } else { 
       //switch to activity on right fling 
       Intent intent = new Intent(this, StudentLife.class); 
       startActivity(intent);  


       } 
       return true; 

      } else { 

       return false; 
      } 
     } 

This is the whole class file to clarify some of the questions: 

    import android.app.Activity; 
import android.content.Context; 
import android.content.Intent; 
import android.content.res.TypedArray; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.GestureDetector.OnGestureListener; 
import android.view.MenuItem; 
import android.view.MotionEvent; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.AdapterView; 
import android.widget.AdapterView.OnItemClickListener; 
import android.widget.BaseAdapter; 
import android.widget.Gallery; 
import android.widget.ImageView; 
import android.widget.Toast; 

@SuppressWarnings("deprecation") 
public class GalleryStudent extends Activity implements OnGestureListener { 

    private static final int MAJOR_MOVE = 0; 


    public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_galery_student); 

      Gallery g = (Gallery) findViewById(R.id.gallery); 
      g.setAdapter(new ImageAdapter(this)); 

      g.setOnItemClickListener(new OnItemClickListener() { 
       public void onItemClick(@SuppressWarnings("rawtypes") AdapterView parent, View v, int position, long id) { 
        Toast.makeText(GalleryStudent.this, "" + position, Toast.LENGTH_SHORT).show(); 
       } 
      }); 
     } 


    public boolean onOptionsItemSelected(MenuItem item) 
     { 

      switch (item.getItemId()) 
      { 
       case android.R.id.home: 
        // This is called when the Home (Up) button is pressed 
        // in the Action Bar. 
        Intent parentActivityIntent = new Intent(this, 
        MainActivity.class); 
        parentActivityIntent.addFlags(
        Intent.FLAG_ACTIVITY_CLEAR_TOP | 
        Intent.FLAG_ACTIVITY_NEW_TASK); 
        startActivity(parentActivityIntent); 
        finish(); 
        return true; 

       default: 

        return super.onOptionsItemSelected(item); 



      } 



     } 

    public void onBackPressed() { 
     Intent startMain = new Intent(Intent.ACTION_MAIN); 
      startMain.addCategory(Intent.CATEGORY_HOME); 
      startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
      startActivity(startMain); 

    } 



    public class ImageAdapter extends BaseAdapter { 


     int mGalleryItemBackground; 
     private Context mContext; 

     private Integer[] mImageIds = { 
       R.drawable.gmitlogo, 
       R.drawable.michaelcarmody, 
       R.drawable.fb 
     }; 

     public ImageAdapter(Context c) { 
      mContext = c; 
      TypedArray a = obtainStyledAttributes(R.styleable.HelloGallery); 
      mGalleryItemBackground = a.getResourceId(
        R.styleable.HelloGallery_android_galleryItemBackground, 0); 
      a.recycle(); 
     } 

     public int getCount() { 
      return mImageIds.length; 
     } 

     public Object getItem(int position) { 
      return position; 
     } 

     public long getItemId(int position) { 
      return position; 
     } 

     public View getView(int position, View convertView, ViewGroup parent) { 
      ImageView i = new ImageView(mContext); 

      i.setImageResource(mImageIds[position]); 
      i.setLayoutParams(new Gallery.LayoutParams(150, 100)); 
      i.setScaleType(ImageView.ScaleType.FIT_XY); 
      i.setBackgroundResource(mGalleryItemBackground); 

      return i; 
     } 
    } 

    @Override 
    public boolean onDown(MotionEvent e) { 
     // TODO Auto-generated method stub 
     return false; 
    } 


    View.OnTouchListener gestureListener = new View.OnTouchListener() { 
     public boolean onTouch(View v, MotionEvent event) { 
       return detector.onTouchEvent(event); 
     } 
    }; 

    setOnTouchListener(gestureListener); 




    @Override 
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, 
      float velocityY) { 
     Log.d(null,"Fling"); 
     int dx = (int) (e2.getX() - e1.getX()); 
     // don't accept the fling if it's too short 
     // as it may conflict with a button push 
     if (Math.abs(dx) > MAJOR_MOVE && Math.abs(velocityX) > Math.abs(velocityY)) { 
      if (velocityX > 0) { 
      //switch to activity on left fling 
      Intent intent = new Intent(this, StudentLife.class); 
      startActivity(intent); 

      } else { 
      //switch to activity on right fling 
      Intent intent = new Intent(this, StudentPortal.class); 
      startActivity(intent);  


      } 
      return true; 

     } else { 

      return false; 
     } 
    } 
+0

이 방법으로 중단 점이 생깁니 까? –

+0

gestureDetector에 touchEvent를 보내시겠습니까? –

+0

사실은 아닙니다. 전체 수업을 볼 수 있도록 내 질문을 업데이트 할 것입니다. 터치 이벤트가 onFling()을 트리거한다는 인상하에있었습니다. –

답변

1

먼저 터치 수신기를 설정 한 다음 제스처 감지기에 연결하십시오.

View.OnTouchListener gestureListener = new View.OnTouchListener() { 
       public boolean onTouch(View v, MotionEvent event) { 
         return detector.onTouchEvent(event); 
       } 
      }; 

      setOnTouchListener(gestureListener); 

이 문서가 문제를 해결하는 것입니다 : 여기

는 샘플 코드입니다.

+0

onFling() 전에 코드 패치를 구현했지만 오류가 발생합니다. setOnTouchListener (gestureListener); 토큰 "gestureListener"에 구문 오류가 있습니다.이 토큰 다음에 VariableDeclaratorId가 필요합니다. 여기서 내가 잘못되었는지 모르겠습니다. @UmerFarooq –

+0

이 코드를 넣으십시오 innside onCreate() –