我开发了一个基于片段的应用程序。
我有一个带按钮的菜单片段,这些按钮每个按钮打开一个新片段,替换最后一个片段。
问题是,某些片段在开放时需要一段时间,因为它会对asynctasks进行一些调用并填充一些列表视图。
因此,当我按下菜单片段中的按钮时,它会一直冻结2秒,直到新片段出现,取代菜单片段。
我想要一个微调器或者#34;装载......"对话框出现在那个时间。
我已经测试了这个
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秒,你可以看到按下按钮的菜单片段
可能是新片段中的原因我调用了所有的asynctasks和操作来填充OnCreateView中的listviews吗?
我该如何解决这个问题?
提前致谢
我的菜单片段
@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;
}
答案 0 :(得分:0)
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();
}
答案 1 :(得分:0)
我建议您在布局中放置一个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);
答案 2 :(得分:0)
如果您按照这些步骤操作,则不应冻结用户界面:
Progressdialog
构造函数中初始化AsyncTask
Progressdialog
onPreExecute()
的{{1}}中显示AsyncTask
dialog.show()
Progressdialog
onPostExecute()
中的AsyncTask