簡體   English   中英

ANDROID:適配器位置返回零,並將視圖重新添加到ListView

[英]ANDROID: Adapter position returns to zero and re-adds views to 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>

我更改背景顏色和文本,僅此而已。 任何想法是什么問題?

謝謝!

那是因為ListView回收視圖。 它創建所需數量的視圖,然后將不可用的視圖作為下一個回收。

因此,您必須在適配器的getView或bindView方法中設置顏色/屬性。

順便說一句。 您發布的內容不會使ListView膨脹,因為其中沒有''對象。 充其量只能是一個ListView項,但不是listview本身^^

我現在問這個問題而又沒有完全回顧本教程,這真是愚蠢。

我正在設置布局中的值,僅當創建新的ViewHolder時,才將其放置在主布局中的ListView中。 因此,當我滾動時,視圖被回收,但從未使用正確的“視圖”設置進行更新。

現在一切正常!

干杯

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM