2014-09-22 67 views
1

我有這個在我的xml:爲什麼我無法檢測到滾動視圖內的滑動?

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context=".MainActivity" > 

    <ImageView 
     android:id="@+id/jokeIcon" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:contentDescription="@string/app_name" 
     /> 

    <ScrollView 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_below="@+id/jokeIcon" > 

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

      <TextView 
       android:id="@+id/allJokesTxt" 
       style="?android:textAppearanceMedium" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:gravity="center" 
       android:lineSpacingMultiplier="1.2" 
       android:padding="16dp" /> 

     </LinearLayout> 
    </ScrollView> 

</RelativeLayout> 

這個代碼在我的活動:

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) { 
        changeText(); 

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


private void changeText(){ 
    // Left to Right swipe action 
    if (x2 > x1) { 
     if (jokesCounter > 0) { 
      --jokesCounter; 
      currentJoke.setVisibility(View.VISIBLE); 
      currentJoke.setText(jokesCollector.get(jokesCounter)); 
     } else { 
      Context context = getApplicationContext(); 
      CharSequence text = "xxx"; 
      int duration = Toast.LENGTH_SHORT; 

      Toast toast = Toast.makeText(context, text, duration); 
      toast.show(); 
     } 

    } 

    // Right to left swipe action 
    else { 
     if (jokesLengthCounter >= jokesCounter) { 
      ++jokesCounter; 

      currentJoke.setVisibility(View.VISIBLE); 
      currentJoke.setText(jokesCollector.get(jokesCounter)); 
     } else { 
      Context context = getApplicationContext(); 
      CharSequence text = "xxx"; 
      int duration = Toast.LENGTH_SHORT; 

      Toast toast = Toast.makeText(context, text, duration); 
      toast.show(); 
     } 

    } 

}

現在這是應該改變屏幕上的文本的用戶刷卡時從左到右,或相反的方式。問題是,只有在ìmageView上完成滑動檢測,我的意思是根本沒有檢測到ScrollView(在實際的textView上)內部的滑動。

這是爲什麼發生?我知道我在這裏錯過了一個非常小的部分,但我目前無法發現它。你能給我一個推動力嗎?

回答

相關問題