【问题标题】:Android ToggleButton - setChecked in onCheckedChangeListener only works onceAndroid ToggleButton - onCheckedChangeListener 中的 setChecked 仅工作一次
【发布时间】: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


    【解决方案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
            }
        });
    }
    

    【讨论】:

      【解决方案2】:

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

      所以关于 CheckButton 的状态变化,你或多或少有以下几点。

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

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

      【讨论】:

      • 是的,这是我正在测试的初始行为。如果我检查 button1,则检查 button2。问题是,在我取消选中按钮 1 后,按钮 2 仍处于选中状态。按钮 2 在调用 setChecked 时不再改变状态,只有在用户手动切换时才会改变。
      猜你喜欢
      • 2012-04-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多