Android 对话框内的ProgressBar片段冻结/不更新

Android 对话框内的ProgressBar片段冻结/不更新,android,android-dialogfragment,android-progressbar,Android,Android Dialogfragment,Android Progressbar,我有一个DialogFragment,当单击save按钮时,我试图在保存操作正在进行时显示ProgressBar。然而,我似乎无法让它工作: 我尝试了默认显示的ProgressBar,当保存操作开始时,我注意到它冻结了,所以我怀疑UI冻结了 当它被隐藏并且我尝试在单击“保存”按钮时显示它时,它根本不会出现 我怀疑这与UI线程有关,但我没有主意了。这是我的密码: public class EditUserPreferencesDialogFragment extends DialogFragmen

我有一个DialogFragment,当单击save按钮时,我试图在保存操作正在进行时显示ProgressBar。然而,我似乎无法让它工作:

  • 我尝试了默认显示的ProgressBar,当保存操作开始时,我注意到它冻结了,所以我怀疑UI冻结了
  • 当它被隐藏并且我尝试在单击“保存”按钮时显示它时,它根本不会出现
  • 我怀疑这与UI线程有关,但我没有主意了。这是我的密码:

    public class EditUserPreferencesDialogFragment extends DialogFragment {
    
        private DashboardActivity mDashboardActivity;
    
        public EditUserPreferencesDialogFragment() {
            // Required empty public constructor
        }
    
        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
    
            mDashboardActivity = (DashboardActivity) getActivity();
    
            // Use the Builder class for convenient dialog construction
            final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    
            // Get the layout inflater
            final LayoutInflater inflater = getActivity().getLayoutInflater();
            View view = inflater.inflate(R.layout.fragment_dialog_edit_user_preferences, null);
    
            //Title comes from activity
            builder.setTitle(mDashboardActivity.EditUserPreferencesTitle);
    
            builder.setView(view);
    
            final Switch displaySwitch = (Switch) view.findViewById(R.id.popup_change_pref_sub_group);
            final Switch pushSwitch = (Switch) view.findViewById(R.id.popup_change_pref_push_group);
            final ProgressBar progress = (ProgressBar) view.findViewById(R.id.progress_edit_user_pref);
            final LinearLayout ll = (LinearLayout) view.findViewById(R.id.ll_edit_user_pref);
    
            if (mDashboardActivity.zvUserPreferences.getDisplay().isEmpty()) {
                displaySwitch.setChecked(false);
            } else {
                displaySwitch.setChecked(true);
            }
    
            if (mDashboardActivity.zvUserPreferences.getPush().isEmpty()) {
                pushSwitch.setChecked(false);
            } else {
                pushSwitch.setChecked(true);
            }
    
            builder.setPositiveButton(R.string.save, new DialogInterface.OnClickListener() {
                /**
                 * This method will be invoked when a button in the dialog is clicked.
                 *
                 * @param dialog The dialog that received the click.
                 * @param which  The button that was clicked (e.g.
                 *               {@link DialogInterface#BUTTON1}) or the position
                 */
                @Override
                public void onClick(DialogInterface dialog, int which) {
    
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            progress.post(new Runnable() {
                                @Override
                                public void run() {
                                    progress.setVisibility(View.VISIBLE);
                                }
                            });
                            ll.post(new Runnable() {
                                @Override
                                public void run() {
                                    ll.setEnabled(false);
                                }
                            });
                        }
                    }).start();
    
                    boolean hasChanged = false;
    
                    //save sub and push
                    UserSub userSub = UserSubSingleton.findUserSub(mDashboardActivity.zvUserPreferences
                            .getUserId(), mDashboardActivity.zvUserPreferences.getChild());
    
                    String display = displaySwitch.isChecked() ? "X" : "";
                    String push = pushSwitch.isChecked() ? "X" : "";
    
                    if (!userSub.getDisplay().equals(display)) {
                        userSub.setDisplay(display);
                        hasChanged = true;
                    }
                    if (!userSub.getPush().equals(push)) {
                        userSub.setPush(push);
                        hasChanged = true;
                    }
                    if (hasChanged) {
    
                        try {
                            UserSubSingleton.updateUserSub(userSub);
                            //refresh user preferences data
                            mDashboardActivity.userPrefGroup.clear();
                            mDashboardActivity.userPrefChild.clear();
                            //refresh user pref singletons
                            ZvUserPreferencesSingleton.initialize(getActivity());
                            UserSubSingleton.initialize(getActivity());
                            mDashboardActivity.onUserPrefDisplay(
                                    mDashboardActivity.userPrefUser,
                                    mDashboardActivity.userPrefChild,
                                    mDashboardActivity.userPrefGroup,
                                    mDashboardActivity.userPrefExpListView,
                                    mDashboardActivity.userPrefProfileCode);
    
                            //Show pop-up message
                            Toast.makeText(getActivity(), R.string.preferences_saved, Toast
                                    .LENGTH_SHORT).show();
                        } catch (OnlineODataStoreException e) {
                            //refresh user preferences data
                            mDashboardActivity.userPrefGroup.clear();
                            mDashboardActivity.userPrefChild.clear();
                            //refresh user pref singletons
                            ZvUserPreferencesSingleton.initialize(getActivity());
                            UserSubSingleton.initialize(getActivity());
                            mDashboardActivity.onUserPrefDisplay(
                                    mDashboardActivity.userPrefUser,
                                    mDashboardActivity.userPrefChild,
                                    mDashboardActivity.userPrefGroup,
                                    mDashboardActivity.userPrefExpListView,
                                    mDashboardActivity.userPrefProfileCode);
                            Toast.makeText(getActivity(), R.string.preferences_error, Toast
                                    .LENGTH_SHORT).show();
                        }
                    }
                }
            });
    
            // Create the AlertDialog object and return it
            return builder.create();
        }
    }
    
    谢谢