2014-02-11 26 views
0

所以我有一個使用數組列表和可擴展列表適配器的項目的動態列表,每組有1個子項和1個項目。我有一個最喜歡的按鈕和一個警報按鈕。警報只有在物品也是最愛時纔有可能。要開始我只是試圖強制提醒按鈕的狀態到最喜歡按鈕的狀態。一旦我能夠做到這一點,我就會開始檢查狀態並執行正確的行爲。Android ToggleButton - setChecked in onCheckedChangeListener只能工作一次

問題是,我第一次檢查警報按鈕時,最喜歡的按鈕也被選中,之後setChecked在偵聽器中似乎沒有任何效果。列表中的其他子視圖也將首次運行,但隨後停止工作。

private SparseArray<ToggleButton> favoriteButtons; 

    ... 
    @Override 
public View getChildView(int groupPosition, int childPosition, 
     boolean isLastChild, View view, ViewGroup parent) 
{ 
    ... 
    //Store the group position in the toggle button 
    ToggleButton tapAlert = (ToggleButton)view.findViewById(R.id.tapAlertButton); 
    tapAlert.setHint(Integer.valueOf(groupPosition).toString()); 
    //add favoriteButton to array at group position 
    favoriteButtons.put(groupPosition, (ToggleButton)view.findViewById(R.id.favoriteButton)); 

    tapAlert.setOnCheckedChangeListener(new OnCheckedChangeListener() 
    { 
     @Override 
     public void onCheckedChanged(CompoundButton toggleButton, boolean isChecked) 
     { 
      int groupPosition = Integer.parseInt((String) toggleButton.getHint()); 
      ToggleButton curButton = favoriteButtons.get(groupPosition); 
      curButton.setChecked(isChecked); 
      //TODO 
     } 
    }); 

XML聲明

 <ToggleButton 
      android:id="@+id/tapAlertButton" 
      android:layout_width="50dp" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:textOn="@string/tap_alert" 
      android:textOff="@string/tap_alert" 
      android:textSize="12sp" 
      android:lines="2" 
     /> 
     <ToggleButton 
      android:id="@+id/favoriteButton" 
      android:layout_width="50dp" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:textOn="@string/fav_list" 
      android:textOff="@string/fav_list" 
      android:textSize="12sp" 
      android:lines="2" 
      android:layout_marginBottom="6dp" 
     /> 

另外一個問題提到的設置機器人:saveEnabled = 「true」 或機器人:saveEnabled = 「假」,但它似乎並沒有改變行爲。

編輯

似乎curButton不再指向孩子視圖的第二呼叫監聽按鈕。

EDIT 2.5

看來getChildView可以很不可預知的,僅通過設置監聽器第一次被調用時,(孩子第一延展),一切似乎是現在的正常工作。在8小時的時間限制結束後,我會發布/接受並回復。

if(favoriteButtons.get(groupPosition, null) == null) 
    { 
     //Store the group position in the toggle button 
     ToggleButton tapAlert = (ToggleButton)view.findViewById(R.id.tapAlertButton); 
     tapAlert.setHint(Integer.valueOf(groupPosition).toString()); 
     //add favoriteButton to array at group position 
     favoriteButtons.put(groupPosition, (ToggleButton)view.findViewById(R.id.favoriteButton)); 
     tapAlert.setOnCheckedChangeListener(new OnCheckedChangeListener() 
     { 
      @Override 
      public void onCheckedChanged(CompoundButton toggleButton, boolean isChecked) 
      { 
       int groupPosition = Integer.parseInt((String) toggleButton.getHint()); 
       ToggleButton curButton = favoriteButtons.get(groupPosition); 
       curButton.setChecked(isChecked); 
       //TODO 
      } 
     }); 
    } 

回答

1

只,如果他們沒有被設置尚未設置監聽器固定檢查我的稀疏陣列空條目

if(favoriteButtons.get(groupPosition, null) == null) 
{ 
    //Store the group position in the toggle button 
    ToggleButton tapAlert = (ToggleButton)view.findViewById(R.id.tapAlertButton); 
    tapAlert.setHint(Integer.valueOf(groupPosition).toString()); 
    //add favoriteButton to array at group position 
    favoriteButtons.put(groupPosition, (ToggleButton)view.findViewById(R.id.favoriteButton)); 
    tapAlert.setOnCheckedChangeListener(new OnCheckedChangeListener() 
    { 
     @Override 
     public void onCheckedChanged(CompoundButton toggleButton, boolean isChecked) 
     { 
      int groupPosition = Integer.parseInt((String) toggleButton.getHint()); 
      ToggleButton curButton = favoriteButtons.get(groupPosition); 
      curButton.setChecked(isChecked); 
      //TODO 
     } 
    }); 
} 
0

您將它設置爲「isChecked」而不是您自己的真/假。

所以關於CheckButton的狀態變化,你或多或少有以下幾點。

如果按鍵器isChecked = false,那麼按鈕setChecked(假)

如果按鍵器isChecked = true,則按鈕setChecked(真)

+0

是的,這是我正在測試的初始行爲。如果我檢查button1,然後檢查button2。問題是,我取消了button1之後,按鈕2仍然被選中。當setChecked被調用時,按鈕2不再改變狀態,只有當用戶手動切換時它纔會改變。 –