2014-03-25 40 views
-1

我已經在我的RelativeLayout上直接設置了我的onTouchlistener,但它永遠不會被調用。你有什麼建議我可能做錯了嗎?我的代碼:爲什麼我的onTouchEvent從未被解僱?

layout_own_container.setOnTouchListener(new OnTouchListener() { 

     @Override 
     public boolean onTouch(View view, MotionEvent event) { 
      // TODO Auto-generated method stub 
      Toast.makeText(getApplicationContext(), "WTF", Toast.LENGTH_SHORT).show(); 
      final int X = (int) event.getRawX(); 
      float dest = 0; 
      switch (event.getAction() & MotionEvent.ACTION_MASK) { 
      case MotionEvent.ACTION_DOWN: 
       RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) view.getLayoutParams(); 
       _xDelta = X - lParams.leftMargin; 
       break; 
      case MotionEvent.ACTION_UP: 
       switch (view.getId()){ 
       case 1: 
        dest = bIOwn.getWidth() + bIWish.getWidth(); 
        break; 
       } 
       ObjectAnimator animation2 = ObjectAnimator.ofFloat(view, "x", dest); 
       animation2.setDuration(100); 
       animation2.start(); 
       break; 
      case MotionEvent.ACTION_POINTER_DOWN: 
       break; 
      case MotionEvent.ACTION_POINTER_UP: 
       break; 
      case MotionEvent.ACTION_MOVE: 
       float width = layout_own_top.getWidth() - dest; 
       if (view.getWidth() != width){ 
        RelativeLayout.LayoutParams layoutParams_move = (RelativeLayout.LayoutParams) view.getLayoutParams(); 
        layoutParams_move.leftMargin = X - _xDelta; 
        layoutParams_move.rightMargin = -(layout_own_top.getWidth() + 200); 
        view.setLayoutParams(layoutParams_move); 
       } 
       break; 
      } 
      layout_own_top.invalidate(); 
      return true; 
     } 
    }); 

編輯

這是我的xml:

<RelativeLayout 
      android:id="@+id/relative_iown_top" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:background="#26466D" 
      android:orientation="vertical" > 

      <Button 
       android:id="@+id/button_iOwn" 
       style="?android:attr/buttonStyleSmall" 
       android:layout_width="80sp" 
       android:layout_height="match_parent" 
       android:layout_alignParentLeft="true" 
       android:layout_centerVertical="true" 
       android:background="#003f62" 
       android:text="" /> 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_centerVertical="true" 
       android:layout_marginLeft="10sp" 
       android:text="iOwn" 
       android:textColor="#FFFFFF" 
       android:textSize="28sp" 
       android:textStyle="bold" /> 

      <Button 
       android:id="@+id/button_iWish" 
       style="?android:attr/buttonStyleSmall" 
       android:layout_width="80sp" 
       android:layout_height="match_parent" 
       android:layout_centerVertical="true" 
       android:layout_toRightOf="@+id/button_iOwn" 
       android:background="#0e6798" 
       android:text="" /> 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_centerVertical="true" 
       android:layout_marginLeft="85sp" 
       android:text="iWish" 
       android:textColor="#FFFFFF" 
       android:textSize="28sp" 
       android:textStyle="bold" /> 
     </RelativeLayout> 
+0

試過調試代碼? – GrIsHu

+0

在xml中檢查了layout_own_container的id? – Lokesh

+0

@GrIsHu是的,就像我在onClicklistener中設置我的動畫的測試一樣,它完美地工作。我的Toast消息從不顯示,所以事件不會觸發。 – Lunchbox

回答

1

但你錯在這裏做什麼。您提到您需要觸摸RelativeLayout,但您在此使用的是

layout_own_container = new LinearLayout(this); 

表示線性佈局。所以你必須改變並找到你的onCreate()方法的相對佈局的ID。

RelativeLayout rel = (RelativeLayout) findViewById(R.id.relative_iown_top); 

rel.setOnTouchListener(new OnTouchListener() { 

     @Override 
     public boolean onTouch(View v, MotionEvent event) { 
      // TODO Auto-generated method stub 
      Toast.makeText(getApplicationContext(), "WTF", Toast.LENGTH_SHORT).show(); 
     final int X = (int) event.getRawX(); 
     float dest = 0; 
     switch (event.getAction() & MotionEvent.ACTION_MASK) { 
     case MotionEvent.ACTION_DOWN: 
      RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) view.getLayoutParams(); 
      _xDelta = X - lParams.leftMargin; 
      break; 
     case MotionEvent.ACTION_UP: 
      switch (view.getId()){ 
      case 1: 
       dest = bIOwn.getWidth() + bIWish.getWidth(); 
       break; 
      } 
      ObjectAnimator animation2 = ObjectAnimator.ofFloat(view, "x", dest); 
      animation2.setDuration(100); 
      animation2.start(); 
      break; 
     case MotionEvent.ACTION_POINTER_DOWN: 
      break; 
     case MotionEvent.ACTION_POINTER_UP: 
      break; 
     case MotionEvent.ACTION_MOVE: 
      float width = layout_own_top.getWidth() - dest; 
      if (view.getWidth() != width){ 
       RelativeLayout.LayoutParams layoutParams_move = (RelativeLayout.LayoutParams) view.getLayoutParams(); 
       layoutParams_move.leftMargin = X - _xDelta; 
       layoutParams_move.rightMargin = -(layout_own_top.getWidth() + 200); 
       view.setLayoutParams(layoutParams_move); 
      } 
      break; 
     } 
     layout_own_top.invalidate(); 
     return true; 
     } 
    }); 
+0

我犯了一個輕微的錯字,我正在以編程方式添加linearLayout,以便我可以對其進行動畫處理。問題是我需要添加的一些功能是重寫onTouch,所以我需要在該代碼中添加一個檢查來忽略我的佈局。對錯字感到抱歉。如果我刪除了所需的功能,代碼就可以工作。 – Lunchbox

相關問題