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;