Android 加载片段时显示加载微调器

Android 加载片段时显示加载微调器,android,android-fragments,loading,Android,Android Fragments,Loading,我开发了一个基于片段的应用程序 我有一个带有按钮的菜单片段,这些按钮每一个都会打开一个新片段,替换上一个 问题是,某些片段在打开时需要一段时间,因为它会调用asynctasks并填充一些ListView 因此,当我按下菜单片段中的按钮时,它会保持冻结状态2秒,直到新片段取代菜单片段出现 我希望此时出现微调器或“加载…”对话框 我已经测试过了 private progressDialog progressDialog; progressDialog = new Pr

我开发了一个基于片段的应用程序

我有一个带有按钮的菜单片段,这些按钮每一个都会打开一个新片段,替换上一个

问题是,某些片段在打开时需要一段时间,因为它会调用asynctasks并填充一些ListView

因此,当我按下菜单片段中的按钮时,它会保持冻结状态2秒,直到新片段取代菜单片段出现

我希望此时出现微调器或“加载…”对话框

我已经测试过了

        private progressDialog progressDialog;
        progressDialog = new ProgressDialog(this);
        progressDialog.setIndeterminate(true);
        progressDialog.setMessage("Loading...");
        progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progressDialog.show();
此代码显示对话框,但在屏幕冻结时从不显示

我应该把代码放在哪里?在包含所有片段的活动中,还是在菜单片段中?或者可能在装载的碎片中

我无法完成这项任务,所以当我在菜单片段中按下按钮时,我执行以下代码

         NewAppointmentFragment fragment = new NewAppointmentFragment();
                android.support.v4.app.FragmentTransaction fragmentTransaction =

        getActivity().getSupportFragmentManager().beginTransaction();
                fragmentTransaction.replace(R.id.fragment_container, fragment, 
        "NewAppointmentFragment");
                fragmentTransaction.addToBackStack(null);
                fragmentTransaction.commit();
但是这个新片段加载并出现需要2秒钟 冻结2秒钟后,按下按钮即可看到菜单片段

可能是因为在新片段中,我调用了所有异步任务和操作来填充OnCreateView中的ListView?

我如何解决这个问题

提前谢谢

我的菜单片段

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


         nextAppointmentsButton = (Button) 
         rootView.findViewById(R.id.nextAppointmentsButton);
         nuevaCitaButton = (Button) rootView.findViewById(R.id.nuevaCitaButton);
         nearbyPharmaciesButton = (Button) 
         rootView.findViewById(R.id.nearbyPharmaciesButton);
         ourLocationButton = (Button) rootView.findViewById(R.id.ourLocationButton);

         nextAppointmentsButton.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                UpcomingAppointmentsFragment fragment = new 
         UpcomingAppointmentsFragment();
                android.support.v4.app.FragmentTransaction fragmentTransaction =

          getActivity().getSupportFragmentManager().beginTransaction();
                fragmentTransaction.replace(R.id.fragment_container, fragment);
                fragmentTransaction.addToBackStack(null);
                fragmentTransaction.commit();

            }

         });

         nuevaCitaButton.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                 ((MainActivity)getActivity()).showProgressDialog();
                 NewAppointmentFragment fragment = new NewAppointmentFragment();
                 android.support.v4.app.FragmentTransaction fragmentTransaction =

         getActivity().getSupportFragmentManager().beginTransaction();
                fragmentTransaction.replace(R.id.fragment_container, fragment, 
         "NewAppointmentFragment");
                fragmentTransaction.addToBackStack(null);
                fragmentTransaction.commit();
            }

        });

        nearbyPharmaciesButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                NearbyPharmaciesFragment fragment = new NearbyPharmaciesFragment();
                android.support.v4.app.FragmentTransaction fragmentTransaction =

        getActivity().getSupportFragmentManager().beginTransaction();
                fragmentTransaction.replace(R.id.fragment_container, fragment);
                fragmentTransaction.addToBackStack(null);
                fragmentTransaction.commit();

            }

        });
        ourLocationButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                OurLocationMapFragment fragment = new OurLocationMapFragment();
                android.support.v4.app.FragmentTransaction fragmentTransaction =

        getActivity().getSupportFragmentManager().beginTransaction();
                fragmentTransaction.replace(R.id.fragment_container, fragment);
                fragmentTransaction.addToBackStack(null);
                fragmentTransaction.commit();
            }

        });


        // Inflate the layout for this fragment
        return rootView;
        }
当我按下菜单按钮时,我的新片段被加载

      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                     Bundle savedInstanceState) {
       View rootView = inflater.inflate(R.layout.fragment_new_appointment, 
        container, false);

      // **Calls to asynctasks **
      // **Populate operations ListViews**

      return rootView;
      }

我建议您在布局中放置ProgressBar小部件:

<ProgressBar
    android:id="@+id/progress_bar"
    style="@style/Base.Widget.AppCompat.ProgressBar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:indeterminate="true"
    android:visibility="visible" />
完成http请求后:

mProgressBar.setVisibility(View.GONE);

如果遵循以下步骤,您的UI不应冻结:

  • 加载新片段
  • 启动异步任务
  • 在要加载数据的
    AsyncTask
    的构造函数中初始化
    Progressdialog
  • 使用
    dialog.Show()
  • 使用
    对话框在
    onPostExecute()
    中关闭
    AsyncTask
    中的
    Progressdialog。关闭()
  • 更新UI/将新数据设置到适配器等

  • ProgressDialog
    已被Android团队正式贬低。现在建议在活动中实现
    ProgressBar
    (或类似功能)。我加载了多个片段,这显示了在一个活动中重叠的所有进度对话框,我可以只将进度绑定到片段吗?显示“调用asynctasks”-听起来你做错了什么,阻塞了主线程。
    ProgressBar mProgressBar;
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
     View view = inflater.inflate(R.layout.fragment, container, false);
     mProgressBar = (ProgressBar) view.findViewById(R.id.progress_bar);
     //other references
     return view;
    }
    
    mProgressBar.setVisibility(View.GONE);