ANDROID:适配器位置返回零并重新添加视图到ListView

时间:2010-11-11 08:55:28

标签: java android android-layout android-listview

我的应用程序需要能够以用户选择的顺序在一行上添加多个视图。

我发现这个教程似乎通过一些修改实现了我想要的目标。

http://www.androidpeople.com/android-custom-listview-tutorial-example/

在遵循教程并进行了必要的更改之后,除了一个奇怪的问题之外,代码可以正常工作。位置递增但是当它达到~9时它返回到零,然后重新添加已经在列表中的视图,因此永远不会达到> 9个。

此外,如果我向下滚动到底部然后备份,则第一个条目已更改!它可能会更改,但我没有检查过。

通过一些测试我发现textSize有一些效果。如果我将它设置得足够小,以便所有“行”一次显示在屏幕上,那么它们看起来很好。

这是我的listview布局,它被夸大了主要布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:minHeight="60dip"
  style="@style/DefaultTheme">
    <TextView 
      android:id="@+id/Line01"
      android:layout_width="5dip"
      android:layout_height="fill_parent"
      android:background="#F00" />
    <TextView
      android:id="@+id/Line02"
      android:layout_width="5dip"
      android:layout_height="fill_parent"
      android:background="#0F0" />
    <TextView
      android:id="@+id/Line03"
      android:layout_width="5dip"
      android:layout_height="fill_parent"
      android:background="#00F"
      android:layout_marginRight="5dip"/>
    <LinearLayout
      android:id="@+id/LinearLayout"
      android:layout_weight="1"
      android:layout_width="wrap_content"
      android:layout_height="fill_parent"
      android:orientation="vertical"
      android:padding="2dip">
        <TextView android:id="@+id/Name"
          android:text="Name"
          style="@style/Name" />
        <TextView 
          android:id="@+id/Status"
          android:text="Status"
          style="@style/Status" />
    </LinearLayout>
    <ImageView 
      android:id="@+id/StatusImage"
      style="@style/StatusImage" />
</LinearLayout>

我改变了背景颜色和文字,但这就是全部。有什么想法是什么问题?

谢谢!

2 个答案:

答案 0 :(得分:0)

那是因为ListView回收了Views。它会根据需要创建尽可能多的视图,然后将不可用的视图作为下一个进行回收。

因此,您必须在适配器的getView或bindView方法中设置colors / attributes。

顺便说一下。你发布的内容不会给ListView带来膨胀,因为它里面没有''对象。最好它可以是一个ListView项,但不是listview本身^^

答案 1 :(得分:0)

我现在对这个问题感到愚蠢,并没有完全回过教程。

我在我的布局中设置了值,只有在创建了新的ViewHolder时,才会在我的主布局上的ListView中设置这些值。因此,当我滚动时,视图被回收,但从未使用正确的视图设置进行更新。

现在一切正常!

干杯