[英]Selected list item color moves on scrolling the listView in Android
在我的Android應用程序中,我使用的是listview。 列表視圖的代碼如下
<ListView
android:id="@+id/inputselect_listview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:choiceMode="singleChoice"
android:footerDividersEnabled="true"
android:headerDividersEnabled="true"
android:listSelector="@drawable/list_selector"
android:splitMotionEvents="true" >
</ListView>
當用戶從列表視圖中選擇一個項目時,列表項目的背景顏色將根據此行進行更改
android:listSelector="@drawable/list_selector"
編輯:list_selector.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_window_focused="false" android:state_selected="true"
android:drawable="@android:color/transparent" />
<item android:state_selected="true"
android:drawable="@android:color/transparent" />
<item android:state_pressed="true" android:state_selected="false"
android:drawable="@android:color/transparent" />
<item android:state_selected="false"
android:drawable="@color/blue_train" />
</selector>
list_selector.xml位於drawable文件夾中以指定顏色。 現在的問題是,當我從列表視圖中選擇一個項目並滾動列表時,所選項目背景顏色也會根據滾動向下/向上移動。 請提供解決方案,以便選定的項目背景顏色保持滾動狀態。
編輯:這里滾動列表視圖的截圖通過選擇單項。藍色保持原樣
請指教。
似乎在選擇器中未清除狀態導致這種情況。 用一個像
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Selected -->
<item
android:state_focused="true"
android:state_selected="false"
android:drawable="@drawable/focused"/>
<!-- Pressed -->
<item
android:state_selected="true"
android:state_focused="false"
android:drawable="@drawable/pressed" />
<!-- default -->
<item android:drawable="@drawable/default" />
</selector>
在getView()方法中添加行; convertView.setBackgroundResource(selectedItemInAactivityList.contains。(arrayListSetInAdaptor.get(position)?r.drawable.selected:R.drawable.transperant));
這一行解決了你的問題
在onItemClick()方法中存儲集或數組中的單擊/選定位置。 編寫代碼以使用存儲的位置處理適配器的getview()方法中的顏色。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.