如何禁用滚动型 [英] How disable ScrollView

查看:176
本文介绍了如何禁用滚动型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个布局,我有一个滚动型和一些东西。它看起来像:

I have a layout where I have a ScrollView and some stuffs. It looks like:

<?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>

正如你可以看到我在滚动型中Viewpager。我想禁用滚动,当我触摸ViewPager,因为当我想改变图片页面滚动。如何禁用滚动功能,当我触摸ViewPager?

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?

推荐答案

在Android上,家长可以消耗孩子的TouchEvents。在这种情况下,滚动型消耗的ViewPager组件的TouchEvents。为了避免这种行为,Android框架提供了onInterceptTouchEvent(MotionEvent)。

In Android, parents can consume child's TouchEvents. In this case, the ScrollView consumes the TouchEvents of your ViewPager component. To avoid this behaviour, Android framework provides the onInterceptTouchEvent(MotionEvent).

在你的情况,你必须继承滚动型和更改onInterceptTouchEvent(MotionEvent)方法。你应该改变onInterceptTouchEvent(MotionEvent),使滚动型消费孩子的事件,只有当用户滚动滚动型垂直。

In your case, you have to subclass ScrollView and change the onInterceptTouchEvent(MotionEvent) method. You should change the onInterceptTouchEvent(MotionEvent), so that the ScrollView consumes the child's event only when the user scrolls the ScrollView vertically.

有关ListView的示例实现:

example implementation for 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);
}

}

这篇关于如何禁用滚动型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆