2012-06-19 54 views
0

我有一個佈局,我有一個ScrollView和一些東西。它看起來像:如何禁用ScrollView

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/welcome_scrol_lay" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fillViewport="true" 
    android:background="@drawable/bg"> 
    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" 
     android:background="@drawable/bg" 
     android:weightSum="100" > 
     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="250dp" 
      android:orientation="vertical" 
      android:weightSum="100" 
      android:background="@drawable/specialmapholder"> 
      <android.support.v4.view.ViewPager 
       android:id="@+id/welcomeViewPager" 
       android:layout_width="match_parent" 
       android:layout_height="0dp" 
       android:layout_marginTop="15dp" 
       android:layout_marginLeft="15dp" 
       android:layout_marginRight="15dp" 
       android:layout_weight="90" 
       android:clickable="true" /> 
      <LinearLayout 
       android:id="@+id/welcome_indicatorLayout" 
       android:layout_width="match_parent" 
       android:layout_height="0dp" 
       android:layout_weight="10" 
       android:gravity="center" > 
      </LinearLayout> 
     </LinearLayout> 
     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="50" 
      android:orientation="vertical"> 
      <TextView 
       android:id="@+id/welcome_header" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginTop="15dp" 
       android:layout_marginLeft="20dp" 
       android:layout_marginRight="20dp" 
       android:text="Large Text" 
       android:textColor="@color/white" 
       android:textStyle="bold" 
       android:textSize="26dp" 
       android:textAppearance="?android:attr/textAppearanceLarge" /> 
      <TextView 
       android:id="@+id/welcome_body" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginLeft="20dp" 
       android:layout_marginRight="20dp" 
       android:text="Medium Text" 
       android:textColor="@color/white" 
       android:textSize="14dp" 
       android:textStyle="bold" 
       android:layout_marginTop="20dp" 
       android:textAppearance="?android:attr/textAppearanceMedium" /> 
     </LinearLayout> 
    </LinearLayout> 
</ScrollView> 

正如你所看到的,我在ScrollView中有一個Viewpager。我想在觸摸ViewPager時禁用滾動,因爲當我想更改圖片頁面時會滾動。我如何在觸摸ViewPager時禁用滾動功能?

回答

3

在Android中,家長可以消耗孩子的TouchEvents。在這種情況下,ScrollView消耗ViewPager組件的TouchEvents。爲了避免這種行爲,Android框架提供了onInterceptTouchEvent(MotionEvent)。

在你的情況,你必須繼承ScrollView的子類並改變onInterceptTouchEvent(MotionEvent)方法。您應該更改onInterceptTouchEvent(MotionEvent),以便ScrollView僅在用戶垂直滾動ScrollView時才使用該子項的事件。

的ListView的示例實現:

public class VerticalListView extends ListView { 

private float mLastX; 
private float mLastY; 
private float mDiffX; 
private float mDiffY; 

public VerticalListView(Context context, AttributeSet attrs, int defStyle) { 
    super(context, attrs, defStyle); 
} 

public VerticalListView(Context context, AttributeSet attrs) { 
    super(context, attrs); 
} 

public VerticalListView(Context context) { 
    super(context); 
} 

@Override 
public boolean onInterceptTouchEvent(MotionEvent ev) { 
    switch (ev.getAction()) { 
     case MotionEvent.ACTION_DOWN: 
      // reset difference values 
      mDiffX = 0; 
      mDiffY = 0; 

      mLastX = ev.getX(); 
      mLastY = ev.getY(); 
      break; 

     case MotionEvent.ACTION_MOVE: 
      final float curX = ev.getX(); 
      final float curY = ev.getY(); 
      mDiffX += Math.abs(curX - mLastX); 
      mDiffY += Math.abs(curY - mLastY); 
      mLastX = curX; 
      mLastY = curY; 

      // don't intercept event, when user tries to scroll horizontally 
      if (mDiffX > mDiffY) { 
       return false; 
      } 
    } 

    return super.onInterceptTouchEvent(ev); 
} 

}

+0

謝謝。偉大工程:) – user1302569

+0

高興,我可以幫助你:) – AZ13

+0

我沒有得到在這一類方法onInterceptTouchEvent這與View類擴展..誰能幫助? – djk

0
// Get the ScrollView 
final ScrollView myScroll = (ScrollView) findViewById(R.id.welcome_scrol_lay); 


// Disable Scrolling by setting up an OnTouchListener to do nothing 
    myScroll.setOnTouchListener(new OnTouchListener(){ 

     public boolean onTouch(View v, MotionEvent event) { 
      return true; 
     } 
    }); 


// Enable Scrolling by removing the OnTouchListner 
    tvDisplayScroll.setOnTouchListener(null); ` 

但問題是,這可能完全禁用滾動效果

相關問題