在加载片段时显示加载微调器 [英] Show a loading spinner while a fragment is loading

查看:69
本文介绍了在加载片段时显示加载微调器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have developed a fragment-based app.

我有一个带有按钮的菜单片段,这些按钮每个都打开一个新片段,以替换最后一个片段.

I have a menu fragment with buttons,these buttons open a new fragment each one, replacing the last one.

问题是,某些片段在打开时会花费一些时间,因为它会调用asynctasks并填充一些列表视图.

The issue is, that some fragment takes a while in opening cause it does some calls to asynctasks and populates some listviews.

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

So when I press the button in the menu fragment, it keeps freezed 2 seconds until the new fragment appears replacing the menu fragment.

我希望那时出现一个微调器或正在加载..."对话框.

I would like that a spinner or "loading.." dialog appears in that time.

我已经测试过

        private progressDialog progressDialog;
        progressDialog = new ProgressDialog(this);
        progressDialog.setIndeterminate(true);
        progressDialog.setMessage("Loading...");
        progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progressDialog.show();

此代码显示对话框,但是在冻结屏幕时从不显示该对话框.

This code show the dialog but it never shows it when the screen is freezed.

我应该在哪里放置该代码?在包含所有片段的活动中还是在菜单片段中?还是在加载的片段中?

Where I should place that code? in the activity which contains all fragments or in the menu fragment? or maybe in the fragment which is loaded?

我没有做到这一点,所以当我在菜单片段中按下按钮时,我将执行以下代码.

I dont get to accomplish this so when in my menu fragment I press the button I do the following code.

         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 秒您可以看到按下按钮的菜单片段

but it takes 2 seconds until this new fragment is loaded and appears The freezed 2 seconds you can see the menu fragment with the button pressed

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

我该如何解决此问题?

预先感谢

我的菜单片段

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

当我按下菜单按钮时,我的新片段已加载

My new Fragment loaded when I press the menu button

      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小部件:

I suggest that you put a ProgressBar widget inside your layout:

<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" />

然后在您的片段中

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

当您完成http请求时:

When you finish your http request:

mProgressBar.setVisibility(View.GONE);

这篇关于在加载片段时显示加载微调器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆