有问题的 ListView 让我很难过

发布于 2024-09-14 12:58:28 字数 3491 浏览 7 评论 0原文

我有一个 ListView,其中我在单独的 XML 文件中定义了每个项目的布局。在此文件中,我包含了一个 RatingBar 和一个 EditText

我已通过编程方式在此 ListView 中创建了 7-8 个项目。当我滚动浏览它们时,它似乎有很多问题。以下是一些示例:

  1. 如果我将焦点设置到第一行中的 EditText,然后向下滚动 ListView,则会随机生成 EditTexts其他行将获得焦点。看起来焦点消失后的下一个 EditText 会获得焦点。也许这是故意的,但是,作为一个用户,这看起来很奇怪。

  2. 如果我将焦点设置为 EditText,接收虚拟键盘,输入内容,然后单击虚拟键盘上的“完成”按钮,则 EditText 将为空一旦虚拟键盘消失。

  3. 有时,当我单击 EditText 时,会收到虚拟键盘并开始输入字母,这些字母一输入就会消失。

  4. 当我单击 EditText 时,虚拟键盘会自行显示,但 EditText 失去焦点,我必须再次单击 EditText .

  5. 我已将 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:

  1. If I set focus to the EditText in the first row and then scroll down the ListView, random EditTexts from other rows will have focus. It seems to be that the next EditText after the focused one disappears receives focus. Perhaps this is intentional, but, as a user, it seems very weird.

  2. If I set focus to an EditText, receive a virtual keyboard, type something, and click the "Done" button on my virtual keyboard, the EditText will empty as soon as the virtual keyboard disappears.

  3. Sometimes, when I click an EditText, receive a virtual keyboard and start typing letters, the letters will disappear as soon as I type them.

  4. When I click on an EditText, the virtual keyboard shows itself, but the EditText loses focus and I have to click the EditText again.

  5. Even though I've set the RatingBar to focusable="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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

划一舟意中人 2024-09-21 12:58:28

听起来 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?

昇り龍 2024-09-21 12:58:28

好吧,将其添加到清单中的活动中...

android:windowSoftInputMode="adjustPan"

示例...

<activity android:name=".mapview.AddParkingLotActivity" android:screenOrientation="portrait" android:windowSoftInputMode="adjustPan"/>

Well,add this to your activity in manifest...

android:windowSoftInputMode="adjustPan"

Example...

<activity android:name=".mapview.AddParkingLotActivity" android:screenOrientation="portrait" android:windowSoftInputMode="adjustPan"/>
画骨成沙 2024-09-21 12:58:28

我也遇到了同样的问题。当键盘出现时,LisView 内部的 EditText 失去焦点,因此我从 ListView 项目中获取 getView

final EditText editInput = (EditText)convertView.findViewById(R.id.edit_input);
editInput.requestFocusFromTouch();

这对我有用。

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

final EditText editInput = (EditText)convertView.findViewById(R.id.edit_input);
editInput.requestFocusFromTouch();

And this work for me.

拧巴小姐 2024-09-21 12:58:28

这两个步骤解决了我遇到的类似问题(ListView 中的一行上的 EditText + 有趣的键盘行为)。

  1. 添加 android:windowSoftInputMode="adjustPan" 属性
    ListView 所在活动的 AndroidManifest.xml 文件
    提出。
  2. android:descendantFocusability="afterDescendants" 属性添加到 ListView 本身。

这是实际的示例:

AndroidManifest.xml

    <activity
        android:name=".SubscriptionListActivity"
        android:windowSoftInputMode="adjustPan"
        android:label="@string/title_activity_subscriptions" >
    </activity>

layout.xml 文件

    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:descendantFocusability="afterDescendants"
        android:id="@+id/subscriptionList" />

我在三星 Galaxy S3 上测试了它,它在三星以及 SwiftKey 键盘上运行良好。

These two steps resolved a similar issue I had (EditText on a row within ListView + funny keyboard behavior).

  1. Add android:windowSoftInputMode="adjustPan" attribute into
    AndroidManifest.xml file on the activity where the ListView is
    presented.
  2. Add android:descendantFocusability="afterDescendants" attribute onto the ListView itself.

This is the actual example:

AndroidManifest.xml

    <activity
        android:name=".SubscriptionListActivity"
        android:windowSoftInputMode="adjustPan"
        android:label="@string/title_activity_subscriptions" >
    </activity>

layout.xml file

    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:descendantFocusability="afterDescendants"
        android:id="@+id/subscriptionList" />

I tested it on Samsung Galaxy S3 and it works fine with Samsung as well as SwiftKey keyboards.

眼眸里的那抹悲凉 2024-09-21 12:58:28

我有一些问题我解决了只是评论这一行

this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

i have some issue i solved just commenting this line

this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文