ProgressDialog 嵌套在另一个空白对话框中

发布于 2024-09-10 19:31:55 字数 1266 浏览 5 评论 0原文

替代文本 http://img252.imageshack.us/img252/643/snakesonadialog.png< /a>

大家好,

我正在对一个应用程序进行最后的润色,但整个应用程序仍然存在一个悬而未决的问题。

我在从网络上拉取数据时以 ProgressDialogs 的形式显示加载指示器。由于某种原因,我的 ProgressDialogs 似乎嵌套在另一个空白对话框中。结果很俗气。

我的布局代码如下所示:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="@style/Theme.MyTheme.EpisodeList">
    <ImageView android:src="@drawable/featured"
        android:layout_width="fill_parent"
        android:layout_height="50sp"
        android:layout_gravity="top"
        android:shadowRadius="0"
        android:shadowColor="#FFFFFF"
        android:id="@+id/header_image"
     />
   <ListView android:id="@+id/android:list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
   />
</LinearLayout>

我用来显示 ProgressDialog 的代码如下:

// In my class definition
private ProgressDialog p;

// in my onCreate method
p = new ProgressDialog(EpisodeActivity.this, ProgressDialog.STYLE_SPINNER);
p.setMessage("Loading...");
p.show();

从外观来看,没有什么问题。我还附上一张照片来说明问题。

任何帮助表示赞赏!

alt text http://img252.imageshack.us/img252/643/snakesonadialog.png

Hi folks,

So I'm putting the finishing touches on an application and there's still one outstanding issue with the application as a whole.

I display loading indicators in the form of ProgressDialogs while pulling data down from the web. For some reason or another, my ProgressDialogs are appearing to be nested inside of another blank dialog box. The result is tacky.

My layout code looks like this:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="@style/Theme.MyTheme.EpisodeList">
    <ImageView android:src="@drawable/featured"
        android:layout_width="fill_parent"
        android:layout_height="50sp"
        android:layout_gravity="top"
        android:shadowRadius="0"
        android:shadowColor="#FFFFFF"
        android:id="@+id/header_image"
     />
   <ListView android:id="@+id/android:list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
   />
</LinearLayout>

The code I use to display the ProgressDialog is the following:

// In my class definition
private ProgressDialog p;

// in my onCreate method
p = new ProgressDialog(EpisodeActivity.this, ProgressDialog.STYLE_SPINNER);
p.setMessage("Loading...");
p.show();

From the looks if it, there's nothing that seems amiss. I'm also attaching a photo to illustrate the problem.

Any help is appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱情眠于流年 2024-09-17 19:31:55

经过几天的摸索,我找到了处理 ProgressDialogs 的最佳方法。在 Android 平台上处理简单的 ProgressDialog 肯定过于复杂,但这并不意味着我们有任何借口不使用它们。希望它(以及文档)在未来会变得更好。这是我的最佳实践。

ProgressDialog 在空白对话框中的嵌套问题似乎是在 2.1 或 2.2 系统上运行 2.0 之前的代码的问题。我只能猜测 show() 方法也在调用 super.show(),它在 2.1 和 2.2 上的行为有所不同。

因此,我发现如果您不直接在 ProgressDialog 对象上调用 show(),效果最好。而是使用内置的 showDialog() 和 onCreateDialog() 方法来处理对话框。这是一个代码片段:

private static final int ID_DIALOG_LOADING = 0;

@Override
protected void onCreate(Bundle tedBundy) {
    // Do stuff

    showDialog(ID_DIALOG_LOADING);

    // Do more stuff in a thread
}

@Override
public void run() {
  // Do some stuff in this thread
  handler.sendEmptyMessage(0);
}

private Handler handler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        // Let adapters know the data under them have changed

        try {
            dimissDialog(ArticlesList.ID_DIALOG_LOADING);
            removeDialog(ArticlesList.ID_DIALOG_LOADING);
        } catch (Exception e) {}
    }
}

@Override
protected Dialog onCreateDialog(int id) {
    if (id == ID_DIALOG_LOADING) {
        ProgressDialog loadingDialog = new ProgressDialog(this);
        loadingDialog.setMessage("Loading...");
        loadingDialog.setIndeterminate(true);
        loadingDialog.setCancelable(true);
        return loadingDialog;
    }

    return super.onCreateDialog(id);
}

@Override
protected void onSaveInstanceState(Bundle outState) {
    try {
        Log.i("ArticlesList.onSaveInstanceState", "Chirp chirp");
        dismissDialog(ArticlesList.ID_DIALOG_LOADING);
        removeDialog(ArticlesList.ID_DIALOG_LOADING);
    } catch (Exception e) {

    }

    super.onSaveInstanceState(outState);
}

@Override
protected void onDestroy() {
    try {
        dismissDialog(ArticlesList.ID_DIALOG_LOADING);
        removeDialog(ArticlesList.ID_DIALOG_LOADING);
    } catch (Exception e) {

    }

    super.onDestroy();
}

@Override
protected void onPause() {
    try {
        dismissDialog(ArticlesList.ID_DIALOG_LOADING);
        removeDialog(ArticlesList.ID_DIALOG_LOADING);
    } catch (Exception e) {

    }

    super.onPause();
}

@Override
public void onDetachedFromWindow() {
    try {
        dismissDialog(ArticlesList.ID_DIALOG_LOADING);
            removeDialog(ArticlesList.ID_DIALOG_LOADING);
    } catch (Exception e) {

    }

    super.onDetachedFromWindow();
}

这是一个很长的代码片段。您还会看到一堆重复的代码,它们不断地试图杀死该对话框。这样做是因为每当手机旋转时 Android 都会销毁并重新创建视图。希望在未来的版本中,每当用户旋转屏幕时,窗口不会被完全销毁并重新加载,而是实现 onScreenRotate() 方法。

在尝试了几种不同的方法之后,这个黑客解决方案似乎是唯一能够提供一致、快速的删除 ProgressDialog 结果的方法。希望这可以在将来节省某人几天的时间。

After a few days of futzing around, I figured out the best way to handle ProgressDialogs. Dealing with simple ProgressDialogs is definitely over-complicated on the Android platform, but that doesn't mean we have any excuse to not work with them. Hopefully it will get better (along with the documentation) going into the future. Here are my best practices.

The nesting issue of a ProgressDialog inside a blank Dialog seems to be an issue of pre-2.0 code being run on a 2.1 or 2.2 system. I can only guess that the show() method is also calling super.show(), which behaves differently on 2.1 and 2.2.

So, I've found that it works best if you don't call show() directly on the ProgressDialog objects. Instead, use the built in showDialog() and onCreateDialog() methods to juggle dialogs. Here's a code snippet:

private static final int ID_DIALOG_LOADING = 0;

@Override
protected void onCreate(Bundle tedBundy) {
    // Do stuff

    showDialog(ID_DIALOG_LOADING);

    // Do more stuff in a thread
}

@Override
public void run() {
  // Do some stuff in this thread
  handler.sendEmptyMessage(0);
}

private Handler handler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        // Let adapters know the data under them have changed

        try {
            dimissDialog(ArticlesList.ID_DIALOG_LOADING);
            removeDialog(ArticlesList.ID_DIALOG_LOADING);
        } catch (Exception e) {}
    }
}

@Override
protected Dialog onCreateDialog(int id) {
    if (id == ID_DIALOG_LOADING) {
        ProgressDialog loadingDialog = new ProgressDialog(this);
        loadingDialog.setMessage("Loading...");
        loadingDialog.setIndeterminate(true);
        loadingDialog.setCancelable(true);
        return loadingDialog;
    }

    return super.onCreateDialog(id);
}

@Override
protected void onSaveInstanceState(Bundle outState) {
    try {
        Log.i("ArticlesList.onSaveInstanceState", "Chirp chirp");
        dismissDialog(ArticlesList.ID_DIALOG_LOADING);
        removeDialog(ArticlesList.ID_DIALOG_LOADING);
    } catch (Exception e) {

    }

    super.onSaveInstanceState(outState);
}

@Override
protected void onDestroy() {
    try {
        dismissDialog(ArticlesList.ID_DIALOG_LOADING);
        removeDialog(ArticlesList.ID_DIALOG_LOADING);
    } catch (Exception e) {

    }

    super.onDestroy();
}

@Override
protected void onPause() {
    try {
        dismissDialog(ArticlesList.ID_DIALOG_LOADING);
        removeDialog(ArticlesList.ID_DIALOG_LOADING);
    } catch (Exception e) {

    }

    super.onPause();
}

@Override
public void onDetachedFromWindow() {
    try {
        dismissDialog(ArticlesList.ID_DIALOG_LOADING);
            removeDialog(ArticlesList.ID_DIALOG_LOADING);
    } catch (Exception e) {

    }

    super.onDetachedFromWindow();
}

This is a long code snippet. You will also see bunches of duplicated code that is constantly trying to kill that dialog dead. This is done because Android will destroy and then re-create a View whenever the phone is rotated. Hopefully in future versions, the window won't be completed destroyed and reloaded whenever the user rotates the screen, and instead implement a onScreenRotate() method.

After trying a few different ways, this hacky solution seems to be the only thing that delivers consistent, speedy results of removing the ProgressDialog. Hope this saves a few days of someone's time in the future.

追星践月 2024-09-17 19:31:55

我通过分两步构建对话框解决了同样的问题:

ProgressDialog progressDia = new ProgressDialog(this);
progressDia.setProgressStyle(ProgressDialog.STYLE_SPINNER);

I have fixed the same issue by constructing the dialog in two steps:

ProgressDialog progressDia = new ProgressDialog(this);
progressDia.setProgressStyle(ProgressDialog.STYLE_SPINNER);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文