2017-03-21 12 views
0

최근 안드로이드 API로만 시작되었다고 생각하지만 문제는 처음으로 MultiChoice 대화 상자에서 체크 박스를 선택하려고 할 때, UI를 업데이트하려면 추가 클릭이 필요합니다.안드로이드 DialogFragment - MultiChoice 대화 상자 체크 박스가 두 번의 클릭으로 업데이트/필요 없음

내 코드가 매우 간단하기 때문에 안드로이드 버그로 인한 것 같습니다. 실험의 많은 후

, 나는 솔루션의

답변

0

까다로운 부분이 View 개체를 얻을 수 있었다 ... 답변이 너무 아래를 공유 할 것입니다 발견했습니다. View이 있으면 확인란의 UI를 업데이트하기 위해 invalidate() 수 있습니다.

public class MyMultiChoiceDialogFragment extends DialogFragment { 

    private View mView = null; 

    @Override @NonNull 
    public Dialog onCreateDialog(Bundle savedInstanceState) { 
     AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 
     builder.setTitle(title); 
     builder.setMultiChoiceItems(
      cursor, 
      isCheckedColumn 
      labelColumn 
      new DialogInterface.OnMultiChoiceClickListener() { 

      @Override 
      public void onClick(DialogInterface dialogInterface, int which, boolean isChecked) { 

       // Handle the checkbox de/selection 

       /* 
       * The problem is that, despite onClick being called (with the correct parameter values), the 
       * checkbox ticks were not updating on the UI. 
       * Solution is to invalidate/redraw the layout so the checkboxes also update visually 
       */ 
       mView.invalidate(); 
       mView.forceLayout(); // Following tests, this line is also required. 

      } 
     }); 

     AlertDialog dialog = builder.create(); 

     /* 
     * This seems to be the only way to get the view. 
     * Save it in an instance variable so we can access it within onClick() 
     */ 
     mView = dialog.getListView(); 

     return dialog; 
    }  

    @Override 
    public void onDestroyView() { 
     super.onDestroyView(); 
     mView = null; // Clean up/prevent memory leak - necessary? 
    } 

} 
: 여기

DialogFragment 서브 클래스에서 필수입니다