1

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

+0

正在显示的数据? –

+0

是的它正在显示 – Ackman

回答

1

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

它解散并重新开始。为什么这种行为? – Ackman

+0

您使用哪种方法?第一还是第二? –

0
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); 
     } 
    } 
+0

它驳回我也尝试过第二个答案的解决方案,但又重新开始。 (也许)为什么会这样? – Ackman

0

这是对我工作:

@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;