Alertdialog 运行 正在进行中不会关闭或取消
Alertdialog running ongoing dosen't dismiss or cancel
根据这个 answer ,我想在加载下 10 个帖子之前显示 dilaog,所以我创建了静态 alertDialog 方法以在我的应用程序的不同位置使用它,但问题是对话框没有' t 取消或取消
setProgressDialog 在 Utils class
public static AlertDialog setProgressDialog(Context context) {
int llPadding = 30;
LinearLayout ll = new LinearLayout(context);
ll.setOrientation(LinearLayout.HORIZONTAL);
ll.setPadding(llPadding, llPadding, llPadding, llPadding);
ll.setGravity(Gravity.CENTER);
LinearLayout.LayoutParams llParam = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
llParam.gravity = Gravity.CENTER;
ll.setLayoutParams(llParam);
ProgressBar progressBar = new ProgressBar(context);
progressBar.setIndeterminate(true);
progressBar.setPadding(0, 0, llPadding, 0);
progressBar.setLayoutParams(llParam);
llParam = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
llParam.gravity = Gravity.CENTER;
TextView tvText = new TextView(context);
tvText.setText("Loading ...");
tvText.setTextColor(context.getResources().getColor(R.color.black));
tvText.setTextSize(20);
tvText.setLayoutParams(llParam);
ll.addView(progressBar);
ll.addView(tvText);
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setCancelable(false);
builder.setView(ll);
AlertDialog dialog = builder.create();
dialog.show();
Window window = dialog.getWindow();
if (window != null) {
WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
layoutParams.copyFrom(dialog.getWindow().getAttributes());
layoutParams.width = LinearLayout.LayoutParams.WRAP_CONTENT;
layoutParams.height = LinearLayout.LayoutParams.WRAP_CONTENT;
dialog.getWindow().setAttributes(layoutParams);
}
return dialog;
}
然后我在 HomeFragment 中使用它点击加载更多按钮
binding.loadMoreBtn.setOnClickListener(view -> {
Utils.setProgressDialog(requireContext());
if (Utils.hasNetworkAccess(requireContext())) {
postViewModel.getPosts();
// Log.w(TAG, "loadMoreBtn: "+dialog.isShowing() );
} else {
postViewModel.getAllItemsFromDataBase.getValue();
}
Utils.setProgressDialog(requireContext()).cancel();
Utils.setProgressDialog(requireContext()).dismiss();
});
PS: 我也尝试创建对话框 AlertDialog dialog = Utils.setProgressDialog(requireContext());
而不是直接调用方法 dialod.show()
,然后 getPosts
取消或关闭它但它没有出现
binding.loadMoreBtn.setOnClickListener(view -> {
Utils.setProgressDialog(requireContext());
if (Utils.hasNetworkAccess(requireContext())) {
postViewModel.getPosts(); // Log.w(TAG, "loadMoreBtn: "+dialog.isShowing() );
} else {
postViewModel.getAllItemsFromDataBase.getValue();
}
Utils.setProgressDialog(requireContext()).cancel();
Utils.setProgressDialog(requireContext()).dismiss();
});
这里你多次调用了静态方法Utils.setProgressDialog()
;每次调用它时,它 returns 都会返回一个全新的对话框,因此您可以关闭未显示在屏幕上的对话框。
相反,您需要将第一次调用该方法返回的对话框存储到一个变量中:
AlertDialog dialog = Utils.setProgressDialog(requireContext());
然后在该对话框版本上调用 dismiss
:dialog.dismiss()
或 dialog.cancel()
每当您需要关闭它时。
我通过在视图模型中添加一个标志布尔值来检测加载状态来修复它
public final MutableLiveData<Boolean> isLoading = new MutableLiveData<>();
并像这样在 getPosts()
中使用它
public void getPosts() {
Log.e(TAG, finalURL.getValue());
isLoading.setValue(true);
repository.remoteDataSource.getPostList(finalURL.getValue())
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<Response<PostList>>() {
@Override
public void onSubscribe(@NonNull Disposable d) {
}
@Override
public void onNext(@NonNull Response<PostList> postListResponse) {
if (postListResponse.isSuccessful()) {
if (postListResponse.body() != null
&& postListResponse.body().getNextPageToken() != null) {
Log.e(TAG, postListResponse.body().getNextPageToken());
token.setValue(postListResponse.body().getNextPageToken());
isLoading.setValue(false);
}
postListMutableLiveData.setValue(postListResponse.body());
最后在点击按钮的片段中
binding.loadMoreBtn.setOnClickListener(view -> {
AlertDialog dialog = Utils.setProgressDialog(requireContext());
postViewModel.isLoading.observe(getViewLifecycleOwner(), isLoading -> {
if (isLoading) {
dialog.show();
} else {
dialog.dismiss();
}
});
if (Utils.hasNetworkAccess(requireContext())) {
postViewModel.getPosts();
Log.w(TAG, "loadMoreBtn: " + dialog.isShowing());
} else {
postViewModel.isLoading.postValue(true);
postViewModel.getAllItemsFromDataBase.getValue();
postViewModel.isLoading.postValue(false);
}
});
根据这个 answer ,我想在加载下 10 个帖子之前显示 dilaog,所以我创建了静态 alertDialog 方法以在我的应用程序的不同位置使用它,但问题是对话框没有' t 取消或取消
setProgressDialog 在 Utils class
public static AlertDialog setProgressDialog(Context context) {
int llPadding = 30;
LinearLayout ll = new LinearLayout(context);
ll.setOrientation(LinearLayout.HORIZONTAL);
ll.setPadding(llPadding, llPadding, llPadding, llPadding);
ll.setGravity(Gravity.CENTER);
LinearLayout.LayoutParams llParam = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
llParam.gravity = Gravity.CENTER;
ll.setLayoutParams(llParam);
ProgressBar progressBar = new ProgressBar(context);
progressBar.setIndeterminate(true);
progressBar.setPadding(0, 0, llPadding, 0);
progressBar.setLayoutParams(llParam);
llParam = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
llParam.gravity = Gravity.CENTER;
TextView tvText = new TextView(context);
tvText.setText("Loading ...");
tvText.setTextColor(context.getResources().getColor(R.color.black));
tvText.setTextSize(20);
tvText.setLayoutParams(llParam);
ll.addView(progressBar);
ll.addView(tvText);
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setCancelable(false);
builder.setView(ll);
AlertDialog dialog = builder.create();
dialog.show();
Window window = dialog.getWindow();
if (window != null) {
WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
layoutParams.copyFrom(dialog.getWindow().getAttributes());
layoutParams.width = LinearLayout.LayoutParams.WRAP_CONTENT;
layoutParams.height = LinearLayout.LayoutParams.WRAP_CONTENT;
dialog.getWindow().setAttributes(layoutParams);
}
return dialog;
}
然后我在 HomeFragment 中使用它点击加载更多按钮
binding.loadMoreBtn.setOnClickListener(view -> {
Utils.setProgressDialog(requireContext());
if (Utils.hasNetworkAccess(requireContext())) {
postViewModel.getPosts();
// Log.w(TAG, "loadMoreBtn: "+dialog.isShowing() );
} else {
postViewModel.getAllItemsFromDataBase.getValue();
}
Utils.setProgressDialog(requireContext()).cancel();
Utils.setProgressDialog(requireContext()).dismiss();
});
PS: 我也尝试创建对话框 AlertDialog dialog = Utils.setProgressDialog(requireContext());
而不是直接调用方法 dialod.show()
,然后 getPosts
取消或关闭它但它没有出现
binding.loadMoreBtn.setOnClickListener(view -> { Utils.setProgressDialog(requireContext()); if (Utils.hasNetworkAccess(requireContext())) { postViewModel.getPosts(); // Log.w(TAG, "loadMoreBtn: "+dialog.isShowing() ); } else { postViewModel.getAllItemsFromDataBase.getValue(); } Utils.setProgressDialog(requireContext()).cancel(); Utils.setProgressDialog(requireContext()).dismiss(); });
这里你多次调用了静态方法Utils.setProgressDialog()
;每次调用它时,它 returns 都会返回一个全新的对话框,因此您可以关闭未显示在屏幕上的对话框。
相反,您需要将第一次调用该方法返回的对话框存储到一个变量中:
AlertDialog dialog = Utils.setProgressDialog(requireContext());
然后在该对话框版本上调用 dismiss
:dialog.dismiss()
或 dialog.cancel()
每当您需要关闭它时。
我通过在视图模型中添加一个标志布尔值来检测加载状态来修复它
public final MutableLiveData<Boolean> isLoading = new MutableLiveData<>();
并像这样在 getPosts()
中使用它
public void getPosts() {
Log.e(TAG, finalURL.getValue());
isLoading.setValue(true);
repository.remoteDataSource.getPostList(finalURL.getValue())
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<Response<PostList>>() {
@Override
public void onSubscribe(@NonNull Disposable d) {
}
@Override
public void onNext(@NonNull Response<PostList> postListResponse) {
if (postListResponse.isSuccessful()) {
if (postListResponse.body() != null
&& postListResponse.body().getNextPageToken() != null) {
Log.e(TAG, postListResponse.body().getNextPageToken());
token.setValue(postListResponse.body().getNextPageToken());
isLoading.setValue(false);
}
postListMutableLiveData.setValue(postListResponse.body());
最后在点击按钮的片段中
binding.loadMoreBtn.setOnClickListener(view -> {
AlertDialog dialog = Utils.setProgressDialog(requireContext());
postViewModel.isLoading.observe(getViewLifecycleOwner(), isLoading -> {
if (isLoading) {
dialog.show();
} else {
dialog.dismiss();
}
});
if (Utils.hasNetworkAccess(requireContext())) {
postViewModel.getPosts();
Log.w(TAG, "loadMoreBtn: " + dialog.isShowing());
} else {
postViewModel.isLoading.postValue(true);
postViewModel.getAllItemsFromDataBase.getValue();
postViewModel.isLoading.postValue(false);
}
});