ScrollView与TimePicker的滚动战斗,因此Timepicker不会滚动

RED*_*ED_ 8 android android-scrollview android-timepicker

希望这里有一个解决方案,我在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>
Run Code Online (Sandbox Code Playgroud)

我删除了不相关的代码,但保留大纲只是为了向您展示布局的布局.

XML中是否有修复?我尝试了类似android:fillViewPort=""但没有做任何事情的事情.我的TimePicker不是Dialog或其他任何东西的一部分,希望我能保持这种方式.

RED*_*ED_ 14

找到解决方案,创建一个自定义的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;
}
Run Code Online (Sandbox Code Playgroud)