在onCreateView中显示“正在加载...”对话框

时间:2014-11-28 12:48:05

标签: android android-fragments process dialog loading

我的项目中有这些代码:

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

            int intPos = getArguments().getInt(ARG_SECTION_NUMBER);
            View rootView;

            rootView = inflater.inflate(R.layout.fragment_main, container,false);

            TableLayout ll = (TableLayout) rootView.findViewById(R.id.tableLayoutList);
            View mTableRow = null;
            Typeface tf = Typeface.createFromAsset(getActivity().getAssets(), "fonts/Far_DastNevis.otf");

            for (int i =1; i <= 2000; ++i)
            {
                mTableRow = (TableRow) View.inflate(getActivity(), R.layout.mrowrayout, null);
                final TextView txtBody = (TextView)mTableRow.findViewById(R.id.txtItem);
                txtBody.setText("Some Text" + i);
                txtBody.setId(i);
                txtBody.setTypeface(tf);
                mTableRow.setTag(i);
                ll.addView(mTableRow);
            }

            return rootView;
        }

加载需要几秒钟(或几分钟),我想向用户显示进程对话框或类似内容,直到加载为止。 但是我无法在onCreateView中使用进程对话框。 请帮帮我们。

6 个答案:

答案 0 :(得分:1)

您必须使用ASyncTask。有关非常好的教程,请参阅http://www.vogella.com/tutorials/AndroidBackgroundProcessing/article.html#concurrency_asynchtask。我们的想法是,在不同的线程中运行长时间运行的代码。如果它在UI线程中运行,则根本无法更新UI。

或者在您的情况下,Handler可能会提供帮助(相同的链接)。首先让系统打开对话框(通过离开onCreateView()),运行处理程序代码。

否则你的app会收到ANR报告。

整件事情有点棘手,因为你必须更新UI:

将外观置于doInBackground()中并调用publishProgress():

protected Integer doInBackground()
....
for (int i = 0; i < 2000; i++) {
         publishProgress(i);
}

接下来用您的UI代码填充onProgressUpdate(Integer ... progress)函数。

protected void onProgressUpdate(Integer... progress) {
   // update progress dialog here
   for(int i:progress) {
            mTableRow = (TableRow) View.inflate(getActivity(), R.layout.mrowrayout, null);
            final TextView txtBody = (TextView)mTableRow.findViewById(R.id.txtItem);
            txtBody.setText("Some Text" + i);
            txtBody.setId(i);
            txtBody.setTypeface(tf);
            mTableRow.setTag(i);
            ll.addView(mTableRow);
    }
}
protected void onPostExecute() {
     // close progress dialog here
 }
protected void onPreExecute() {
     // open progress dialog here
 }

这只是元代码,您必须根据需要进行修改。您可以在此处更新进度对话框。

答案 1 :(得分:1)

主要有两种解决方法。

  1. getActivity()方法: - 使用上下文或此关键字的getActivity()方法。

  2. 如果you can not access getActivity out of your onCreateView method,则将查看变量设为全局并使用其上下文,例如,如果 View itemView. itemView.getContext()

  3. 这就是你想要的。

答案 2 :(得分:0)

ProgressDialog pDialog;
pDialog = new ProgressDialog((Main)context);
pDialog.setMessage("Loading");
pDialog.show();

答案 3 :(得分:0)

问题是你在UIThread上对所有那些视图进行了膨胀,你可以解决它但是在AsyncTask中这样做就像其他帖子一样。但是我看到的问题是你根本膨胀了它们,其中有2000个!?

我建议使用ListView和Adapter,这样就不需要加载...对话框了,因为根本没有加载。

答案 4 :(得分:0)

在类定义之前全局定义TableLayout ll。

调用AsyncTask来执行占用时间的代码部分。在AsyncTask的onPreExecute中,定义ProgressDialog。您可以通过getActivity()调用获取活动。

在doInBackground中将文本数据填充到全局ArrayList中。

在onPostExecute中,调用这样的适配器......

ll.setAdapter(new llAdapter(getActivity(),yourArrayList));

创建一个扩展BaseAdapter的函数llAdapter。使用此选项将所有数据填充到您的tablerow中。

这可能有助于Android: Customize list view, Table view in adapter

希望这会有所帮助..

答案 5 :(得分:-1)

尝试此代码......

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

                ProgressDialog dialog = new ProgressDialog(getActivity());
                dialog.setMessage("Loading");
                dialog.show();

                int intPos = getArguments().getInt(ARG_SECTION_NUMBER);
                View rootView;

                rootView = inflater.inflate(R.layout.fragment_main, container,false);

                TableLayout ll = (TableLayout) rootView.findViewById(R.id.tableLayoutList);
                View mTableRow = null;
                Typeface tf = Typeface.createFromAsset(getActivity().getAssets(), "fonts/Far_DastNevis.otf");

                for (int i =1; i <= 2000; ++i)
                {
                    mTableRow = (TableRow) View.inflate(getActivity(), R.layout.mrowrayout, null);
                    final TextView txtBody = (TextView)mTableRow.findViewById(R.id.txtItem);
                    txtBody.setText("Some Text" + i);
                    txtBody.setId(i);
                    txtBody.setTypeface(tf);
                    mTableRow.setTag(i);
                    ll.addView(mTableRow);
                }

                return rootView;
            }