【问题标题】:Show "Loading..." dialog in onCreateView在 onCreateView 中显示“正在加载...”对话框
【发布时间】:2014-11-28 12:48:04
【问题描述】:

我的项目中有这些代码:

@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 中使用进程对话框。 请帮帮我。

【问题讨论】:

    标签: android android-fragments process dialog loading


    【解决方案1】:

    主要有两种方法可以解决。

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

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

    这正是你想要的。

    【讨论】:

    • 我可以访问 getActivity() 但在我的循环完成之前它不会显示任何进程。
    【解决方案2】:

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

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

    否则您的应用会收到 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
     }
    

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

    【讨论】:

    • 我会检查的。谢谢。
    • 我认为它必须工作,但如果我使用 ASyncTask,我如何更新我的 UI?我必须在 ASyncTask 类中使用我的循环?!像这样的东西:stackoverflow.com/questions/14426356/… ???
    【解决方案3】:
    ProgressDialog pDialog;
    pDialog = new ProgressDialog((Main)context);
    pDialog.setMessage("Loading");
    pDialog.show();
    

    【讨论】:

    • 加载后才显示,加载视图后显示。
    • 可能在 onCreate().. 之后记得调用 dialog.dismiss()
    • 这行不通,因为您在 UI 线程中。在 onCreateView() 返回之前,这不会刷新 UI。
    【解决方案4】:

    问题是你在 UIThread 上膨胀了所有这些视图,你可以解决它,但是按照其他帖子的建议在 AsyncTask 中进行。但在我看来,问题是你给它们充气了,2000 个!?

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

    【讨论】:

      【解决方案5】:

      在您的类定义之前全局定义 TableLayout ll..

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

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

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

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

      创建一个扩展 BaseAdapter 的函数 llAdapter。使用它来将所有数据填充到您的表格行中。

      这可能会有所帮助Android: Customize list view, Table view in adapter

      希望这会有所帮助..

      【讨论】:

        【解决方案6】:

        试试这个代码...

                   @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;
                    }
        

        【讨论】:

        • 只需删除 dialog.dismiss();
        • 这行不通,因为您在 UI 线程中。在 onCreateView() 返回之前,这不会刷新 UI。
        • 这是我的代码 @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.mysnipclip_fragment, container, false); ProgressDialog 对话框 = new ProgressDialog(getActivity()); dialog.setMessage("加载中");对话框.show(); System.out.println("onCreateView"); mctx = getActivity();返回 v; }
        • @Rv Panchal:不,它不起作用。是的,如果我删除 dialog.dismiss(); 它会显示对话框;但它在我的循环完成后显示!我需要在循环开始之前显示对话框直到它结束。
        猜你喜欢
        • 1970-01-01
        • 2017-10-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-04-16
        • 1970-01-01
        • 2010-11-10
        • 1970-01-01
        相关资源
        最近更新 更多