ProgressDialog嵌套另一张空白对话里 [英] ProgressDialog nesting inside of another blank dialog

查看:134
本文介绍了ProgressDialog嵌套另一张空白对话里的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

乡亲

所以我把收尾的应用程序和仍有与应用程序作为一个整体一个未决的问题。

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

我在ProgressDialogs的形式显示加载的指标,同时拉下来的数据来自网络。出于某种原因或其他,我ProgressDialogs正在出现嵌套的又一空白对话框里面。其结果是俗气。

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.

我的布局code是这样的:

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>

在code我用它来显示ProgressDialog如下:

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.

任何帮助是AP preciated!

Any help is appreciated!

推荐答案

在身边把玩了几天,我想通了处理ProgressDialogs的最佳方式。简单ProgressDialogs处理肯定是过于复杂的Andr​​oid平台上,但这并不意味着我们有什么理由不与他们合作。希望这将得到更好的(以及文档)进入未来。这里是我的最佳实践。

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.

一ProgressDialog于空白对话框的嵌套问题似乎是pre-2.0 code正运行在2.1或2.2系统的问题。我只能猜测,show()方法还呼吁super.show(),其行为不同的2.1和2.2。

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.

所以,我发现它的作品最好的,如果你的不要在ProgressDialog对象调用show()直接。相反,使用内置的ShowDialog()和onCreateDialog()方法忙里忙外的对话框。这里有一个code片断:

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();
}

这是一个漫长的code段。您还将看到重复的code束,是不断试图杀死该对话框死了。这样做是因为Android将毁灭,然后重新创建每当手机旋转视图。希望在未来的版本中,该窗口将无法完成销毁并重新加载每当用户旋转屏幕,而是实现onScreenRotate()方法。

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.

在尝试一些不同的方式,这哈克的解决方案似乎是提供去除ProgressDialog一致,快速结果的唯一的事情。希望这节省了别人的时间了几天的未来。

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.

这篇关于ProgressDialog嵌套另一张空白对话里的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆