简体   繁体   English

ScrollView滚动到顶部按钮点击android

[英]ScrollView scroll to top on button click android

I have this strange "bug".我有这个奇怪的“错误”。 A button that change its backgroundcolor inside a scrollview cause the scrollview to scroll up.在滚动视图中更改其背景颜色的按钮会导致滚动视图向上滚动。

Layout布局

<?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>

Inside the inc_reactions_buttons.xml layout there are six buttons in an horizontal linearlayout.在 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>

In the OnClickListener of this buttons, I change the enabled and the backgroundColor of them在此按钮的 OnClickListener 中,我更改了它们的启用和背景颜色

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

The problem is that when a button is clicked, the scrollView scroll to the top.问题是当一个按钮被点击时,scrollView 滚动到顶部。 If I remove the setBackground, the scroll doesn't happens.如果我删除 setBackground,则不会发生滚动。 I've already tried to use other types of view (ImageView, ImageButton) but the result is the same.我已经尝试使用其他类型的视图(ImageView、ImageButton),但结果是一样的。 Happens on API 25 and 26 (didn't test on earlier version)发生在 API 25 和 26(未在早期版本上测试)

Anyone knows the reason?有谁知道原因吗?

Thanks谢谢

Ok, found a solution.好的,找到了解决办法。

As I stated in a comment above, the problem is caused by the TextViews having the attribute "textIsSelectable" set to True.正如我在上面的评论中所述,问题是由将属性“textIsSelectable”设置为 True 的 TextViews 引起的。 It is not clear the reason (something calling the requestChildFocus), but I've found a workaround.目前尚不清楚原因(称为 requestChildFocus),但我找到了一种解决方法。 In my buttons code, I set the textIsSelectable attribute to false, then change the backgroundColor of buttons, then reset the textIsSelectable to true.在我的按钮代码中,我将 textIsSelectable 属性设置为 false,然后更改按钮的 backgroundColor,然后将 textIsSelectable 重置为 true。

Anyone have a better option?有人有更好的选择吗?

Had same problem.有同样的问题。 In my case, simply setting就我而言,只需设置

setKeyListener(null);
setFocusable(false);

for each EditText inside my ScrollView, fixed it.对于我的 ScrollView 中的每个 EditText,修复它。

Setting textIsSelectable to false is a solution too.textIsSelectable设置为 false 也是一种解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM