2012-10-05 18 views
1

可能重複:
ListView selection remains persistent after exiting choice modeListView項的DE /選擇編程

我在我的應用程序列表視圖,它包含了listitem_row.xml(佈局我使用內部的多個組件每一個項目)。我面臨的問題是我有一個ActionMode來選擇每個項目。當用戶選擇任何項目時,動作模式被創建並按預期工作。當我關閉動作模式時,我想讓項目看起來沒有被選中/突出顯示/激活/再次選中,因爲它在選擇之前。這裏是我正在使用的代碼:

if (selectedView.isPressed()) { 
    selectedView.setPressed(false); 
} 
if (selectedView.isActivated()) { 
    selectedView.setActivated(false); 
} 
if (selectedView.isSelected()) { 
    selectedView.setSelected(false); 
} 
if (selectedView.isFocused()) { 
    selectedView.clearFocus(); 
    selectedView.dispatchWindowFocusChanged(false); 
} 
selectedView.refreshDrawableState(); 
selectedView.requestLayout(); 
if (selectedView.isDirty()) { 
    selectedView.postInvalidate(); 
} 

對於信息的緣故:我嘗試了所有的組合單獨和完全。我上面粘貼的代碼是我沒有看到任何工作時所達到的狀態。我使用調試來檢查所有狀態,當用戶點擊關閉操作模式按鈕時,處於真實狀態的唯一狀態是激活狀態。

這是我爲ListView行XML:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/row" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:background="?android:attr/activatedBackgroundIndicator" 
    android:orientation="vertical" > 

    <LinearLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/innerrow" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:background="@drawable/listview_selector" 
     android:orientation="vertical" > 

     <TextView 
      android:id="@+id/listViewHeading" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:drawableLeft="@drawable/mydrawable" 
      android:drawablePadding="5sp" 
      android:gravity="center|left" 
      android:paddingBottom="5sp" 
      android:paddingLeft="15sp" 
      android:paddingRight="15sp" 
      android:paddingTop="5sp" 
      android:textAppearance="?android:attr/textAppearanceSmall" 
      android:textColor="@color/app_text_color" 
      android:textStyle="bold" /> 

     <View 
      style="@style/line" 
      android:layout_width="match_parent" 
      android:layout_height="1dp" /> 

     <TextView 
      android:id="@+id/listViewDetail" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:paddingBottom="15sp" 
      android:paddingLeft="15sp" 
      android:paddingRight="15sp" 
      android:paddingTop="5sp" 
      android:textAppearance="?android:attr/textAppearanceSmall" 
      android:textColor="@color/app_text_color" /> 
    </LinearLayout> 

</LinearLayout> 

任何建議/反饋將得到高度讚賞。

+0

感謝Tobias它真的是..我應該關閉這個問題或什麼?因爲我是一個新用戶,所以忍受着我... –

回答

0

對於任何ListView項,其中已經有一個單一選擇模式,只需要兩條線用於去除選擇:

myListView.clearChoices(); 
myListView.requestLayout(); 

這解決了我的問題。