android - 在 onCreateView 中显示 "Loading..."对话框

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

最佳答案

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

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

  2. 如果您无法通过 onCreateView 方法访问 getActivity,则将您的 View 变量设置为全局变量并使用其上下文,例如,如果您的 查看 itemView。 itemView.getContext()

这正是您想要的。

关于android - 在 onCreateView 中显示 "Loading..."对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27189024/

相关文章:

android - Fragment 和 Activity 之间的通信

c# - 可以通过重用进程更快地完成 Python 脚本的 C# 执行吗?

c - 信号处理程序不处理信号

android - NoSuchMethodError getChildFragmentManager()

shell - 在 shell 脚本中杀死后台进程

安卓约束布局 : How to add a dynamic view one below another

android - 带有 CoordinatorLayout 的 NestedScrollView 内的 Recycler View

安卓 : Access Mapview Inside Fragment With ViewPager

java - latlng 不能为空 - 自动完成中需要一个位置

android - 按钮 setOnClickListener 不起作用