2016-02-01 76 views

回答

1

1)在您的Model-Class中創建boolean字段。
2)定義項目的根目錄Layout(創建id並在ListViewAdapter'sgetView()方法中找到視圖)。
3)爲根Layout添加OnClickListener,如果點擊新的boolean = true。
4)如果您的Model-Class項目的新boolean爲真,請設置根目錄Layout bg_c​​olor_1,否則設置bg_color_2。

2

listItem.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="?android:attr/activatedBackgroundIndicator" 
android:orientation="vertical" > 

<TextView 
    android:id="@+id/tv_list_item" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:gravity="center_vertical" /> 
</LinearLayout> 

您的選擇:listview_selector.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android" 
android:exitFadeDuration="@android:integer/config_mediumAnimTime"> 

<item android:drawable="@drawable/list_pressed" android:state_pressed="true"/> 
<item android:drawable="@drawable/list_selected" android:state_activated="true"/> 
<item android:drawable="@drawable/list_default"/> 

</selector> 

佈局與listivew:

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

<ListView 
    android:id="@+id/listViewMainFragment" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:listSelector="@drawable/listview_selector.xml" 
    android:choiceMode="singleChoice"> 
</ListView> 

</LinearLayout> 
+0

什麼是'android:state_activated'屬性裏面的item? – Dre

1

試試這個

listview.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
      view.setBackgroundColor(Color.GREEN); 
     } 
    }); 
+0

我列表中的其他項目在我按下它們之前也變成綠色, – Dre

相關問題