2015-06-12 116 views
0

我有一些tabpages和片段。這些片段包含一個元素。在獲取圖像緩存和填充gridviews自定義網格後,我的程序將進入web服務並讀取json數據。如何在Android碎片中顯示ProgressDialog?

現在,怎樣才能顯示ProgressDialog只完成碎片?我怎麼能解僱progressDialog GridView填充將完成?

My Fragment OnCreate Method;

public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     // Inflate the layout for this fragment 
     View v = inflater.inflate(R.layout.fragment_b, container, false); 
     context = getActivity(); 
     final ProgressDialog dialog = ProgressDialog.show(context, "", "Please wait, Loading Page...", true); 
     GridView gridView = (GridView) v.findViewById(R.id.gridview); 
     if (isConnected()) { 
      try { 
       new ProgressTask(context).execute(); 
       gridView.setAdapter(new AdapterB(context, WallPaperList)); 
      } catch (NullPointerException e) { 
       e.printStackTrace(); 
       return v; 
      } 
     } else { 
      Toast.makeText(v.getContext(), "Please check your internet connection..!", Toast.LENGTH_LONG).show(); 
     } 

     return v; 
    } 
+0

最好的辦法是使用'的AsyncTask ' –

+0

可能可以使用接口 –

回答

0

在的AsyncTask,create progress dialogonPreExecute()dismissonPostExecute()進度對話框。

更新:

在viewpager活動添加該代碼。

viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { 
     @Override 
     public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { 

     } 

     @Override 
     public void onPageSelected(int position) { 
      //You can get Call back to show the progress dialog when swipping. 
      boolean isLoading = true;//Check if the fragment is still loading or not at given position. 
      if (isLoading) { 
       //Show progress dialog. 
      } 
     } 

     @Override 
     public void onPageScrollStateChanged(int state) { 
     } 
}); 
+0

Y不能評論?如果回答Y不是一個合適的編碼答案 –

+0

accualy我嘗試它,但無法正常工作。 ProgressDialog首先顯示,當解散和滑動頁面到片段時,我什麼都看不到。等待並填充所有圖像突然顯示給我。 –

+0

解除第一個片段中的對話框並滑動到下一個片段之後,現在是否要顯示進度對話框? –

0

其顯示進度的簡單的例子可以在此幫助您

private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> { 

     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      progressDialog = ProgressDialog.show(DownloadImageActivity.this, "Wait", "Downloading..."); 
     } 

     @Override 
     protected Bitmap doInBackground(String... params) { 
      //your downloading code 
     } 

     /** 
     * After completing background task Dismiss the progress dialog 
     * **/ 
     protected void onPostExecute(Bitmap bitmap) { 
      progressDialog.dismiss(); 


      } 
     } 
+0

accualy我嘗試它,但無法正常工作。 ProgressDialog首先顯示,當解散和滑動頁面到片段時,我什麼都看不到。等待並填充所有圖像突然顯示給我。 –

+0

,因爲您在異步任務完成後設置適配器,請嘗試在'onPostexecute()'方法中設置適配器 – SAM

0

入住這...........

public interface AsyncTaskListener<Boolean> { 
    public void onCompletingTask(Boolean result); 
} 

public class AsyncTaskGetApiKey extends AsyncTask<Void, Void, Boolean>{ 

    ArrayList<AsyncTaskListener<Boolean>> listeners; 

    public AsyncTaskLoadingInSplash() { 
     listeners = new ArrayList<AsyncTaskListener<Boolean>>(); 
    } 

    public void addListener(AsyncTaskListener<Boolean> listener){ 
     listeners.add(listener); 
    } 

    public void removeListener(AsyncTaskListener<Boolean> listener){ 
     listeners.remove(listener); 
    } 


    @Override 
    protected Boolean doInBackground(Void... params) { 

     //do your activities here 

     return true; // If everything is fine 
    } 

    @Override 
    protected void onPostExecute(Boolean result) { 
     // TODO Auto-generated method stub 
     super.onPostExecute(result); 
     for (AsyncTaskListener<Boolean> listener:listeners) { 
      listener.onReceivingApiKey(result); 
     } 
    } 
} 

public class MyFagemnt extends Fragment 
implements AsyncTaskListener<Boolean>{ 

    AsyncTaskGetApiKey task; 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 

     //...... 

     task = new AsyncTaskGetApiKey(); 
     task.addListener(this); 
     //........ 
    } 


    @Override 
    public void onCompletingTask(Boolean result) { 
     // your asyntask finished 
     // now close the progress bar 
    } 
}