2014-02-11 2 views
0

배열 목록과 확장 가능한 목록 어댑터를 사용하는 항목의 동적 목록이 있으므로 그룹당 1 개의 하위 항목과 1 개의 항목이 있습니다. 좋아하는 버튼과 경고 버튼이 있습니다. 항목이 즐겨 찾기 인 경우에만 경고가 가능합니다. 시작하려면 즐겨 찾기 버튼 상태로 경고 버튼의 상태를 강제로 설정하려고합니다. 이 작업을 수행 할 수있게되면 실제로 상태를 확인하고 올바른 동작을 구현하기 시작합니다.Android ToggleButton - onCheckedChangeListener의 setChecked는 한 번만 작동합니다.

문제는 청취자의 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 더 이상 포인트를 보인다.

getChildView 만 (아이가 먼저 확장), 청취자에게이 처음 호출을 설정하여, 꽤 예측할 수 없습니다 보인다 2.5

편집, 모든 것이 제대로 작동하는 것 같군. 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 
      } 
     }); 
    } 

답변

1

그들이가 아직 설정되지 않은 경우에만 청취자를 설정하여 고정 (참) setChecked null 배열에 대한 내 희소 배열 검사

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 
     } 
    }); 
} 
0

자신의 true/false 대신 "isChecked"로 설정합니다.

CheckButton의 상태가 변경되면 다음과 같은 결과가 발생합니다. 버튼 = 거짓의 IsChecked 경우

, 다음 버튼 (false)를 setChecked

하면 버튼의 IsChecked = true로 버튼

+0

예. 테스트 초기 동작입니다. button1을 체크하면 button2가 체크됩니다. 문제는 버튼 1을 선택 취소 한 후에 버튼 2가 선택된 상태입니다. setChecked가 호출 될 때 Button 2는 더 이상 상태를 변경하지 않으며, 사용자가 수동으로 토글하면 변경됩니다. –