0

我有一个布局,其中有一个 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>

As you can see I have a Viewpager in the ScrollView. I want to disable scroll when I touch ViewPager, because when I want to change picture page is scrolled. How can I disable the scroll function when I touch the ViewPager?

4

2 回答 2

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);
}

}

于 2012-06-19T14:37:42.447 回答
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);  ` 

但问题是,这可能会完全禁用滚动效果

于 2012-06-19T14:22:14.450 回答