2013-01-11 47 views
1

我有可滾動的列表視圖項目。每個項目都有以下佈局。列表視圖項目滾動後更改順序

根據需要顯示或隱藏ID爲customer_menu_index的LinearLayout。 Ramaining元素顯示在每個方法調用中。

<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:orientation="vertical" > 

<LinearLayout 
    android:id="@+id/customer_menu_index" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:background="@color/menu_listview_item_alphabet_bg" 
    android:orientation="horizontal" 
    android:visibility="gone"> 

    <TextView 
     android:id="@+id/customer_menu_index_letter" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="5dp" 
     android:paddingBottom="5dp" 
     android:paddingTop="5dp" 
     android:text="bla" 
     android:textColor="@color/menu_listview_item_title" 
     android:textSize="10sp" 
     android:textStyle="bold" 
     android:typeface="sans" /> 
</LinearLayout> 

<RelativeLayout 
    android:id="@+id/customer_menu_item" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:background="@drawable/menu_listview_item_selector" 
    android:orientation="horizontal" > 

    <TextView 
     android:id="@+id/customer_menu_item_title" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="5dp" 
     android:layout_marginRight="0dp" 
     android:paddingBottom="0dp" 
     android:paddingTop="5dp" 
     android:textColor="@color/menu_listview_item_title" 
     android:textSize="10sp" 
     android:textStyle="bold" 
     android:typeface="sans" /> 

    <TextView 
     android:id="@+id/customer_menu_item_subtitle" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_below="@+id/customer_menu_item_title" 
     android:layout_marginLeft="5dp" 
     android:layout_marginRight="0dp" 
     android:paddingBottom="5dp" 
     android:paddingTop="1dp" 
     android:textColor="@color/menu_listview_item_subtitle" 
     android:textSize="8sp" 
     android:textStyle="normal" 
     android:typeface="sans" /> 
</RelativeLayout> 

</LinearLayout> 

我的BaseAdapter方法getView使用此佈局。實現getView方法。

public View getView(int position, View convertView, ViewGroup parentView) { 

    View vi = convertView; 

    if (convertView == null) 
    { 
     vi = inflater.inflate(R.layout.customer_menu_index_item, null); 
    } 

    LinearLayout indexLayout = (LinearLayout) vi.findViewById(R.id.customer_menu_index); 

    HashMap<String, String> category = new HashMap<String, String>(); 
    category = mData.get(position); 

    String currentIndexLetter = category.get(CustomerMenu.KEY_COMPANY).substring(0, 1); 
    if (currentIndexLetter.equals(mLastIndexLetter) == false) 
    { 
     TextView letter = (TextView) vi.findViewById(R.id.customer_menu_index_letter); 
     letter.setText(currentIndexLetter); 
     mLastIndexLetter = currentIndexLetter; 
     indexLayout.setVisibility(LinearLayout.VISIBLE); 
    } 

    TextView title = (TextView) vi.findViewById(R.id.customer_menu_item_title); 
    TextView subtitle = (TextView) vi.findViewById(R.id.customer_menu_item_subtitle); 
    title.setText(category.get(CustomerMenu.KEY_COMPANY)); 
    subtitle.setText(category.get(CustomerMenu.KEY_CONTACT_PERSON)); 

    return vi; 
} 

它加載後工作正常。但是當我滾動列表視圖時,項目的順序發生了變化。

+0

「//用text填充數據」 - 這部分可能是相關的。 – dokkaebi

+0

以及'if(...){' –

+1

是什麼問題? – misco

回答

0

你必須有一個匹配else語句顯示或隱藏的LinearLayout中:

if (currentIndexLetter.equals(mLastIndexLetter) == false) 
{ 
    TextView letter = (TextView) vi.findViewById(R.id.customer_menu_index_letter); 
    letter.setText(currentIndexLetter); 
    mLastIndexLetter = currentIndexLetter; 
    indexLayout.setVisibility(LinearLayout.VISIBLE); 
} 
else { 
    indexLayout.setVisibility(LinearLayout.GONE); 
} 

其他兩個點:

  • if(a.equals(b) == false)相同if(!a.equals(b))。這只是一條捷徑,你不必使用它。
  • 你還應該考慮使用ViewHolder來加速你的代碼。
+0

GONE在XML文件'android:visibility =「gone」'中設置。此更改不能解決問題。 – misco

+1

「GONE在XML中設置」在滾動ListView後,這將不執行任何操作。您應該瞭解ListView的RecycleBin如何工作以及如何使用ViewHolders,這[Google Talk](http://www.google.com/events/io/2009/sessions/TurboChargeUiAndroidFast.html)詳細解釋了這些概念。如果數據亂序,請使用您的調試器來驗證'mData.get(position);'返回您所期望的數據。 – Sam

+0

謝謝您的參考。我從來沒有聽說過關於ListView的RecycleBin。 – misco

相關問題