【问题标题】:ScrollView fighting with scroll of TimePicker, Timepicker not scrolling as a resultScrollView 与 TimePicker 的滚动发生冲突,导致 Timepicker 不滚动
【发布时间】:2013-01-16 08:23:14
【问题描述】:

希望这里有一个解决方案,我的 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 不是 Dialog 或任何东西的一部分,希望我能保持这种状态。

【问题讨论】:

    标签: android android-scrollview android-timepicker


    【解决方案1】:

    找到解决方案,创建一个自定义 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
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-05-13
    • 2021-10-15
    • 1970-01-01
    • 1970-01-01
    • 2022-10-25
    • 2013-01-17
    • 1970-01-01
    相关资源
    最近更新 更多