片段交易的进度对话框

时间:2015-01-26 02:52:32

标签: android android-fragments progressdialog

我无法显示进行大量工作的片段事务的进度对话框。我试图实现这一目标的当前方式是:

主要活动onCreate:

protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 Fragment myFragment = new MyFragment();
 fragmentTransaction.add(R.id.container,myFragment).commit();
}

在我的片段类中:

public class MyFragment extends Fragment {

public MyFragment() {
}

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

@Override
public void onStart() {
   super.onStart();
   ProgressDialog mProgressDialog = ProgressDialog.show(getActivity(), "title", "msg");
   mProgressDialog.setCancelable(true);
   mProgressDialog.show();

   // Time taking code 

   mProgressDialog.dismiss();
}

这只是在任务完成之前显示黑屏。

2 个答案:

答案 0 :(得分:0)

应该在后台线程上完成时间工作。你可以这样做:

@Override  
public void onStart() {
        super.onStart();
        final ProgressDialog progressDialog = ProgressDialog.show(getActivity(), "title", "msg");
        progressDialog.setCancelable(true);
        progressDialog.show();

        final Handler handler = new Handler();
        new Thread(){

            @Override
            public void run() {

                // TODO: your time taking code runs here.

                handler.post(new Runnable() {

                    @Override
                    public void run() {
                        // close the progress dialog.
                        progressDialog.dismiss();                           
                    }
                });
            }

        }.start();    
    }

答案 1 :(得分:0)

您可以使用AsyncTask执行耗时的工作,并在onPreExecute中显示对话框,将其隐藏在onPostExcute中。