2012-04-13 68 views
1

我正在使用android中的列表視圖。 我想爲列表視圖中的每個項目設置背景顏色。在列表視圖中爲項目設置背景

如果我使用setbackgroudcolor函數,它顯示不正確的顏色。看起來有應用背景和新顏色的混合顏色。 如果我使用setbackgroundresource基金,它顯示ok。但是,當我點擊一個項目時,顏色不會改變。

我需要設置背景對列表視圖中的每個項目,當點擊一個項目,背景改爲其他顏色或背景

我的代碼:OnView

// Set background 
    RelativeLayout root = (RelativeLayout) vi.findViewById(R.id.p60001_layout_info); 
    if (position % 2 == 0){ 
     root.setBackgroundResource(R.drawable.cell_g_02); 
     //root.setBackgroundColor(R.color.color_fist_in_pack_view); 
    }else{ 
     root.setBackgroundResource(R.drawable.cell_g_01); 
     //root.setBackgroundColor(R.color.color_fist_in_pack_view); 
    } 

行佈局:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/p60001_layout_info" 
android:layout_width="match_parent" 
android:layout_height="30dp"> 

<TextView 
    android:id="@+id/p60001_item_info" 
    android:layout_width="wrap_content" 
    android:layout_height="30dp" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true" 
    android:textSize="15dp" android:layout_marginLeft="10dp" android:fadeScrollbars="false" android:textColor="@color/txt_normal" android:scrollHorizontally="true" android:gravity="center_vertical"> 
</TextView> 

<ImageView 
    android:id="@+id/icon" 
    android:layout_width="22px" 
    android:layout_height="30dp" 
    android:layout_alignParentRight="true" 
    android:layout_centerVertical="true" 
    android:src="@drawable/arrow" android:layout_marginRight="15dp"/> 

<ImageView 
    android:id="@+id/p60001_image_notice_num" 
    android:layout_width="wrap_content" 
    android:layout_height="25dp" 
    android:layout_alignParentTop="true" 
    android:layout_marginRight="30dp" 
    android:layout_toLeftOf="@+id/icon" 
    android:layout_marginTop="5dp" android:src="@drawable/fukidashi" android:visibility="invisible" android:clickable="false" android:focusable="false" android:focusableInTouchMode="false" android:longClickable="false"/> 

<TextView 
    android:id="@+id/p60001_item_notices" 
    android:layout_width="10dp" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
    android:layout_toLeftOf="@+id/icon" 
    android:ems="10" android:layout_marginRight="40dp" android:background="@android:color/transparent" android:freezesText="false" android:focusableInTouchMode="false" android:focusable="false" android:clickable="false" android:selectAllOnFocus="false" android:textSize="20px" android:layout_marginTop="4dp" android:visibility="invisible"> 

    <requestFocus /> 
</TextView> 

</RelativeLayout> 
+0

您使用的自定義適配器 – 2012-04-13 02:16:44

回答

1

如果您使用自定義適配器的概念,並且上面是您的getView代碼。

你必須轉換視圖(每行的xml文件)改變它的背景,如下所示。

if (position % 2 == 0){ 
    converterView.setBackgroundResource(R.drawable.cell_g_02); 
    //root.setBackgroundColor(R.color.color_fist_in_pack_view); 
}else{ 
    converterView.setBackgroundResource(R.drawable.cell_g_01); 
    //root.setBackgroundColor(R.color.color_fist_in_pack_view); 
} 

如果您需要更改焦點上的顏色或按下您使用下面的xml作爲背景。

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_pressed="true" 
     android:drawable="@color/red" /> 
    <item android:state_selected="true" 
     android:drawable="@color/red" /> 
</selector> 

此外,如果需要設置點擊項目的背景,然後一個辦法做到這一點是::::

有一個布爾變量(= FALSE)在你的POJO(豆)你在哪裏存儲每個項目的數據。 因此,當你點擊物品使該變量爲真。而在getView()中,只需檢查該變量是true還是false,並相應地滿足需要。

+1

他爲什麼要這麼做?我的意思是,如果root是convertView,該怎麼辦? – Cristian 2012-04-13 02:23:11

+0

是的,我使用自定義適配器和編輯getView函數。 但是,當您點擊物品時,我已經處理了您的建議,無法更改物品的顏色。點擊項目時是否需要實施? – Ankata 2012-04-13 04:58:44

+0

我試過你的選擇器,並設置爲列表視圖的背景,但是,當點擊項目時,它只顯示一條小紅線。看來當前的背景資源是重疊的, – Ankata 2012-04-13 05:46:08

1

試試這個

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <!-- When selected, use selected state color --> 
    <item android:drawable="@drawable/your_drawable_when_selected" 
      android:state_selected="true" /> 
    <!-- When not selected, use default item color--> 
    <item android:drawable="@drawable/your_drawable_when_unselected" /> 
</selector>