Android:textView.setText导致scrollview跳动、抖动等问题解决

958 阅读1分钟

持续创作,加速成长!这是我参与「掘金日新计划 · 6 月更文挑战」的第1天,点击查看活动详情

此方法适用于NestedScrollView,ScrollView等任何第三方继承自ScrollView的控件 当scrollView内部嵌套过多view,比如Rv,textview,editext等。此时如果某textview数据需要实时从刷新,setText会使textview获取焦点,就会导致scrollview出现跳动、抖动到焦点位置等情况。

(任选其一) 有以下解决办法:

  1. 将textview的android:layout_width属性设置为"match_parent"。(若下面所有方法无效的情况,此方法可解决)
<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    ...
     />
  1. 去掉textview焦点。
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:focusable="false"
    android:focusableInTouchMode="false"
    ...
  />
  1. 给scrollview的第一个子控件添加焦点:focusable、focusableInTouchMode
<androidx.core.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:focusable="false"
        android:focusableInTouchMode="false">


        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />
 ......
  1. 给scrollview的第一个子控件添加:descendantFocusability = blocksDescendants
<androidx.core.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:descendantFocusability="blocksDescendants">


        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />
            ......
  1. 给textview设置一个宽度
       <TextView
           android:layout_width="30dp"
           android:layout_height="wrap_content"
           />

若转载,请著名出处