【发布时间】:2014-02-11 21:51:30
【问题描述】:
所以我有一个使用数组列表和可扩展列表适配器的动态项目列表,每个组有 1 个子项和 1 个项目。我有一个收藏按钮和一个警告按钮。仅当该项目也是收藏夹时才可能发出警报。要开始,我只是试图将警报按钮的状态强制转换为收藏按钮的状态。一旦我可以让它工作,我实际上会开始检查状态并实施正确的行为。
问题是,我第一次勾选alert按钮,收藏按钮也勾选了,之后在listener中的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"
/>
另一个问题提到设置 android:saveEnabled="true" 或 android:saveEnabled="false" 但它似乎并没有改变行为。
编辑
似乎 curButton 在第二次调用监听器时不再指向子视图的按钮。
编辑 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
}
});
}
【问题讨论】:
标签: android togglebutton expandablelistadapter