代码之家  ›  专栏  ›  技术社区  ›  Dave S

Android ToggleButton-setCheckedChangeListener仅工作一次

  •  0
  • Dave S  · 技术社区  · 11 年前

    所以我有一个使用数组列表和可扩展列表适配器的动态项目列表,每个组有一个子项目和一个项目。我有一个收藏夹按钮和一个提醒按钮。只有当该项目也是收藏夹时,才能发出警报。开始时,我只是试图将提醒按钮的状态强制设置为最喜欢的按钮的状态。一旦我能做到这一点,我就会开始检查状态并实施正确的行为。

    问题是,当我第一次检查提醒按钮时,最喜欢的按钮也会被选中,之后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:ssaveEnabled=“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 回复  |  直到 9 年前
        1
  •  3
  •   Dave S    11 年前

    通过检查稀疏数组中的空条目,修复了仅在尚未设置侦听器时设置侦听器的问题

    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
  •   Angry 84    11 年前

    您将其设置为“isChecked”,而不是您自己的true/false。

    因此,在CheckButton的状态更改时,您或多或少会遇到以下情况。

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

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