2017-05-03 2 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'는 공식적으로 안드로이드 팀에 의해 평가되었습니다. 액티비티 내에서'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. 초기화하면 dialog.dismiss()
  4. 업데이트로 AsyncTask에 해제를 dialog.show()
  5. AsyncTask의 UI를 onPostExecute()에서 Progressdialog
  6. onPreExecute()에서 Progressdialog보기 데이터를로드 할 AsyncTask의 생성자 Progressdialog 설정/귀하의 어댑터 등에 새 데이터를 입력하십시오.
관련 문제