0

问题是进度微调器仅在 api 调用完成后加载。我想只有在我返回视图之后才有意义。我不确定如何在进行 api 调用之前和期间将其添加到对话框中?

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

频道内容视图.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>
4

3 回答 3

0

这实际上是我的布局的一个非常简单的问题

c将我的布局更改为:有效

<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>
于 2013-03-15T08:30:38.240 回答
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 the Progress Bar,看看它是否有效:

    调用 showProgressBar() 方法,您将从那里创建对话框的实例,或者如果不可能,则开始 onCreateDialog 方法:

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

此外,如果您想在对话框中嵌入 ProgressBar,则必须创建一个活动并将其显示为对话框。为此只需添加

下面从 Android 文档页面 ( http://developer.android.com/guide/topics/ui/dialogs.html )复制:

提示:如果您想要自定义对话框,则可以将 Activity 显示为对话框,而不是使用对话框 API。只需创建一个活动并将其主题设置为清单元素中的 Theme.Holo.Dialog :

注意 您的 onCreateDialogView 中的大部分代码都非常容易移植到 Activity onCreate() 方法

于 2013-03-14T14:02:52.707 回答
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);

                }
            });

}
于 2013-03-14T13:07:50.117 回答