错误的 ListView 让我很难过 [英] Buggy ListView makes me sad

查看:26
本文介绍了错误的 ListView 让我很难过的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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. 如果我将焦点设置在第一行的 EditText 然后向下滚动 ListView,其他行的随机 EditTexts会有焦点.似乎是焦点消失后的下一个 EditText 获得焦点.也许这是故意的,但作为用户,这似乎很奇怪.

  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.

如果我将焦点设置为 EditText,接收一个虚拟键盘,输入一些东西,然后点击我的虚拟键盘上的完成"按钮,EditText虚拟键盘消失后就会清空.

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.

有时,当我点击一个EditText,收到一个虚拟键盘并开始输入字母时,我一输入字母就会消失.

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

当我点击一个 EditText 时,虚拟键盘会显示出来,但 EditText 失去焦点,我必须点击 EditText再次.

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

即使我将 RatingBar 设置为 focusable="false",如果我移动滚轮,它仍然会抓住焦点.

Even though I've set the RatingBar to focusable="false", if I move my scrollwheel, it still grabs focus.

我的一个问题是当我在虚拟键盘中输入一个字符时所有可见的列表项都会被重绘(并且由于 EditText 的文本被设置为一些空的数据,它被清除了.我不明白为什么每次我输入一个字符时,Android 都会决定重新绘制列表.

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.

这是我用来绘制它们的 XML.它们是白色气泡,带有灰色边框和一些文本、一个 RatingBar 和一个 EditText:

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>

推荐答案

听起来 ListViews 不能很好地处理 EditTexts.我做了一些研究,共识似乎是不要那样做".所以我采取的是创建一个简单的布局文件,它是一个 ScrollView,里面有一个 LinearLayout.在我的 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.

这听起来合理吗?

这篇关于错误的 ListView 让我很难过的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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