Android實現為ListView同時設定點擊時的背景和點擊鬆手之後的背景_Android

來源:互聯網
上載者:User

本文執行個體講述了Android實現為ListView同時設定點擊時的背景和點擊鬆手之後的背景。分享給大家供大家參考。具體分析如下:

這裡要達到的效果是,

(1)點擊ListView的item時會有指定的背景,

(2)鬆手之後,剛才點擊的item也會有指定的背景

實現(1)很簡單:在xml中為ListView設定listSelector即可。

複製代碼 代碼如下:
<ListView 
android:id="@+id/pop_listview_left" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:scrollbars="none" 
android:divider="@color/popup_left_bg" 
android:dividerHeight="1dp" 
android:listSelector="@color/popup_right_bg" 
android:scrollingCache="false" 
/>

實現(2)也很簡單,在adapter中動態改變背景:

複製代碼 代碼如下:
if (position == selectedPosition){ 
    convertView.setBackgroundResource(R.color.left_selected);
}else{ 
    convertView.setBackgroundResource(R.color.left_normal);
}

並且在該ListView的點擊事件中及時更新selectedPosition:
複製代碼 代碼如下:
leftLV.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override 
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
 //更新背景色 
 FirstClassAdapter adapter = (FirstClassAdapter) (parent.getAdapter()); 
 adapter.setSelectedPosition(position); 
 adapter.notifyDataSetChanged(); 
    } 
});

可是,問題出來了:設定了(2)之後,(1)的效果沒了!!!
這是因為,在設定

複製代碼 代碼如下:
convertView.setBackgroundResource(R.color.left_selected);
 
時,(1)中listSelector中指定的顏色會被覆蓋。

解決方案有兩種:

(一)

將convertView的一個純色的背景改為一個selector,並設定其在點擊時的顏色為透明(這樣下面listSelector的顏色就露出來了)。以下是selector_left_normal.xml和selector_left_selected.xml.

複製代碼 代碼如下:
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_pressed="true" 
        android:drawable="@android:color/transparent"/> 
 
    <item android:state_pressed="false" 
        android:drawable="@color/popup_left_bg"/> 
</selector> 

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_pressed="true" 
        android:drawable="@android:color/transparent"/> 
 
    <item android:state_pressed="false" 
        android:drawable="@color/popup_right_bg"/> 
</selector>

然後將(2)中的代碼改為:

複製代碼 代碼如下:
if (position == selectedPosition){ 
    convertView.setBackgroundResource(R.drawable.selector_left_selected); 
}else{ 
    convertView.setBackgroundResource(R.drawable.selector_left_normal); 
}

(二)

參照(一),將ListView的listSelector屬性去掉,並將其顏色複製到上面兩個selector中替換transparent那個顏色。

也就是說,每次點擊ListView的條目之後,設定背景色時,

(a)如果該條目現在是選中狀態,則直接設為某一個顏色

(b)否則,將其顏色設定為一個selector,並在selector中分別指定點擊和沒有點擊時的顏色。

問題圓滿解決。

希望本文所述對大家的Android程式設計有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.