2014-09-02 58 views
2

我想在我的類被調用時實現progessDialog。我知道如何爲使用AsyncTask的片段類實現progressDialog。但是在這裏我有一個不使用asyncTask的片段類。實現progressDialog

這個片段的功能是在調用它時顯示一些新聞細節。我想在新聞加載時顯示progressDialog,並在新聞加載並準備顯示時停止progressDialog。如何才能做到這一點?我是個新手,所以請大家幫我

我的片段類

package com.fortuna.cinemalk; 

public class NewsDetailFragment extends Fragment { 

    private ViewPager view1; 
    private ViewPageAdapter adapter; 
    private Activity activity; 
    private CommonVariable commonVariable; 


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

     View view = inflater.inflate(R.layout.newsdetail_fragment, container, 
       false); 

     activity = this.getActivity(); 

     commonVariable = (CommonVariable) activity.getApplication(); 

     view1 = (ViewPager) view.findViewById(R.id.listviewpager); 

     adapter = new ViewPageAdapter(commonVariable.getNewsDescription(), 
       Element.NEWS_DETAIL.getType(), activity); 

     view1.setAdapter(adapter); 



     return view; 
    } 

} 
+0

數據如何加載新聞。u使用和Webservie加載新 – 2014-09-02 09:23:26

回答

1

都需要這樣的:

enter image description here

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:tools="http://schemas.android.com/tools" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <ProgressBar 
     android:id="@+id/progressBar1" 
     style="?android:attr/progressBarStyleLarge" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:layout_centerVertical="true" 
     android:indeterminate="true" /> 

     </RelativeLayout> 

就隱藏這個進度,一旦加載喜歡

 progressBarObj.setVisibility(false); 
+0

什麼是progresBarObj在這裏? – CraZyDroiD 2014-09-03 04:21:15

1

爲我創造它修改proggress組件自定義處理程序的最佳選擇:

public final class ProgressBarComponentViewHandler extends Handler { 

    private ProgressBar progressLayout; 

    private Activity mActivity; 

    public LoadingComponentViewHandler(Activity activity, ProgressBar progressLayout) { 
     this.progressLayout = progressLayout; 
     this.mActivity=activity 
    } 

    public void update(int status){ 
     Message m = new Message(); 
     Bundle bundle = new Bundle(); 
     bundle.putInt("status", status); 
     m.setData(bundle); 
     super.sendMessage(m); 
    } 

    public void handleMessage(Message msg) { 
     Bundle b = msg.getData(); 
     Integer status = b.getInt("status", 0); 
     progressLayout.setProgress(status); 
    } 
} 

而在你的片段:

loadingComponentView = (ProgressBar) yourRootView.findViewById(R.id.progress_bar_component_view); 

progressBarDialogHandler = new ProgressBarViewHandler(getActivity(), loadingComponentView); 

最後,要更新您的視圖:

progressBarDialogHandler.update(80);//To set 80% of progressbar. 
0

您可以從您的AsyncTask或服務 顯示ProgressDialog,或者你可以像靜態片段初始化ProgressDialog並從您的AsyncTask或服務呼叫對話,但你需要檢查progressDialog!= NULL

(因爲我會加載新聞時需要解僱進度)