所以我有一個使用數組列表和可擴展列表適配器的項目的動態列表,每組有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
}
});
}
是的,這是我正在測試的初始行爲。如果我檢查button1,然後檢查button2。問題是,我取消了button1之後,按鈕2仍然被選中。當setChecked被調用時,按鈕2不再改變狀態,只有當用戶手動切換時它纔會改變。 –