Android ToggleButton - onCheckedChangeListener中的setChecked只能运行一次

时间:2014-02-11 21:51:30

标签: android togglebutton expandablelistadapter

所以我有一个使用数组列表和可扩展列表适配器的动态项目列表,每个组有1个子项和1个项目。我有一个最喜欢的按钮和一个警报按钮。只有在项目也是最喜欢的情况下才能进行提醒。为了开始,我只是试图将警报按钮的状态强制到收藏按钮的状态。实际上,一旦我能够开始工作,我就会开始检查状态并实施正确的行为。

问题是,我第一次检查提醒按钮时,也检查了收藏夹按钮,之后听众中的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
            }
        });
    }

2 个答案:

答案 0 :(得分:2)

通过仅通过检查我的稀疏数组中的空条目来设置尚未设置的侦听器来修复

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 :(得分:0)

您将其设置为“isChecked”而不是您自己的真/假。

因此,在CheckButton的状态更改中,您或多或少会有以下内容。

如果按钮IsChecked = false,则按钮setChecked(false)

如果按钮IsChecked = true,则按钮setChecked(true)