我个人通过设置/清除标志来为用户锁定/解锁 UI:
//The user cannot interact with the UI
private void disableUserInteraction() {
(getActivity()).getWindow()
.setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,
WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
}
//The user can interact with the UI
private void enableUserInteraction() {
(getActivity()).getWindow()
.clearFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
}
这将阻止用户与屏幕进行交互。
然后,您可以创建一个带有灰色的 RelativeLayout,以使用户明显需要等待,并在执行您的操作时使用它的可见性:
<LinearLayout
android:id="@+id/loading_mask"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#B0000000"
android:visibility="gone"/>
您还可以同时添加一个 ProgressDialog,以显示/关闭:
//The UI message to show the user during the loading
private void showProgressDialog() {
if (progressDialog == null) {
progressDialog = new ProgressDialog(Objects.requireNonNull(getParentFragment())
.getActivity());
progressDialog.setIndeterminate(true);
progressDialog.setCancelable(false);
}
progressDialog.setMessage("Loading...");
progressDialog.show();
}
private void dismissProgressDialog() {
if (progressDialog != null) {
progressDialog.dismiss();
}
}
如果你把这三个结合起来,我认为它是一个很好的等待屏幕,那么你可以像这样把这些方法放在一起:
//The user can interact with the UI, end of progressdialog and waiting mask
private void unlockUI() {
loadingMask.setVisibility(View.GONE);
dismissProgressDialog();
enableUserInteraction();
}
//The user cannot interact with the UI, start of progressdialog and waiting mask
private void lockUI() {
showProgressDialog();
disableUserInteraction();
loadingMask.setVisibility(View.VISIBLE);
}
您只需要在启动时调用 lockUI() 并在用户可以再次与屏幕交互时调用 unlockUI()。
希望这会有所帮助,让我们知道进展如何!