2014-02-17 36 views
0

我是新的Android開發人員,實際上我是網絡開發人員。問題類似於html + css類。Android設計實踐 - 開關視圖

在我的應用程序中,我有簡單的listView,自定義行視圖。對於不同的行狀態,我需要切換行的許多子元素,如顏色,背景顏色,某些元素的可見性。

我可以在我的CustomArrayAdapter的代碼中執行此操作。但它看起來很髒。

我該如何做到「美麗」?有類似於HTML中的類的android API嗎?

public View getView(int position, View convertView, ViewGroup parent) { 
    // ... 
    Integer items_count = merc.getItemsCount(); String item_count_label = ""; 
    if(0 < items_count){ 
     item_count_label = String.valueOf(items_count); 
    } else { 
     item_count_label = ""; 
    } 
    countTextView.setText(item_count_label); 

    return rowView; 
} 

<TextView 
    android:id="@+id/label" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Item label" 
    android:focusable="false" 
    android:textSize="18dp" 
    android:textIsSelectable="true"> 
</TextView> 
<TextView 
    android:id="@+id/profit" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="" 
    android:textSize="12dp" 
    android:textColor="#00aa00" 
    android:layout_weight="1" 
    android:layout_marginLeft="15px"> 
</TextView> 
<TextView 
    android:id="@+id/count" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="6" 
    android:textSize="18dp" > 
</TextView> 

+0

你能顯示你的代碼嗎? – Raptor

+0

現在很簡單。 https://github.com/Kein1945/LumiRoMerchantsAndroid。 文件src/com/lumiro/merchantmonitor/view/MerchantArrayAdapter.java 和res/layout/merchant_row.xml。但後來我需要添加更多元素。 – Kein

+0

請在問題中粘貼相關的代碼,而不僅僅是一個鏈接。 – Raptor

回答

1

通常你會重寫getViewTypeCount()並返回不同的行 「類型」 你的ListView可以有(假設N種)的數量,並覆蓋getItemViewType()返回從0到(N-1)的值。然後,在getView()中,您可以調用getItemViewType(position)switch來覆蓋返回值,以擴大正確的行佈局,並執行所需的任何操作。

我建議你看這個視頻:The World of ListView