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

nxagd54h  于 2022-11-20  发布在  Android
关注(0)|答案(4)|浏览(151)

我开发了一个基于碎片的应用程序。
我有一个带按钮的菜单片段,这些按钮每打开一个新的片段,替换上一个。
问题是,一些片段需要一段时间才能打开,因为它会调用一些asynctasks并填充一些listview。
因此,当我按下菜单片段中的按钮时,它会保持冻结2秒,直到新的片段取代菜单片段出现。
我希望在该时间出现一个微调器或“加载..”对话框。
我已经测试过了

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

这段代码显示了对话框,但当屏幕冻结时它从不显示。
我应该把代码放在哪里?在包含所有片段的Activity中,还是在菜单片段中?或者可能在加载的片段中?
我不能完成这个任务,所以当我在菜单片段中按下按钮时,我会执行以下代码。

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

可能是因为我在新片段中调用了所有asynctasks和操作来填充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;
        }

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

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

j2cgzkjk1#

我建议您在布局中放置一个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" />

然后在你的片段里:

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请求后:

mProgressBar.setVisibility(View.GONE);
t98cgbkg

t98cgbkg2#

private ProgressDialog mProgressDialog;

mProgressDialog = new ProgressDialog(this);

mProgressDialog.setMessage("Working ...");

private void doSomeWorkMethod() {

mProgressDialog.show();

doSomeWork . . .

mProgressDialog.dismiss();

Snackbar.make(v, "Work Complete.", Snackbar.LENGTH_LONG)
  .setAction("Action", null).show();
}
n9vozmp4

n9vozmp43#

如果您按照以下步骤操作,则UI不会冻结:
1.载入新片段
1.启动异步任务
1.在AsyncTask的构造函数中初始化要加载数据的Progressdialog
1.在您的AsyncTask中显示Progressdialogdialog.show()
1.使用dialog.dismiss()在您的AsyncTask中解除onPostExecute()中的Progressdialog
1.更新UI /将新数据设置到适配器等。

fcg9iug3

fcg9iug34#

您可能正在执行阻塞UI线程的长时间运行的任务。首先您必须切换到Kotlin,
1.创建视图模型
1.在视图模型中添加一个返回流的函数
fun doTask()= flow {瓦尔result = withContext(Dispatchers.IO){ //在此处执行您的任务。//下面是示例代码val myResult = 1 + 1 myResult } emit(result)}
1.在fragment中,创建一个类似于
如果您有任何问题,请联系我们。如果您有问题,请联系我们。
}

相关问题