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



我定义了一个textview与自动滚动到底部启用,它的工作。但当我启动应用程序时,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()

希望它对你有用。

相关内容

  • 没有找到相关文章

最新更新