ProgressBar内部DialogFragment冻结/不更新

时间:2016-10-04 14:31:12

标签: android android-dialogfragment android-progressbar

我有一个DialogFragment,点击保存按钮后,我试图在保存操作正在进行时显示ProgressBar。但是,我似乎无法使其发挥作用:

  1. 我尝试将ProgressBar默认显示,当保存操作开始时我注意到它冻结了,所以我怀疑用户界面是冻结的
  2. 当它被隐藏时,我会在点击保存按钮时尝试显示它,但它根本不显示。
  3. 我怀疑它与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();
        }
    }
    

    谢谢!

0 个答案:

没有答案