• 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏吧

进度对话框没有被解雇的片段

java 来源:Ackman 7次浏览

我有一个片段,增加了一些基于类别的动态用户界面。我需要显示一个进度对话框,并在用户界面生成时关闭它。我在类别获取过程的开始和结束以及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?


===========解决方案如下:

由于订阅是在主线程上观察到的,因此您无需单独在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(); 
     } 
    }); 
} 

希望这有助于。


版权声明:本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系管理员进行删除。
喜欢 (0)