在多行EditText中键入时,ListView滚动到顶部 [英] ListView scrolls to top when typing in multi-line EditText

查看:160
本文介绍了在多行EditText中键入时,ListView滚动到顶部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个简单的ListView,其中每一行都是一个EditText.这是我的R.layout.item:

I create a simple ListView where each row is an EditText. Here is my R.layout.item:

<?xml version="1.0" encoding="utf-8"?>

<EditText xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:inputType="textMultiLine">

</EditText>

注意EditText如何通过android:inputType="textMultiLine"标志允许多行输入.通过ArrayAdapter,我在ListView中填充了许多行,以便可以滚动ListView.在我的情况下,这些物品是40.这是我的MainActivity.java:

Notice how the EditText allows multi-line input through the android:inputType="textMultiLine" flag. By means of an ArrayAdapter I populate the ListView with a number of rows such that the ListView can be scrolled. In my case the items are 40. Here is my MainActivity.java:

public class MainActivity extends ListActivity {

    final int NUM_ITEMS = 40;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        String[] items = new String[NUM_ITEMS];
        for (int i = 0; i < NUM_ITEMS; i++) {
            items[i] = Integer.toString(i);
        }
        setListAdapter(new ArrayAdapter<String>(this, R.layout.item, items));
    }

}

我向下滚动到第20行并开始输入文本,但是当我到达该行的末尾并且EditText更改其大小以适应第二行时,突然ListView会自动一直滚动到最佳.

I scroll down to row 20 and start typing text, but when I reach the end of the line and the EditText changes its size to accommodate for the second row, suddenly the ListView automatically scrolls all the way to the top.

我认为所描述的行为是可怕的用户体验,并且我很难理解为什么应将其作为默认行为.我寻找的行为是使上面的行保持原状,并使下面的行移动必要的量,以便为展开的EditText留出空间.我该如何实现?

I consider the described behaviour a horrible user experience and I struggle to understand why it should be the default. The behaviour I look for is for the rows above to stay put and for the rows below to shift of the necessary amount to leave room for the expanded EditText. How can I achieve it?

一些其他信息,我在针对Android 4.2.2的模拟器上运行此代码,该设备是3.2英寸HVGA滑块(ADP1).我选择了该特定设备来重现用户遇到的错误.

Some additional info, I run this code on the emulator against Android 4.2.2, the device is a 3.2" HVGA slider (ADP1). I've chosen that particular device to reproduce a bug experienced by a user.

推荐答案

实际上是一种奇怪的默认行为.但是,您可以尝试一些简单的步骤来解决该问题,而不用费力地解决问题的原因:

Indeed a weird default behavior. But instead of struggling with the cause of the problem, you can fix it with a few simple steps:

  • 每当用户更改滚动状态时,使用AbsListView.OnScrollListener
  • Keep track of the scrolling state whenever the user changed it, using AbsListView.OnScrollListener's onScroll callback. Save the value of firstVisibleItem.
  • It might be a bit of a hassle, but subclass EditText and override onSizeChanged. In this method, notify your list view (And dont forget to call super.onSizeChanged).
  • When your list view is notified that an EditText has changed, you should call smoothScrollToPosition with the last value of firstVisibleItem you have saved.

这篇关于在多行EditText中键入时,ListView滚动到顶部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆