在自定义DialogPreference中加载listview之前加载进度条

时间:2013-03-14 12:41:08

标签: android android-layout

问题是进度微调器只在api调用完成后加载。我想只有在我返回视图之后它才能生成。 我不知道如何在进行api调用之前将其添加到Dialog中?

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>

3 个答案:

答案 0 :(得分: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);

                }
            });

}

答案 1 :(得分: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进度条进行检查,看看它是否有效:
    2. 调用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,则必须创建一个Acivity并使其显示为对话框。为此,只需添加

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

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

      注意onCreateDialogView中的大多数代码都可以轻松移植到Activity onCreate()方法

答案 2 :(得分: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>