微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Android textview 奇怪的行为:焦点改变时自动滚动到顶部

如何解决Android textview 奇怪的行为:焦点改变时自动滚动到顶部

我定义了一个启用了自动滚动到底部的文本视图,它可以工作。 但是当我启动应用程序时,第一次触摸时 textview 内容自动滚动到顶部,这对我来说很奇怪。从那时起,在我手动将其滚动到底部后,它的行为正常。我的意思是,无论后续触摸如何,都不会再次自动滚动到顶部。当附加文本时,它总是滚动到定义的底部。 我如何确定这种行为的原因,以规避它?。 这是文本视图布局:

<LinearLayout
    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"
    android:background="@color/colorScreenBackground"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="xxx.xxxxx.xxxx.MainActivity"
    tools:showIn="@layout/activity_main"
    android:orientation="vertical"
    android:weightSum="100">

    <TextView
        android:id="@+id/result"
        android:scrollbars = "vertical"
        android:textSize="16sp"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="40"
        android:elevation="8dp"
        android:textIsSelectable="true"
        android:gravity="bottom"
        android:background="@color/white"
        android:padding="5dp"
        android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium" />

    <androidx.core.widget.nestedScrollView
        android:id="@+id/nested_scroll"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="60"
        android:background="@color/gray_background"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recyclerview"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:padding="5dp"
            tools:listitem="@layout/recyclerview_item"
            android:background="@color/gray_background"
            />
    </androidx.core.widget.nestedScrollView>
</LinearLayout>

这就是我在代码中使用它的方式

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    sharedPref = getSharedPreferences(getString(R.string.preference_file_key),Context.MODE_PRIVATE);
    setContentView(R.layout.activity_main);

    final Runnable r = new Runnable() {
        @Override
        public void run() {

            doubleClick = false;
        }
    };

    final TextView textView = findViewById(R.id.result);
    textView.setMovementMethod(new ScrollingMovementMethod());
    textView.setTextIsSelectable(true);
    textView.setText(deviceLog.getResult());
    textView.setonLongClickListener(this);
    recyclerView.setonDragListener(this);
    final nestedScrollView nestedScrollView = findViewById(R.id.nested_scroll);
    if (textView.getText().length() == 0) {
        LinearLayout.LayoutParams nestedScrolliewLayoutParams = (LinearLayout.LayoutParams) nestedScrollView.getLayoutParams();
        nestedScrolliewLayoutParams.weight = 100;
        textView.setVisibility(View.GONE);
        nestedScrollView.setLayoutParams(nestedScrolliewLayoutParams);
    }
    textView.setonClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (doubleClick) {
               LinearLayout.LayoutParams nestedScrolliewLayoutParams = (LinearLayout.LayoutParams) nestedScrollView.getLayoutParams();
                LinearLayout.LayoutParams linearLayoutParams = (LinearLayout.LayoutParams) textView.getLayoutParams();

                if (textViewEnlarged) {
                    nestedScrolliewLayoutParams.weight = 60;
                    linearLayoutParams.weight = 40;
                    textViewEnlarged = false;
                } else {
                    nestedScrolliewLayoutParams.weight = 20;
                    linearLayoutParams.weight = 80;
                    textViewEnlarged = true;
                }
                nestedScrollView.setLayoutParams(nestedScrolliewLayoutParams);
                textView.setLayoutParams(linearLayoutParams);
                doubleClick = false;

            } else {
                doubleClick = true;
                Handler doubleClikHandler = new Handler();
                doubleClikHandler.postDelayed(r,500);
            }
        }
    });
}

解决方法

我有一个已经在我的项目上完成的解决方案。 设置文本值后尝试调用 textView.requestFocus()

itemView.text_view_item_description?.text = "..."
itemView.text_view_item_description?.requestFocus()

希望它对你有用。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。