【问题标题】:Buggy ListView makes me sadBuggy ListView 让我很难过
【发布时间】:2011-03-28 23:31:49
【问题描述】:

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

我已经在这个ListView 中以编程方式创建了 7-8 个项目。当我滚动浏览它们时,它似乎很麻烦。下面是一些例子:

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

  2. 1234563
  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>

【问题讨论】:

  • 我刚刚投了赞成票,因为我喜欢你的问题名称。你的虚拟设备有多强大?
  • 大声谢谢。创建虚拟设备时我唯一做的事情(我只是在这里输入“VD”并想我会拼写出来)是输入一个名称并选择目标(2.1)。其他一切都是默认的。
  • 这里似乎发生的是,当我在虚拟键盘中键入一个字符时,所有可见的列表项都会重新绘制(并且由于 EditText 的文本设置为一些数据,它是空的,它被清除。为什么输入文本会重绘列表项???
  • 我有一个错误的 ListView,其中也包含表单元素。用户响应在滚动到视图之外时消失。找到这篇文章后,我很难过我必须重新编码,但很高兴知道我不应该再寻找答案了。 +1
  • 这里也一样。不过,比起悲伤,我更沮丧,希望我早点知道它的局限性。

标签: android listview listviewitem virtual-keyboard


【解决方案1】:

听起来 ListViews 不能很好地处理 EditTexts。我做了一些研究,共识似乎是“不要那样做”。所以我所采取的是创建一个简单的布局文件,它是一个内部带有 LinearLayout 的 ScrollView。在我的 onCreate 方法中,我为列表项使用的 View 膨胀并将其添加到 LinearLayout。我还将视图添加到 ArrayList,以便以后可以将数据保存在每个视图中。

这听起来合理吗?

【讨论】:

  • 这篇文章很棒。我浪费了很多时间试图让文本和下拉输入在 ListView 中工作。读完这篇文章后,我在 ScrollView 中使用 LinearLayout 重新实现,并且第一次完美运行。
  • 当 EditText 不在 ListView 中时会发生这种情况(我的 EditText 嵌套在几个 LinearLayout 和 RelativeLayout 中)。
  • @SamusArin 当您将 EditTexts 嵌套在多个 LinearLayouts 中时,是否有解决此问题的已知方法?
  • @stephen 看看这是否帮不上忙(第二个答案):stackoverflow.com/questions/15161261/viewpager-focus-issue ...顺便说一句,不应该在这里聊天,但如果我的回答不够,您可以发布问题(如果您发帖,请将问题链接通过电子邮件发送至 samusarin@gmail.com)。
  • 有人对此有实施的解决方案吗?我也遇到了带有edittext的listview问题,我不知道如何更改为scrollview。谢谢!
【解决方案2】:

好吧,把它添加到清单中的活动中......

android:windowSoftInputMode="adjustPan"

示例...

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

【讨论】:

  • 在清单中的我的活动中添加 android:windowSoftInputMode="adjustPan" 解决了这个问题。另一个值得赞赏的技巧是一种添加填充的方法,这样编辑文本字段就不会被软键盘面板破坏。
  • 这对我有用,但向我们解释那是什么可能是个好主意; )
【解决方案3】:

我遇到了同样的问题。当键盘出现时,我在 LisView 中的 EditText 失去焦点,所以从我放置的 ListView 项目中获取 getView

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

这对我有用。

【讨论】:

  • 编辑:它不起作用。三星键盘似乎可以正常工作,但 SwiftKey 键盘上的问题仍然存在。
【解决方案4】:

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

  1. 添加 android:windowSoftInputMode="adjustPan" 属性到 ListView 所在的 Activity 上的 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 键盘。

【讨论】:

    【解决方案5】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-19
      • 2020-04-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多