2013-04-22 67 views
4

我用下面的代碼來檢測我的活動刷卡:檢測使用onTouchListener輕掃,滾動型

getWindow().getDecorView().getRootView().setOnTouchListener(new OnTouchListener() { 
      @Override 
      public boolean onTouch(View v, MotionEvent event) { 
       int action = event.getAction(); 
       if(action == MotionEvent.ACTION_DOWN){ 

        downX = event.getX(); 
        downY = event.getY(); 
        //mSwipeDetected = Action.None; 
        return true; // allow other events like Click to be processed 
       } 
       else if(action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_CANCEL){ 
        upX = event.getX(); 
        upY = event.getY(); 

        float deltaX = downX - upX; 
        float deltaY = downY - upY; 

        // horizontal swipe detection 
        if (Math.abs(deltaX) > MIN_DISTANCE) { 
         // left or right 
         if (deltaX < 0) { 
          Log.i(TAG, "Swipe Left to Right"); 
          //mSwipeDetected = Action.LR; 
          onClickPrevQues(getWindow().getDecorView().getRootView()); 

          return false; 
         } 
         if (deltaX > 0) { 
          Log.i(TAG, "Swipe Right to Left"); 
          onClickNextQues(getWindow().getDecorView().getRootView()); 
          // mSwipeDetected = Action.RL; 
          return false; 
         } 
        } else if (Math.abs(deltaY) > MIN_DISTANCE) { // vertical swipe 
                    // detection 
         // top or down 
         if (deltaY < 0) { 
          Log.i(TAG, "Swipe Top to Bottom"); 
          //mSwipeDetected = Action.TB; 
          return false; 
         } 
         if (deltaY > 0) { 
          Log.i(TAG, "Swipe Bottom to Top"); 
          //mSwipeDetected = Action.BT; 
          return false; 
         } 
        } 
        return false; 
       } 
       else if(action == MotionEvent.ACTION_MOVE){ 
        return true; 
       } 
       return false; 
      } 
     }); 

的代碼在LinearLayout情況下正常工作。令人驚訝的是,如果我將Linearlayout包裝在ScrollView之內,則不起作用。這裏有什麼問題?

編輯:
佈局:

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
<RelativeLayout 
    android:id="@+id/RelativeLayout1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:background="#ffffff" 
    android:gravity="center_horizontal" > 

    <RelativeLayout 
     android:id="@+id/RelativeLayout2" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center_horizontal" 
     android:orientation="horizontal" 
     android:paddingBottom="0dp" 
     android:paddingLeft="620dip" 
     android:paddingTop="15dip" > 

     <TextView 
      android:id="@+id/text_unattempted" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentTop="true" 
      android:paddingTop="2dp" 
      android:text="TextView" 
      android:textColor="#b4cacc" 
      android:textStyle="bold" /> 
    </RelativeLayout> 

    <LinearLayout 
     android:id="@+id/LinearLayout1" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/RelativeLayout2" 
     android:gravity="center_horizontal" 
     android:orientation="horizontal" 
     android:paddingBottom="5dip" 
     android:paddingTop="5dip" > 

     <RelativeLayout 
      android:id="@+id/relative_group" 
      android:layout_width="fill_parent" 
      android:layout_height="260dp" 
      android:layout_marginLeft="20dp" 
      android:orientation="vertical" > 

      <TextView 
       android:id="@+id/txt_question_nu" 
       android:layout_width="180dp" 
       android:layout_height="wrap_content" 
       android:layout_alignParentLeft="true" 
       android:layout_alignParentTop="true" 
       android:paddingTop="0dp" 
       android:text="Question 1" 
       android:textColor="#b4cacc" 
       android:textSize="28dp" 
       android:textStyle="bold" /> 

      <TextView 
       android:id="@+id/question" 
       android:layout_width="fill_parent" 
       android:layout_height="400dp" 
       android:layout_alignLeft="@+id/txt_question_nu" 
       android:layout_below="@+id/txt_question_nu" 
       android:layout_marginBottom="20dp" 
       android:layout_marginTop="24dp" 
       android:textColor="#000000" 
       android:textSize="20dp" /> 

      <WebView 
       android:id="@+id/questionimage" 
       android:layout_width="wrap_content" 
       android:layout_height="400dp" 
       android:layout_alignLeft="@+id/txt_question_nu" 
       android:layout_alignParentBottom="true" 
       android:layout_below="@+id/txt_question_nu" 
       android:layout_marginTop="24dp" 
       android:fadeScrollbars="false" 
       android:textColor="#000000" 
       android:textSize="20dp" 
       android:textStyle="bold" /> 

      <Button 
       android:id="@+id/hintSolBtn" 
       style="@style/HomeButton" 
       android:layout_width="40dp" 
       android:layout_height="30dp" 
       android:layout_alignParentTop="true" 
       android:layout_toRightOf="@+id/txt_question_nu" 
       android:background="#90b7bb" 
       android:onClick="onClickHintSol" 
       android:scaleType="centerInside" 
       android:text="Hint" 
       android:textColor="#ffffff" /> 
     </RelativeLayout> 
    </LinearLayout> 

    <RelativeLayout 
     android:id="@+id/LinearLayout2" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_marginTop="15dp" 
     android:layout_below="@+id/radio_question_group" 
     android:layout_alignParentLeft="true" 
     android:orientation="horizontal" 
     android:paddingTop="5dip" > 

     <ImageButton 
      android:id="@+id/abandonButton" 
      style="@style/HomeButton" 
      android:layout_width="wrap_content" 
      android:layout_height="50dp" 
      android:layout_alignParentRight="true" 
      android:layout_marginRight="45dp" 
      android:onClick="onAbandonAssess" 
      android:paddingLeft="6dp" 
      android:scaleType="centerInside" 
      android:src="@drawable/abandon" 
      android:visibility="gone" /> 

     <Button 
      android:id="@+id/submitBtn" 
      android:layout_width="150dp" 
      android:layout_height="50dp" 
      android:layout_alignParentRight="true" 
      android:layout_centerVertical="true" 
      android:layout_marginRight="63dp" 
      android:background="#00b6c6" 
      android:onClick="onSubmitAssess" 
      android:paddingLeft="6dp" 
      android:scaleType="centerInside" 
      android:text="Submit" 
      android:textColor="#ffffff" 
      android:textSize="22dp" /> 

     <ImageButton 
      android:id="@+id/prevBtn" 
      style="@style/HomeButton" 
      android:layout_width="120dp" 
      android:layout_height="wrap_content" 
      android:layout_alignParentLeft="true" 
      android:layout_weight="0.01" 
      android:onClick="onClickPrevQues" 
      android:scaleType="centerInside" 
      android:src="@drawable/previous" /> 

     <ImageButton 
      android:id="@+id/nextBtn" 
      style="@style/HomeButton" 
      android:layout_width="120dp" 
      android:layout_height="wrap_content" 
      android:layout_alignParentTop="true" 
      android:layout_toRightOf="@+id/prevBtn" 
      android:onClick="onClickNextQues" 
      android:scaleType="centerInside" 
      android:src="@drawable/next" /> 
    </RelativeLayout> 

    <TextView 
     android:id="@+id/answer_choices" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_below="@+id/LinearLayout1" 
     android:layout_marginLeft="37dp" 
     android:text="Answer Choices" 
     android:textColor="#b4cacc" 
     android:textSize="20dp" 
     android:textStyle="bold" /> 

    <RadioGroup 
     android:id="@+id/radio_question_group" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/answer_choices" 
     android:layout_below="@+id/answer_choices" > 

     <RadioButton 
      android:id="@+id/answer1" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:button="@drawable/radiostate" 
      android:checked="false" 
      android:layout_marginTop="10dp" 
      android:hapticFeedbackEnabled="true" 
      android:height="100dp" 
      android:textColor="#000000" /> 

     <RadioButton 
      android:id="@+id/answer2" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:button="@drawable/radiostate" 
      android:layout_marginTop="10dp" 
      android:checked="false" 
      android:hapticFeedbackEnabled="true" 
      android:textColor="#000000" /> 

     <RadioButton 
      android:id="@+id/answer3" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:button="@drawable/radiostate" 
      android:layout_marginTop="10dp" 
      android:checked="false" 
      android:hapticFeedbackEnabled="true" 
      android:textColor="#000000" /> 

     <RadioButton 
      android:id="@+id/answer4" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:button="@drawable/radiostate" 
      android:layout_marginTop="10dp" 
      android:checked="false" 
      android:hapticFeedbackEnabled="true" 
      android:textColor="#000000" /> 
    </RadioGroup> 

    <!-- <ImageView 
     android:id="@+id/pen_and_paper_image" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:layout_centerVertical="true" 
     android:src="@drawable/pen_paper" 
     android:visibility="gone" /> --> 

    <ImageButton 
     android:id="@+id/erase" 
     android:layout_width="35dp" 
     android:layout_height="35dp" 
     android:layout_above="@+id/radio_question_group" 
     android:layout_below="@+id/LinearLayout1" 
     android:layout_toRightOf="@+id/answer_choices" 
     android:onClick="onEraseChoice" 
     android:src="@drawable/eraser" 
     android:visibility="visible" /> 

</RelativeLayout> 
</ScrollView> 
+0

滾動型服用觸摸事件,而不是線性佈局...更好地利用列表視圖insted的 – Akilan 2013-04-22 07:16:33

+0

@Akilan我有一堆'LinearLayout'裏面的東西,即不能在'ListView'結合在一起。 'ScrollView'是 – Jaguar 2013-04-22 07:18:23

+0

可以發佈linearLayout的屏幕截圖的唯一途徑 – Akilan 2013-04-22 07:21:34

回答

4

您必須設置OntouchListener以滾動型。

ScrollView scrollView = (ScrollView)findViewById(R.id.scrollView1); 
scrollView.setOnTouchListener(new View.OnTouchListener() { 
      @Override 
      public boolean onTouch(View v, MotionEvent event) { 
       switch(event.getAction()){ 
        case MotionEvent.ACTION_DOWN:{ 
         downX = event.getX();} 
        case MotionEvent.ACTION_UP:{ 
         upX = event.getX(); 

         float deltaX = downX - upX; 

         if(Math.abs(deltaX)>0){ 
          if(deltaX>=0){ 
           swipeToRight(); 
           return true; 
          }else{ 
           swipeToLeft(); 
           return true; 
          } 
         } 
        } 
       } 

       return false; 
      } 
}); 
-1

mSlop = ViewConfiguration.get(getActivity())getScaledTouchSlop(); boolean mIsSwiping = false;

public boolean onTouch(View v, MotionEvent event) { 
    //Timber.d("onTouch, v.getId %d, event.getActionMasked %d", v.getId(), event.getActionMasked()); 
    switch (event.getActionMasked()) { 
     case MotionEvent.ACTION_DOWN: 
      mDownX = event.getX(); 
      mDownY = event.getY(); 
      break; 
     case MotionEvent.ACTION_UP: 
     case MotionEvent.ACTION_CANCEL: 
      mIsSwiping = false; 
      break; 
     case MotionEvent.ACTION_MOVE: 
      float deltaX = event.getX() - mDownX; 
      float deltaY = event.getY() - mDownY; 
      if (!mIsSwiping && Math.abs(deltaX) > mSlop && Math.abs(deltaY) < mSlop) { 
       mIsSwiping = true; 
       if (deltaX > 0) { 
        Timber.d("swipe from left to right, slop %d", mSlop); 
        // TODO: 
       } else { 
        Timber.d("swipe from right to left"); 
        // TODO: 
       } 
       return true; 
      } else if (!mIsSwiping && Math.abs(deltaY) > mSlop) { 
       mIsSwiping = true; 
       Timber.d("swipe vertical"); 
       return true; 
      } 

    } 
    return false; 
} 
+0

您應該考慮在答案中添加一些解釋。即使有一些代碼,這也是非常含糊的。 – Nicolas 2017-03-31 18:30:06