【问题标题】:progress dialog not getting dismissed in a fragment进度对话框没有在片段中消失
【发布时间】:2017-05-14 05:08:46
【问题描述】:

我有一个根据类别数量添加一些动态用户界面的片段。我需要显示一个进度对话框并在生成 UI 后将其关闭。我在类别获取过程的开始和结束以及 ui 生成结束时调用 pd.show() 和 pd.hide()。我的进度对话框仍然没有被关闭。

 @Override
public void onCreate(@Nullable Bundle savedInstanceState)
{

    super.onCreate(savedInstanceState);
    setTrackedPageName(R.string.analytics_select_service);
    pd = new ProgressDialog(getContext());
    pd.setIndeterminate(true);
    pd.setMessage("Preparing information..");
    pd.setCancelable(false);
    pd.show();


}

@Override
public void onAttach(Context context)
{
    super.onAttach(context);
    App.from(context).inject(this);

    categoriesSubscription = null;
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    View view = inflater.inflate(R.layout.fragment_service_selection, container, false);
    if (view != null)
    {
        ButterKnife.inject(this, view);

        // We're recreating the view - we must be at the top of the scrollview!
        isScrolledDown = false;
    }


    return view;
}


@Override
public void onActivityCreated(Bundle savedInstanceState)
{
    super.onActivityCreated(savedInstanceState);

    if (Categories.hasChildren(category))
    {
        populateWithCategories(category.getChildren());
    }
    else
    {
        categoriesSubscription = categories.list()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .onErrorResumeNext(Observable.empty())
                .subscribe(this::populateWithCategories);
    }




}

@Override
public void onDetach()
{
    super.onDetach();
    if (categoriesSubscription != null && !categoriesSubscription.isUnsubscribed())
    {
        categoriesSubscription.unsubscribe();
    }
}

@Override
public String getTitle()
{
    return null;
}

@Override
public float getDesiredActionBarElevation()
{
    return isScrolledDown ? ActionBarModifyingContent.DEFAULT_ELEVATION : 0;
}

private void populateWithCategories(List<Category> categories)
{
    for (int i = 0; i < categories.size(); i++)
    {
        Category category = categories.get(i);
        if (Categories.isKnown(category)
                && Categories.isValid(category))
                //&& Categories.hasAllowedGenders(category))
        {
            addService(category, i);
        }
    }

    getActivity().runOnUiThread(new Runnable() {
        public void run() {
           pd.dismiss();
            pd.hide();
            pd.cancel();
        }
    });



}

我应该在哪里调用 pd.dismiss 或 pd.hide 或 pd.cancel?

【问题讨论】:

  • 数据显示出来了吗?
  • 是的,它正在显示

标签: java android android-studio android-fragments progressdialog


【解决方案1】:

这对我有用:

@Override
public float getDesiredActionBarElevation()
{
    return isScrolledDown ? ActionBarModifyingContent.DEFAULT_ELEVATION : 0;
}

private void populateWithCategories(List<Category> categories)
{
    for (int i = 0; i < categories.size(); i++)
    {
        Category category = categories.get(i);
        if (Categories.isKnown(category)
                && Categories.isValid(category))
                //&& Categories.hasAllowedGenders(category))
        {
            addService(category, i);
        }
    }

    pd.dismiss();
    pd = null;

【讨论】:

    【解决方案2】:

    由于订阅是在主线程上观察的,因此您不需要在 UI 线程上单独运行它。检查这个:

    private void populateWithCategories(List<Category> categories)
    {
        for (int i = 0; i < categories.size(); i++)
        {
            Category category = categories.get(i);
            if (Categories.isKnown(category)
                    && Categories.isValid(category))
                    //&& Categories.hasAllowedGenders(category))
            {
                addService(category, i);
            }
        }
    
        pd.dismiss();
    
    }
    

    如果这不能解决问题,请尝试改用处理程序:

    private void populateWithCategories(List<Category> categories)
    {
        for (int i = 0; i < categories.size(); i++)
        {
            Category category = categories.get(i);
            if (Categories.isKnown(category)
                    && Categories.isValid(category))
                    //&& Categories.hasAllowedGenders(category))
            {
                addService(category, i);
            }
        }
    
        new Handler().post(new Runnable(){
    
            @Override
            public void run(){
                pd.dismiss();
            }
        });
    }
    

    希望这会有所帮助。

    【讨论】:

    • 它关闭并重新开始。为什么会有这种行为?
    • 您使用哪种方法?第一还是第二?
    【解决方案3】:
    private void populateWithCategories(List<Category> categories)
    {
    dismiss();
    
        for (int i = 0; i < categories.size(); i++)
        {
            Category category = categories.get(i);
            if (Categories.isKnown(category)
                    && Categories.isValid(category))
                    //&& Categories.hasAllowedGenders(category))
            {
                addService(category, i);
            }
        }
    

    【讨论】:

    • 它驳回了我也尝试过第二个答案解决方案,但又重新开始。 (也许)为什么会有这种行为?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多