2013-03-14 89 views
0

問題是隻有在api調用完成後纔會加載進度微調器。我想它只是在我返回視圖之後才導致出現。 我不知道如何將此添加到對話框之前和正在進行的API調用?在自定義DialogPreference中加載列表視圖之前加載進度條

protected View onCreateDialogView() { 

    subscriptions = null; 
    LayoutInflater inflater = ((SettingsActivity) ctx).getLayoutInflater(); 
    final View vw = inflater.inflate(R.layout.channel_content_view, null); 

    header = ((SettingsActivity) ctx).getLayoutInflater().inflate(R.layout.channels_header,null); 
    ((TextView) header.findViewById(R.id.channels_header)).setText("Channels"); 

    LinearLayout linlaHeaderProgress = (LinearLayout) vw.findViewById(R.id.channelsProgress); 
    linlaHeaderProgress.setVisibility(View.VISIBLE); 

//  Do this in a new Thread 

    final ListView lv = (ListView) vw.findViewById(R.id.list); 

//  ((TextView) lv.findViewById(R.id.channel_desciption)).setText("CHANNELS"); 
    Thread thread = new Thread() 
    { 
     @Override 
     public void run() { 
//      get the all the channels from api on web 
        channels = Json.getJson(apiurl + "/rest/channel/"+ linkid +"/"+ username, "GET", null); 
     } 
    }; 

    thread.start(); 

    lv.addHeaderView(header); 

    return vw; 
} 

channel_content_view.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/channels_list" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:clickable="true" 
    android:orientation="horizontal" > 
    <LinearLayout 
     android:id="@+id/channelsProgress" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:gravity="center" 
     android:orientation="vertical" 
     android:visibility="gone" > 

     <ProgressBar 
      android:id="@+id/pbChannelsProgress" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" > 
     </ProgressBar> 
    </LinearLayout> 
    <ListView 
     android:id="@+id/list" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:descendantFocusability="blocksDescendants" > 
    </ListView> 

</LinearLayout> 

回答

0

不知道如果這是去工作,但你可以嘗試先創建對話框,然後改變內容!

這是醜陋的,可以用的AsyncTask被輕易改寫的,但我認爲它會做的伎倆:

protected View onCreateDialogView() { 

    subscriptions = null; 
    LayoutInflater inflater = ((SettingsActivity) ctx).getLayoutInflater(); 
    final View vw = inflater.inflate(R.layout.channel_content_view, null);  
    LinearLayout linlaHeaderProgress = (LinearLayout) vw.findViewById(R.id.channelsProgress); 
    linlaHeaderProgress.setVisibility(View.VISIBLE); 
    ((TextView) header.findViewById(R.id.channels_header)).setText("Channels"); 

    //  Do this in a new Thread 
    Thread thread = new Thread() 
    { 
     @Override 
     public void run() { 
//      get the all the channels from api on web 
        channels = Json.getJson(apiurl + "/rest/channel/"+ linkid +"/"+ username, "GET", null); 
        setListLayout(vw,linlaHeaderProgress); 
     } 
    }; 
    thread.start(); 



    return vw; 
} 


protected void setListLayout(View vw,View progressBar){ 


    runOnUiThread(new Runnable() { 
       public void run() { 
        Thread.sleep(500); // to avoid super fast results from the server and always display the loader bar for a while 
        header = ((SettingsActivity) ctx).getLayoutInflater().inflate(R.layout.channels_header,null);  
        final ListView lv = (ListView) vw.findViewById(R.id.list); 
        lv.addHeaderView(header); 
        progressBar.setVisibility(hidden); 
        lv.setVisibility(visible); 

       } 
      }); 

} 
0

我對你有兩個建議:

  1. 這可能是因爲線程執行速度太快,以至於在顯示進度條之前提前結束。我會讓睡眠模式下的線程看看是否是這種情況。 (這只是爲了確認到底是怎麼回事不要使用這種方法會造成額外的延遲)

    例如試着將這樣的:

    Thread thread = new Thread() 
         { 
          @Override 
          public void run() { 
          try{ 
          Thread.sleep(3000); 
          } 
          catch(Exception e){} 
        //      get the all the channels from api on web 
             channels = Json.getJson(apiurl + "/rest/channel/"+ linkid +"/"+ username, "GET", null); 
          } 
         }; 
    
    1. 此外,如果上面沒有作品然後嘗試下面的Shwoing進度條的方法,看看它是否工作:

    調用showProgressBar()方法從哪裏創建對話框的實例,或者如果不可能,然後開始的onC reateDialog方法:

    void showProgressBar(Context ctx){ 
    
        progressBar = new ProgressDialog(ctx); //this is a context object probably ctx object will go here as 
        progressBar.setCancelable(false); 
        progressBar.setMessage("Heavy Process...."); 
        progressBar.setProgressStyle(ProgressDialog.STYLE_SPINNER); 
    
        progressBar.show(); 
        } 
    
    protected View onCreateDialogView() { 
    
        ///I think ctx is an Activity 
        showProgressBar(ctx); ///if it is not possible to call showProgressBar() from where you are instantiating the Dialog 
    
        subscriptions = null; 
        LayoutInflater inflater = ((SettingsActivity) ctx).getLayoutInflater(); 
        final View vw = inflater.inflate(R.layout.channel_content_view, null); 
    
        header = ((SettingsActivity) ctx).getLayoutInflater().inflate(R.layout.channels_header,null); 
        ((TextView) header.findViewById(R.id.channels_header)).setText("Channels"); 
    
        /* 
        Commented below lines of Progress Bar because we are displayin it above. 
        */ 
        //LinearLayout linlaHeaderProgress = (LinearLayout) vw.findViewById(R.id.channelsProgress); 
        ///linlaHeaderProgress.setVisibility(View.VISIBLE); 
    
    //     Do this in a new Thread 
    
        final ListView lv = (ListView) vw.findViewById(R.id.list); 
    
    //  ((TextView) lv.findViewById(R.id.channel_desciption)).setText("CHANNELS"); 
    
        Thread thread = new Thread() 
        { 
    
        @Override 
        public void run() { 
    
    //      get the all the channels from api on web 
    
          channels = Json.getJson(apiurl + "/rest/channel/"+ linkid +"/"+ username, "GET", null); 
    
        } 
    
        }; 
    
    
    
        thread.start(); 
    
        lv.addHeaderView(header); 
    
        return vw; 
    } 
    

而且如果你想在對話框中的使嵌入進度,那麼你必須創建一個活性,並做出出現一個對話框。爲了簡單地增加

下面從Android文檔頁面(http://developer.android.com/guide/topics/ui/dialogs.html)複製:

提示:如果您想自定義對話框,您可以改爲顯示活動的對話,而不是使用對話框的API 。只需創建一個活動,並設置其主題Theme.Holo.Dialog在manifest元素:

注意這是很容易在大多數的onCreateDialogView的代碼移植到活動onCreate()方法

+0

進度條顯示做,但它的錶盤背後ogview。我該如何使它成爲頂尖? – Harry 2013-03-15 07:24:02

+0

我想在對話框中顯示加載欄 – Harry 2013-03-15 07:31:27

+0

@Harry這意味着你需要一個自定義的對話框。然後,您將不得不將一個活動顯示爲一個對話框。我會更新我的答案... – 2013-03-15 08:59:14

0

這其實是一個很簡單的問題,我的佈局

改變我的佈局:工作

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/channels_list" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:clickable="true" 
    android:orientation="horizontal" > 
    <LinearLayout 
     android:id="@+id/channelsProgress" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:gravity="center" 
     android:orientation="vertical" 
     android:visibility="gone" > 

     <ProgressBar 
      android:id="@+id/pbChannelsProgress" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:descendantFocusability="beforeDescendants" > 
     </ProgressBar> 
    </LinearLayout> 
    <ListView 
     android:id="@+id/list" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:descendantFocusability="afterDescendants" > 
    </ListView> 

</LinearLayout> 
相關問題