2015-05-19 65 views
0

我試圖在顯示滑動抽屜時顯示進度對話框。ProgressDialog出現得太晚,並且消失得太快

這是打開抽屜事件處理程序:

public void onDrawerOpened(View drawerView) { 
      progress = ProgressDialog.show(activity, "dialog title", 
        "dialog message", true); 

      openDrawer(); 

     } 

openDrawer()我調用一個函數fillCommunityList(),我需要顯示進度對話框,而其執行

fillCommunityList()實現如下所示:

private void fillCommunityList(){ 
    new Thread(new Runnable() { 
     @Override 
     public void run() { 
      try { 

       // Here you should write your time consuming task... 
       UIManager manager = new UIManager(activity); 
       coms = manager.getCommunities(); 
       progress.dismiss(); 
       getOutTread = false; 
      } catch (Exception e) { 

      } 
      getOutTread = false; 
     } 
    }).start(); 
    // to stop code till thread be finished 
    while(getOutTread){ } 

    SlidingMenuExpandableListAdapter adapter = new SlidingMenuExpandableListAdapter(this, 
      navDrawerItems, coms, mDrawerList); 
    mDrawerList.setAdapter(adapter); 

} 

注意: 我放了一個線程只是爲了讓進度對話窩RKS

我的問題是以下兩點:出現

1-進度對話框太晚突然,然後消失

2-線程花費了大量的時間在其執行(無螺紋fillCommunityList()約需10秒,但有一個線程它不到一分鐘花費更多)

注:manager.getCommunities()具有的AsyncTask在其實施

任何幫助!

回答

1

的問題是以下行

while(getOutTread){ } 

這就是呼叫busy waiting。 UI Thread正忙於循環,不能同時繪製/更新ProgressDialog

+0

感謝您的回覆。 「fillCommunityList()」得到一些數據,我使用這個數據來填充滑動菜單。所以我需要停止執行剩餘的代碼,直到完成線程中的代碼。何這樣做,而不使用while(getOutTread){}? –

+0

一個解決方案是使用委託模式。像listenrt一樣。檢查我的答案在這裏:http://stackoverflow.com/questions/16752073/how-do-i-return-a-boolean-from-asynctask。它是關於異步任務,但同樣的概念可以很容易地適用於您的情況 – Blackbelt

+0

更多描述請! –