Dialog创建流程源码解析

转载来源:http://blog.csdn.net/geekerparadise/article/details/7787729


一、Dialog创建流程:

当我们重写Activity中onCreateDialog方法时,也就是意味着我们将要配合使用showDialog方法来在当前Activity显示自定义Dialog。

onCreateDialog的实现:


[java] view plaincopyprint?protected Dialog onCreateDialog(int id, Bundle args) {  
        return onCreateDialog(id);  
    }  
  
@Deprecated  
protected Dialog onCreateDialog(int id) {  
        return null;  
}  

protected Dialog onCreateDialog(int id, Bundle args) {
        return onCreateDialog(id);
    }

@Deprecated
protected Dialog onCreateDialog(int id) {
        return null;
}

以上方法是protected类型,是用来给子类重写的。当我们在需要显示Dialog的时候调用showDialog,那么相应id的dialog就出现,查看showDialog源码得出:

showDialog的实现:

[java] view plaincopyprint?public final boolean showDialog(int id, Bundle args) {  
        if (mManagedDialogs == null) {  
            mManagedDialogs = new SparseArray<ManagedDialog>();  
        }  
        ManagedDialog md = mManagedDialogs.get(id);  
        if (md == null) {  
            md = new ManagedDialog();  
            md.mDialog = createDialog(id, null, args);  
            if (md.mDialog == null) {  
                return false;  
            }  
            mManagedDialogs.put(id, md);  
        }  
          
        md.mArgs = args;  
        onPrepareDialog(id, md.mDialog, args);  
        md.mDialog.show();  
        return true;  
    }  

public final boolean showDialog(int id, Bundle args) {
        if (mManagedDialogs == null) {
            mManagedDialogs = new SparseArray<ManagedDialog>();
        }
        ManagedDialog md = mManagedDialogs.get(id);
        if (md == null) {
            md = new ManagedDialog();
            md.mDialog = createDialog(id, null, args);
            if (md.mDialog == null) {
                return false;
            }
            mManagedDialogs.put(id, md);
        }
        
        md.mArgs = args;
        onPrepareDialog(id, md.mDialog, args);
        md.mDialog.show();
        return true;
    }此Dialog会以自定义类型ManagedDialog存入SparseArray<E>中,每个id对应于一个Dialog。

ManagedDialog的定义:

[java] view plaincopyprint?private static class ManagedDialog {  
        Dialog mDialog;  
        Bundle mArgs;  
}  

private static class ManagedDialog {
        Dialog mDialog;
        Bundle mArgs;
}showDialog中调用到了createDialog,以下是具体实现。

createDialog的实现:

[java] view plaincopyprint?private Dialog createDialog(Integer dialogId, Bundle state, Bundle args) {  
     final Dialog dialog = onCreateDialog(dialogId, args);  
     if (dialog == null) {  
         return null;  
     }  
     dialog.dispatchOnCreate(state);  
     return dialog;  
}  

private Dialog createDialog(Integer dialogId, Bundle state, Bundle args) {
     final Dialog dialog = onCreateDialog(dialogId, args);
     if (dialog == null) {
         return null;
     }
     dialog.dispatchOnCreate(state);
     return dialog;
}
可以看出它调用了子类重写父类的onCreateDialog方法。最后在showDialog中拿出相应的Dialog让其显示。

此处设计是把存在的Dialog存入SparseArray中,当要再次显示时会首先从SparseArray中找,如果没有才回新建。



二、SparseArray

在上例中我们发现了Android使用自定义的SparseArray类,它是一个类似于HashMap的工具,用来存放Object类型数据。

1:构造方法:

[java] view plaincopyprint?public SparseArray() {  
        this(10);  
}  
public SparseArray(int initialCapacity) {  
        initialCapacity = ArrayUtils.idealIntArraySize(initialCapacity);  
  
        mKeys = new int[initialCapacity];  
        mValues = new Object[initialCapacity];  
        mSize = 0;  
}  

public SparseArray() {
        this(10);
}
public SparseArray(int initialCapacity) {
        initialCapacity = ArrayUtils.idealIntArraySize(initialCapacity);

        mKeys = new int[initialCapacity];
        mValues = new Object[initialCapacity];
        mSize = 0;
}
2:容量变化:

上文构造方法中我们可以看出它的初始化容量是10。在put方法中有扩充判断,增量为1:


[java] view plaincopyprint?if (mSize >= mKeys.length) {  
int n = ArrayUtils.idealIntArraySize(mSize + 1);  
  
    int[] nkeys = new int[n];  
    Object[] nvalues = new Object[n];  
  
    // Log.e("SparseArray", "grow " + mKeys.length + " to " + n);   
    System.arraycopy(mKeys, 0, nkeys, 0, mKeys.length);  
    System.arraycopy(mValues, 0, nvalues, 0, mValues.length);  
    mKeys = nkeys;  
    mValues = nvalues;  
}  

if (mSize >= mKeys.length) {
int n = ArrayUtils.idealIntArraySize(mSize + 1);

    int[] nkeys = new int[n];
    Object[] nvalues = new Object[n];

    // Log.e("SparseArray", "grow " + mKeys.length + " to " + n);
    System.arraycopy(mKeys, 0, nkeys, 0, mKeys.length);
    System.arraycopy(mValues, 0, nvalues, 0, mValues.length);
    mKeys = nkeys;
    mValues = nvalues;
}

3:折半查找。

这个类比较核心的方法是折半查找。具体实现如下:

[java] view plaincopyprint?private static int binarySearch(int[] a, int start, int len, int key) {  
        int high = start + len, low = start - 1, guess;  
        while (high - low > 1) {  
            guess = (high + low) / 2;  
            if (a[guess] < key)  
                low = guess;  
            else  
                high = guess;  
        }  
        if (high == start + len)  
            return ~(start + len);  
        else if (a[high] == key)  
            return high;  
        else  
            return ~high;  
}  

private static int binarySearch(int[] a, int start, int len, int key) {
        int high = start + len, low = start - 1, guess;
        while (high - low > 1) {
            guess = (high + low) / 2;
            if (a[guess] < key)
                low = guess;
            else
                high = guess;
        }
        if (high == start + len)
            return ~(start + len);
        else if (a[high] == key)
            return high;
        else
            return ~high;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值