【问题标题】:HorizontalScrollView keeps scrollable after clearing the text of the TextView清除 TextView 的文本后,Horizo​​ntalScrollView 保持可滚动
【发布时间】:2020-07-26 12:01:09
【问题描述】:

我有一个以 TextSwitcher 作为子级的 Horizo​​ntalScrollView,如下所示。

<HorizontalScrollView
    android:id="@+id/horizontal_scroll_view_equation"
    android:scrollbars="none"
    android:layout_width="match_parent"
    android:layout_weight="1"
    android:layout_height="0dp"
    android:fillViewport="true"
    android:layout_gravity="right">

    <TextSwitcher
        android:id="@+id/text_switcher_equation"
        android:layout_gravity="right"
        android:layout_width="wrap_content"
        android:layout_height="match_parent">

        <TextView
            android:layout_marginRight="@dimen/spacing_medium"
            android:layout_marginLeft="@dimen/spacing_medium"
            android:id="@+id/text_view_equation_default"
            android:background="@android:color/transparent"
            android:gravity="center_vertical|right"
            android:maxLines="1"
            android:textSize="@dimen/font_xxxlarge"
            android:scrollHorizontally="true"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
        <TextView
            android:layout_marginRight="@dimen/spacing_medium"
            android:layout_marginLeft="@dimen/spacing_medium"
            android:id="@+id/text_view_equation_anim"
            android:background="@android:color/transparent"
            android:gravity="center_vertical|right"
            android:maxLines="1"
            android:textSize="@dimen/font_xxxlarge"
            android:scrollHorizontally="true"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </TextSwitcher>
</HorizontalScrollView>

上面这段代码的目标是创建一个水平滚动的TextView。此代码是计算器的一部分,因此 TextView 的文本是动态的。这意味着有时 TextView 需要可滚动,有时则不需要(取决于文本的长度)。

我现在的奇怪行为如下。一旦用户输入越来越多的字符,TextView 就会变得完全可滚动。但是,当用户清除方程式时,Horizo​​ntalScrollView 保持折叠状态,这意味着它仍然可以滚动,而 TextView 的文本只有一个字符(零)。这意味着用户可以左右滚动文本,而文本只有一个字符。

在下文中,将演示此行为(请参见下图)。在左图中,用户输入了一个方程。然后,用户点击clear,方程被清除,即设置为0。但是,如右图所示,TextView仍然可以向右滚动。

另一个显着的现象是,当用户再次单击清除(连续两次)时,TextView 的滚动是不可能了。出于某种原因,当用户第一次点击重置时,情况并非如此,但我不明白为什么。

我想要实现的是,一旦 TextView 的文本被清除(因此 TextView 的文本包含一个字符),用户就不可能向左或向右滚动文本。

【问题讨论】:

    标签: android textview horizontalscrollview textswitcher


    【解决方案1】:

    当点击清除时滚动到最右边(如果用户滚动到左边)你可以通过

                    while(scoll_view.canScrollHorizontally(1)) {
                        scoll_view.scrollBy(50, 0);
                    }
    

    现在您需要在单击清除时禁用滚动,并在用户单击另一个按钮时重新启用它(在您的情况下为其他数字) 你就那样做

    将此添加到您的班级并使用拦截来禁用和启用滚动

    class OnTouch implements View.OnTouchListener {
            public boolean intercept = false;
            @Override public boolean onTouch(View v, MotionEvent event) {
                return intercept;
            } 
    }
    

    将 onTouchListener 应用到 Horizo​​ntalScrollView

        final OnTouch listener = new OnTouch();
        scoll_view.setOnTouchListener(listener);
    

    当用户在 while 滚动禁用滚动后点击清除时

                listener.intercept = true;
    

    通过执行重新启用滚动

                listener.intercept = false;
    

    【讨论】:

    • @t.r.hogenkamp 它应该可以工作我已经测试过了,你能包含一些代码吗??
    • 我正在为 TextSwitcher 使用滑出和滑入动画。这里的问题是您在滑出之前清除文本。结果,滑出的动画没有显示,因为文本是空的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多