有问题的 ListView 让我很难过
我有一个 ListView,其中我在单独的 XML 文件中定义了每个项目的布局。在此文件中,我包含了一个 RatingBar
和一个 EditText
。
我已通过编程方式在此 ListView
中创建了 7-8 个项目。当我滚动浏览它们时,它似乎有很多问题。以下是一些示例:
如果我将焦点设置到第一行中的
EditText
,然后向下滚动ListView
,则会随机生成EditTexts
其他行将获得焦点。看起来焦点消失后的下一个EditText
会获得焦点。也许这是故意的,但是,作为一个用户,这看起来很奇怪。如果我将焦点设置为
EditText
,接收虚拟键盘,输入内容,然后单击虚拟键盘上的“完成”按钮,则EditText
将为空一旦虚拟键盘消失。有时,当我单击
EditText
时,会收到虚拟键盘并开始输入字母,这些字母一输入就会消失。当我单击
EditText
时,虚拟键盘会自行显示,但EditText
失去焦点,我必须再次单击EditText
.- 我已将
RatingBar
设置为focusable="false"
,如果我移动滚轮,它仍然会捕获焦点。
我的问题之一是,当我在虚拟键盘中键入字符时,所有可见列表项都会重新绘制(并且由于 EditText
的文本设置为一些数据,该数据为空,因此会被清除。我不明白为什么每次我输入一个字符时 Android 都会决定重新绘制列表。
这是我用来绘制它们的 XML。它们是带有灰色边框的白色气泡和一些文本,一个 。 RatingBar
和里面的 EditText
:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="10dip"
android:paddingBottom="10dip"
android:paddingLeft="15dip"
android:paddingRight="15dip"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="2dip"
android:background="@drawable/shape_outer">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="2dip"
android:background="@drawable/shape_inner">
<TextView
android:id="@+id/rating_category"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/dark_gray"
android:textStyle="bold"
android:layout_marginBottom="10dip" />
<RatingBar
android:id="@+id/rating_rating"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="5"
android:rating="0"
android:stepSize="1"
android:focusable="false"
android:clickable="false"
/>
<EditText
android:id="@+id/rating_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_column="1"
android:padding="6dip"
android:textColor="#000000"
android:gravity="left|top"
android:lines="3"
android:hint="Comment"
android:imeOptions="actionDone" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
I have a ListView
where I've defined the layout of each item in a separate XML file. In this file I've included a RatingBar
and an EditText
.
I've programmatically created 7-8 items in this ListView
. When I scroll through them, it seems to be quite buggy. Here are some examples:
If I set focus to the
EditText
in the first row and then scroll down theListView
, randomEditTexts
from other rows will have focus. It seems to be that the nextEditText
after the focused one disappears receives focus. Perhaps this is intentional, but, as a user, it seems very weird.If I set focus to an
EditText
, receive a virtual keyboard, type something, and click the "Done" button on my virtual keyboard, theEditText
will empty as soon as the virtual keyboard disappears.Sometimes, when I click an
EditText
, receive a virtual keyboard and start typing letters, the letters will disappear as soon as I type them.When I click on an
EditText
, the virtual keyboard shows itself, but theEditText
loses focus and I have to click theEditText
again.Even though I've set the
RatingBar
tofocusable="false"
, if I move my scrollwheel, it still grabs focus.
One of my problems is all the visible list items get redrawn when I type a character in the virtual keyboard (and since the text of the EditText
is set to some data, which is empty, it gets cleared. I don't understand why Android would decide to redraw the list every time I type a character.
Here is the XML I'm using to draw them. They are white bubbles, with a gray boarder, and some text, a RatingBar
and an EditText
inside:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="10dip"
android:paddingBottom="10dip"
android:paddingLeft="15dip"
android:paddingRight="15dip"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="2dip"
android:background="@drawable/shape_outer">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="2dip"
android:background="@drawable/shape_inner">
<TextView
android:id="@+id/rating_category"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/dark_gray"
android:textStyle="bold"
android:layout_marginBottom="10dip" />
<RatingBar
android:id="@+id/rating_rating"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="5"
android:rating="0"
android:stepSize="1"
android:focusable="false"
android:clickable="false"
/>
<EditText
android:id="@+id/rating_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_column="1"
android:padding="6dip"
android:textColor="#000000"
android:gravity="left|top"
android:lines="3"
android:hint="Comment"
android:imeOptions="actionDone" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
听起来 ListView 无法很好地处理 EditText。我做了一些研究,共识似乎是“不要这样做”。所以我所采取的方法是创建一个简单的布局文件,它是一个带有 LinearLayout 的 ScrollView 。在我的 onCreate 方法中,我膨胀了用于列表项的视图并将其添加到 LinearLayout。我还将视图添加到 ArrayList,以便稍后可以将数据保存在每个视图中。
这听起来合理吗?
It sounds like ListViews aren't able to handle EditTexts well. I've done some research and the consensus seems to be "don't do that." So what I've resorted to is creating a simple layout file which is a ScrollView with a LinearLayout inside. In my onCreate method, I inflate the View I was using for my list item and add it to the LinearLayout. I'm also adding the View to an ArrayList so I can save the data in each View later on.
Does this sound reasonable?
好吧,将其添加到清单中的活动中...
示例...
Well,add this to your activity in manifest...
Example...
我也遇到了同样的问题。当键盘出现时,LisView 内部的 EditText 失去焦点,因此我从 ListView 项目中获取 getView
这对我有用。
I was having the same problem. My EditText inside of LisView was losing focus when the keyboard appears, so on getView from the ListView item I put
And this work for me.
这两个步骤解决了我遇到的类似问题(ListView 中的一行上的 EditText + 有趣的键盘行为)。
ListView 所在活动的 AndroidManifest.xml 文件
提出。
这是实际的示例:
AndroidManifest.xml
layout.xml 文件
我在三星 Galaxy S3 上测试了它,它在三星以及 SwiftKey 键盘上运行良好。
These two steps resolved a similar issue I had (EditText on a row within ListView + funny keyboard behavior).
AndroidManifest.xml file on the activity where the ListView is
presented.
This is the actual example:
AndroidManifest.xml
layout.xml file
I tested it on Samsung Galaxy S3 and it works fine with Samsung as well as SwiftKey keyboards.
我有一些问题我解决了只是评论这一行
i have some issue i solved just commenting this line