ScrollView与TimePicker的滚动冲突,导致TimePicker无法滚动

9

希望这里有一个解决方案,我的main.xml文件中有一个XML TimePicker和ScrollView,但是设置好之后TimePicker无法滚动。如果我删除ScrollView,Timepicker就可以正常滚动,但是我显然需要同时存在两者。

看起来它们都在争夺触摸事件,但这会导致问题。

这是我的代码:

<ScrollView 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:background="@color/backgroundmain">

    <RelativeLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <!-- Action Title -->
        <ImageView />

        <TextView />

        <!-- Description -->
        <TextView />

        <!-- Have to Button -->
        <TextView />

        <RelativeLayout
            android:id="@+id/TimePickerSection"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:layout_marginTop="5dp"
            android:layout_centerHorizontal="true"
            android:layout_below="@id/">

            <TimePicker 
                android:id="@+id/TimePick"
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:layout_centerVertical="true"
                android:layout_below="@id/HaveToText"/>

            <Button
                android:id="@+id/OkButton"
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:text="@string/OkText"
                android:layout_below="@id/HaveToText"
                android:textSize="20sp"
                android:onClick="TimePickButton"
                android:layout_centerVertical="true"
                android:layout_toRightOf="@id/TimePick"/>

        </RelativeLayout>

    <!-- OR -->
    <TextView />

    <!-- buttons -->
    <TextView />

    <Button />

    </RelativeLayout>

</ScrollView>

我删除了无关的代码,但保留了大纲,以便向您展示布局是如何布置的。
XML中是否有修复程序?我尝试过像 android:fillViewPort="" 这样的东西,但没有任何作用。我的TimePicker不是对话框的一部分,希望能保持这种状态。
1个回答

15

找到解决方案,创建一个自定义的TimePicker类,然后调用它来覆盖触摸事件:

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    // Stop ScrollView from getting involved once you interact with the View
    if (ev.getActionMasked() == MotionEvent.ACTION_DOWN) {
        ViewParent p = getParent();
        if (p != null)
            p.requestDisallowInterceptTouchEvent(true);
    }
    return false;
}

不要忘记覆盖至少第二个构造函数TimePicker(Context context, AttributeSet attrs),并在xml中使用自定义视图的完整路径<com.name.app.CustomDatePicker...>。 - einUsername

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接