2013-03-14 96 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>