我有一个片段,根据类别数添加一些动态ui。我需要显示一个进度对话框,并在生成UI时将其关闭。我在类别获取过程的开始和结束以及ui生成结束时调用pd.show()和pd.hide()。仍然我的进度对话框没有被解雇。
@Override
public void onCreate(@Nullable Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setTrackedPageName(R.string.analytics_select_service);
pd = new ProgressDialog(getContext());
pd.setIndeterminate(true);
pd.setMessage("Preparing information..");
pd.setCancelable(false);
pd.show();
}
@Override
public void onAttach(Context context)
{
super.onAttach(context);
App.from(context).inject(this);
categoriesSubscription = null;
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.fragment_service_selection, container, false);
if (view != null)
{
ButterKnife.inject(this, view);
// We're recreating the view - we must be at the top of the scrollview!
isScrolledDown = false;
}
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
if (Categories.hasChildren(category))
{
populateWithCategories(category.getChildren());
}
else
{
categoriesSubscription = categories.list()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.onErrorResumeNext(Observable.empty())
.subscribe(this::populateWithCategories);
}
}
@Override
public void onDetach()
{
super.onDetach();
if (categoriesSubscription != null && !categoriesSubscription.isUnsubscribed())
{
categoriesSubscription.unsubscribe();
}
}
@Override
public String getTitle()
{
return null;
}
@Override
public float getDesiredActionBarElevation()
{
return isScrolledDown ? ActionBarModifyingContent.DEFAULT_ELEVATION : 0;
}
private void populateWithCategories(List<Category> categories)
{
for (int i = 0; i < categories.size(); i++)
{
Category category = categories.get(i);
if (Categories.isKnown(category)
&& Categories.isValid(category))
//&& Categories.hasAllowedGenders(category))
{
addService(category, i);
}
}
getActivity().runOnUiThread(new Runnable() {
public void run() {
pd.dismiss();
pd.hide();
pd.cancel();
}
});
}
我应该在哪里拨打pd.dismiss或pd.hide或pd.cancel?
答案 0 :(得分:1)
由于在主线程上观察到订阅,因此您无需单独在UI线程上运行它。检查一下:
private void populateWithCategories(List<Category> categories)
{
for (int i = 0; i < categories.size(); i++)
{
Category category = categories.get(i);
if (Categories.isKnown(category)
&& Categories.isValid(category))
//&& Categories.hasAllowedGenders(category))
{
addService(category, i);
}
}
pd.dismiss();
}
如果这不能解决问题,请尝试使用处理程序:
private void populateWithCategories(List<Category> categories)
{
for (int i = 0; i < categories.size(); i++)
{
Category category = categories.get(i);
if (Categories.isKnown(category)
&& Categories.isValid(category))
//&& Categories.hasAllowedGenders(category))
{
addService(category, i);
}
}
new Handler().post(new Runnable(){
@Override
public void run(){
pd.dismiss();
}
});
}
希望这有帮助。
答案 1 :(得分:0)
private void populateWithCategories(List<Category> categories)
{
dismiss();
for (int i = 0; i < categories.size(); i++)
{
Category category = categories.get(i);
if (Categories.isKnown(category)
&& Categories.isValid(category))
//&& Categories.hasAllowedGenders(category))
{
addService(category, i);
}
}
答案 2 :(得分:0)
这对我有用:
@Override
public float getDesiredActionBarElevation()
{
return isScrolledDown ? ActionBarModifyingContent.DEFAULT_ELEVATION : 0;
}
private void populateWithCategories(List<Category> categories)
{
for (int i = 0; i < categories.size(); i++)
{
Category category = categories.get(i);
if (Categories.isKnown(category)
&& Categories.isValid(category))
//&& Categories.hasAllowedGenders(category))
{
addService(category, i);
}
}
pd.dismiss();
pd = null;