2017-03-21 3 views
0

これは最近のAndroid APIで起きているようだが、問題はマルチチョイスダイアログでチェックボックスを初めて選択しようとすると、 UIを更新するために追加のクリックが必要です。Android DialogFragment - MultiChoiceダイアログボックスのチェックボックスが2回のクリックを更新/要求しない

私のコードは非常に簡単なので、Androidのバグが原因だと確信しています。実験の多くの後

は、答えはそう下記のそれを共有することになり、私を見つけた...

答えて

0

ソリューションのトリッキーな部分はViewオブジェクトを取得することでした。 Viewを取得すると、チェックボックスのUIを更新するためにinvalidate()とすることができます。ここで

は私のDialogFragmentサブクラスからの必需品である:

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? 
    } 

} 
関連する問題