转载来源: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); }@Deprecatedprotected 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;}