2014-09-02 52 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

(因为我会加载新闻时需要解雇进度)