2017-05-03 11 views
0

私はフラグメントベースのアプリケーションを開発しました。フラグメントロード中にローディングスピナーを表示

私はボタン付きのメニューフラグメントを持っています。これらのボタンは、それぞれ最後のものを置き換えて、それぞれ新しいフラグメントを開きます。

問題は、いくつかのフラグメントがasynctasksの呼び出しをいくつか行い、いくつかのlistviewsを読み込むために、オープンに時間がかかることです。

メニューフラグメントのボタンを押すと、メニューフラグメントの代わりに新しいフラグメントが現れるまで2秒間フリーズします。

私はその時点でスピナーまたは「読み込み中...」ダイアログが表示されることを希望します。

私はこの

 private progressDialog progressDialog; 
     progressDialog = new ProgressDialog(this); 
     progressDialog.setIndeterminate(true); 
     progressDialog.setMessage("Loading..."); 
     progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); 
     progressDialog.show(); 

このコードをテストしているが、ダイアログを表示しますが、画面がフリーズしたとき、それはそれを示したことがありません。

ここにコードを挿入する必要がありますか?すべてのフラグメントを含むアクティビティ、またはメニューフラグメント内にありますか?またはロードされているフラグメントにありますか?

私のメニューのフラグメントでは、私は次のコードを行うボタンを押してこれを達成することはできません。

  NewAppointmentFragment fragment = new NewAppointmentFragment(); 
       android.support.v4.app.FragmentTransaction fragmentTransaction = 

     getActivity().getSupportFragmentManager().beginTransaction(); 
       fragmentTransaction.replace(R.id.fragment_container, fragment, 
     "NewAppointmentFragment"); 
       fragmentTransaction.addToBackStack(null); 
       fragmentTransaction.commit(); 

が、それは私がすべての呼び出し新しいフラグメントで原因である可能性があります

を押したボタンとメニューの断片を見ることができるこの新しいフラグメントがロードされるまで2秒かかり、 凍結2秒を表示されますOnCreateViewでlistviewsを設定するasynctasksと操作?

どうすればこの問題を解決できますか?事前に

おかげ

マイメニューフラグメント

 @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.fragment_main, container, false); 


     nextAppointmentsButton = (Button) 
     rootView.findViewById(R.id.nextAppointmentsButton); 
     nuevaCitaButton = (Button) rootView.findViewById(R.id.nuevaCitaButton); 
     nearbyPharmaciesButton = (Button) 
     rootView.findViewById(R.id.nearbyPharmaciesButton); 
     ourLocationButton = (Button) rootView.findViewById(R.id.ourLocationButton); 

     nextAppointmentsButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       UpcomingAppointmentsFragment fragment = new 
     UpcomingAppointmentsFragment(); 
       android.support.v4.app.FragmentTransaction fragmentTransaction = 

      getActivity().getSupportFragmentManager().beginTransaction(); 
       fragmentTransaction.replace(R.id.fragment_container, fragment); 
       fragmentTransaction.addToBackStack(null); 
       fragmentTransaction.commit(); 

      } 

     }); 

     nuevaCitaButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       ((MainActivity)getActivity()).showProgressDialog(); 
       NewAppointmentFragment fragment = new NewAppointmentFragment(); 
       android.support.v4.app.FragmentTransaction fragmentTransaction = 

     getActivity().getSupportFragmentManager().beginTransaction(); 
       fragmentTransaction.replace(R.id.fragment_container, fragment, 
     "NewAppointmentFragment"); 
       fragmentTransaction.addToBackStack(null); 
       fragmentTransaction.commit(); 
      } 

     }); 

     nearbyPharmaciesButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       NearbyPharmaciesFragment fragment = new NearbyPharmaciesFragment(); 
       android.support.v4.app.FragmentTransaction fragmentTransaction = 

     getActivity().getSupportFragmentManager().beginTransaction(); 
       fragmentTransaction.replace(R.id.fragment_container, fragment); 
       fragmentTransaction.addToBackStack(null); 
       fragmentTransaction.commit(); 

      } 

     }); 
     ourLocationButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       OurLocationMapFragment fragment = new OurLocationMapFragment(); 
       android.support.v4.app.FragmentTransaction fragmentTransaction = 

     getActivity().getSupportFragmentManager().beginTransaction(); 
       fragmentTransaction.replace(R.id.fragment_container, fragment); 
       fragmentTransaction.addToBackStack(null); 
       fragmentTransaction.commit(); 
      } 

     }); 


     // Inflate the layout for this fragment 
     return rootView; 
     } 

私の新しい断片がロードされた私は、メニューボタン

 public View onCreateView(LayoutInflater inflater, ViewGroup container, 
        Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.fragment_new_appointment, 
     container, false); 

     // **Calls to asynctasks ** 
     // **Populate operations ListViews** 

     return rootView; 
     } 

答えて

0
private ProgressDialog mProgressDialog; 

mProgressDialog = new ProgressDialog(this); 

mProgressDialog.setMessage("Working ..."); 

private void doSomeWorkMethod() { 

mProgressDialog.show(); 

doSomeWork . . . 

mProgressDialog.dismiss(); 

Snackbar.make(v, "Work Complete.", Snackbar.LENGTH_LONG) 
    .setAction("Action", null).show(); 
} 
+0

「ProgressDialog」は正式にAndroidチームによって評価されました。アクティビティ内での 'ProgressBar'(または同様のもの)の実装が推奨されています。 – anthony

0

を押したときに、私はあなたの中のProgressBarウィジェットを置くことを示唆していますあなたのレイアウト:

あなたはhttpリクエストを終了したら

ProgressBar mProgressBar; 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
View view = inflater.inflate(R.layout.fragment, container, false); 
mProgressBar = (ProgressBar) view.findViewById(R.id.progress_bar); 
//other references 
return view; 
} 

は::

<ProgressBar 
    android:id="@+id/progress_bar" 
    style="@style/Base.Widget.AppCompat.ProgressBar" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center" 
    android:indeterminate="true" 
    android:visibility="visible" /> 

次に、あなたのフラグメントで次の手順を実行する場合は

mProgressBar.setVisibility(View.GONE); 
0

はあなたのUIは凍結しないでください。

  1. ロード新しいフラグメント
  2. 非同期タスクを開始
  3. あなたはUIが設定/ dialog.dismiss()
  4. アップデートであなたのAsyncTaskonPostExecute()Progressdialogを閉じdialog.show()
  5. であなたのAsyncTaskonPreExecute()にデータ
  6. ショーProgressdialogをロードしたいAsyncTaskのコンストラクタで初期化Progressdialogあなたのアダプタなどに新しいデータを入力してください。
関連する問題