【问题标题】:ScrollView scroll to top on button click androidScrollView滚动到顶部按钮单击android
【发布时间】:2018-02-14 22:36:44
【问题描述】:

我有这个奇怪的“错误”。在滚动视图中更改其背景颜色的按钮会导致滚动视图向上滚动。

布局

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <include layout="@layout/img_loading"  />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textIsSelectable="true"
            android:id="@+id/txt_read_story_title" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textIsSelectable="true"
            android:id="@+id/txt_read_story_author"
            />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textIsSelectable="true"
            android:id="@+id/txt_read_story_content"
             />

        <include layout="@layout/inc_reactions_buttons" />
    </LinearLayout>
</ScrollView>

</android.support.constraint.ConstraintLayout>

在 inc_reactions_buttons.xml 布局中,水平线性布局中有六个按钮。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_horizontal">


    <ImageView
        android:id="@+id/btn_funny"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_reaction_funny" />

 ...

</LinearLayout>

在此按钮的 OnClickListener 中,我更改了它们的启用和背景颜色

mBtnLove.setEnabled(false);
mBtnLove.setBackground(Color.parseColor("#FF000000"))

问题是当一个按钮被点击时,scrollView 滚动到顶部。 如果我删除 setBackground,则不会发生滚动。 我已经尝试过使用其他类型的视图(ImageView、ImageButton),但结果是一样的。 发生在 API 25 和 26 上(未在早期版本上测试)

有人知道原因吗?

谢谢

【问题讨论】:

  • 能否也提供“inc_reactions_buttons”的布局?
  • 我已经用部分 inc_reactions_buttons 布局更新了答案。无论如何,我也尝试直接在活动布局中(在底部)添加一个图像视图,并且行为是相同的,每当我调用 setBackgroundColor 时,滚动视图向上滚动:(
  • 好的,我找到了一些东西,为了简化答案,我省略了 TextView 的一些属性,它们具有“textIsSelectable=true”。使用此属性,每当单击按钮时,滚动视图都会滚动到带有 textIsSelectable 的元素。

标签: android scrollview


【解决方案1】:

好的,找到了解决办法。

正如我在上面的评论中所说,问题是由将属性“textIsSelectable”设置为 True 的 TextViews 引起的。目前尚不清楚原因(调用 requestChildFocus 的东西),但我找到了一种解决方法。在我的按钮代码中,我将 textIsSelectable 属性设置为 false,然后更改按钮的 backgroundColor,然后将 textIsSelectable 重置为 true。

谁有更好的选择?

【讨论】:

    【解决方案2】:

    有同样的问题。就我而言,只需设置

    setKeyListener(null);
    setFocusable(false);
    

    对于我的 ScrollView 中的每个 EditText,已修复它。

    textIsSelectable 设置为 false 也是一种解决方案。

    【讨论】:

      猜你喜欢
      • 2015-01-12
      • 1970-01-01
      • 1970-01-01
      • 2021-05-04
      • 2021-01-01
      • 2021-11-11
      • 2018-04-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多