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

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>
4

3 に答える 3

0

私はあなたに2つの提案があります:

  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. また、上記が機能しない場合は、以下のプログレスバーを表示する方法を試して、機能するかどうかを確認してください。

    ダイアログボックスのインスタンスを作成する場所から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;
     }
    

また、ダイアログボックス内にプログレスバーを埋め込みたい場合は、Acivityを作成し、それをダイアログとして表示する必要があります。それに単に追加します

以下Androidドキュメントページ(http://developer.android.com/guide/topics/ui/dialogs.html)からコピー:

ヒント:カスタムダイアログが必要な場合は、Dialog APIを使用する代わりに、アクティビティをダイアログとして表示できます。アクティビティを作成し、マニフェスト要素でそのテーマをTheme.Holo.Dialogに設定するだけです。

注onCreateDialogViewのほとんどのコードは、Activity onCreate()メソッドに移植できるので非常に簡単です。

于 2013-03-14T14:02:52.707 に答える
0

これは実際、私のレイアウトでは非常に単純な問題でした

cレイアウトを:worksに変更する

<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

これが機能するかどうかはわかりませんが、最初にダイアログを作成してから内容を変更してみてください!

これは醜く、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 に答える