2014-09-13 2 views
1

는이 코드가 다른 활동에서 작동하지 않습니다 : 이제동일한 코드 내 활동 중 하나에 슬쩍 이벤트를 잡으려고하는데

private float x1,x2; 
static final int MIN_DISTANCE = 150; 
@Override 
public boolean onTouchEvent(MotionEvent event) 
{ 
    switch(event.getAction()) 
    { 
     case MotionEvent.ACTION_DOWN: 
      x1 = event.getX(); 
      break; 
     case MotionEvent.ACTION_UP: 
      x2 = event.getX(); 
      float deltaX = x2 - x1; 

      if (Math.abs(deltaX) > MIN_DISTANCE) 
      { 
       // Left to Right swipe action 
       if (x2 > x1) 
       { 
        Toast.makeText(this, "Left to Right swipe [Previous]", Toast.LENGTH_SHORT).show(); 
        this.overridePendingTransition(R.anim.animation_enter, R.anim.animation_leave); 

       } 

       // Right to left swipe action 
       else 
       { 
        Toast.makeText(this, "Right to Left swipe [Next]", Toast.LENGTH_SHORT).show(); 
       } 

      } 
      else 
      { 
       // TO DO 
      } 
      break; 
    } 
    return super.onTouchEvent(event); 
} 

난 그냥 같은 다른 활동에 코드를 작업 이동 왼쪽 또는 오른쪽으로 스 와이프하면 축배 메시지가 표시되지 않습니다. 무엇이 문제를 일으킬 수 있습니까? 나는 정말로 작은 것을 놓치고있는 것을 안다. 그러나 나는 momment에서 그것을 발견 할 수 없다.

EDIT 비 작업 활동 :

public class BulgarianSayings extends Activity { 

    private float x1,x2; 
    static final int MIN_DISTANCE = 150; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_bulgarian_sayings); 
     if (savedInstanceState == null) { 
      getFragmentManager().beginTransaction() 
        .add(R.id.container, new PlaceholderFragment()) 
        .commit(); 
     } 
    } 


    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.bulgarian_sayings, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 
     if (id == R.id.action_settings) { 
      return true; 
     } 
     return super.onOptionsItemSelected(item); 
    } 

    /** 
    * A placeholder fragment containing a simple view. 
    */ 
    public static class PlaceholderFragment extends Fragment { 

     public PlaceholderFragment() { 
     } 

     @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, 
       Bundle savedInstanceState) { 
      View rootView = inflater.inflate(R.layout.fragment_bulgarian_sayings, container, false); 
      return rootView; 
     } 
    } 

    @Override 
    public boolean onTouchEvent(MotionEvent event) 
    { 
     switch(event.getAction()) 
     { 
      case MotionEvent.ACTION_DOWN: 
       x1 = event.getX(); 
       break; 
      case MotionEvent.ACTION_UP: 
       x2 = event.getX(); 
       float deltaX = x2 - x1; 

       if (Math.abs(deltaX) > MIN_DISTANCE) 
       { 
        // Left to Right swipe action 
        if (x2 > x1) 
        { 
         Toast.makeText(this, "Left to Right swipe [Previous]", Toast.LENGTH_SHORT).show(); 
         this.overridePendingTransition(R.anim.animation_enter, R.anim.animation_leave); 

        } 

        // Right to left swipe action 
        else 
        { 
         Toast.makeText(this, "Right to Left swipe [Next]", Toast.LENGTH_SHORT).show(); 
        } 

       } 
       else 
       { 
        // consider as something else - a screen tap for example 
       } 
       break; 
     } 
     return super.onTouchEvent(event); 
    } 
} 
+0

쇼 토스트의 다른 부분 :

// First make an id for your View (LinearLayout, RelativeLayout, etc.) // and then decaler it as a view. View view = findViewById(R.id.rellayout); // then append onTouchListener on it view.setOnTouchListener(Your OnTouchListener Class or Method); 

다른 방법은 활동의 상단에 다음과 같이 작업하는 것입니다 if (Math.abs (deltaX)> MIN_DISTANCE). –

+0

@DivyangMetalia 아무 일도 일어나지 않습니다 ... 사건이 결코 해고되지 않는 것처럼. – chility

+0

나는 내 두 활동 모두에서 동일한 코드를 시도한다. 두 활동을 모두 붙여 넣습니다. –

답변

1

당신은 그것을 작동하게하는보기에 onTouchListener를 추가해야합니다. 당신은 당신이 할 수있는 전경에 구현하려는 경우 예를 들어

:

public class BulgarianSayings extends Activity implements OnTouchListener { 

//Your Other parts