編輯:關於Android編程
Fragment管理中,不得不談到的就是它的事務管理,它的事務管理寫的非常的出彩。我們先引入一個簡單常用的Fragment事務管理代碼片段:
FragmentTransaction ft = this.getSupportFragmentManager().beginTransaction(); ft.add(R.id.fragmentContainer, fragment, "tag"); ft.addToBackStack("tag"); ft.commitAllowingStateLoss();
我們先來看Manager.beginTransaction這個方法的返回值:
/** * Start a series of edit operations on the Fragments associated with this * FragmentManager. * *在Fragment的管理中FragmentManager的實現類是FragmentManagerImpl,當然這也是Android一貫的命名方式;* Note: A fragment transaction can only be created/committed prior to an * activity saving its state. If you try to commit a transaction after * {@link FragmentActivity#onSaveInstanceState * FragmentActivity.onSaveInstanceState()} (and prior to a following * {@link FragmentActivity#onStart FragmentActivity.onStart} or * {@link FragmentActivity#onResume FragmentActivity.onResume()}, you will * get an error. This is because the framework takes care of saving your * current fragments in the state, and if changes are made after the state * is saved then they will be lost. *
*/ public abstract FragmentTransaction beginTransaction();
FragmentManagerImpl.java: @Override public FragmentTransaction beginTransaction() { return new BackStackRecord(this); }
void addOp(Op op) { if (mHead == null) { mHead = mTail = op; } else { op.prev = mTail; mTail.next = op; mTail = op; } op.enterAnim = mEnterAnim; op.exitAnim = mExitAnim; op.popEnterAnim = mPopEnterAnim; op.popExitAnim = mPopExitAnim; mNumOp++; }
public FragmentTransaction add(Fragment fragment, String tag) { doAddOp(0, fragment, tag, OP_ADD); return this; } public FragmentTransaction add(int containerViewId, Fragment fragment) { doAddOp(containerViewId, fragment, null, OP_ADD); return this; } public FragmentTransaction add(int containerViewId, Fragment fragment, String tag) { doAddOp(containerViewId, fragment, tag, OP_ADD); return this; }
是采用"Builder"的方式來組織。文章開始我已經提到了,Fragment的事務管理是比較出彩的代碼,單純的事務采用了至少三套模式來組織,而且組織起來絲毫沒有感覺。當然Fragment帶給我們的驚喜還不僅限於此。我們總上面的代碼片段可以看出,實際上,通過事務類BackStackRecord生成Op對象實際上在復制BackStackRecord的屬性,所以當我們分析每一個Op裡面的數據的時候,可以直接用BackStackRecord中的屬性映射。
int mNumOp;//Op數量 int mEnterAnim;//進入動畫 int mExitAnim;//退出動畫 int mPopEnterAnim;//彈出進入動畫 int mPopExitAnim;//彈出退出動畫 int mTransition;//轉場動畫 int mTransitionStyle; boolean mAddToBackStack;//是否加入到BackStack中
static final int OP_NULL = 0; static final int OP_ADD = 1; static final int OP_REPLACE = 2; static final int OP_REMOVE = 3; static final int OP_HIDE = 4; static final int OP_SHOW = 5; static final int OP_DETACH = 6; static final int OP_ATTACH = 7;
BackStackRecord.java: int commitInternal(boolean allowStateLoss) { if (mCommitted) throw new IllegalStateException("commit already called"); mCommitted = true; if (mAddToBackStack) { mIndex = mManager.allocBackStackIndex(this); } else { mIndex = -1; } mManager.enqueueAction(this, allowStateLoss); return mIndex; }
FragmentManager.java: public void enqueueAction(Runnable action, boolean allowStateLoss) { if (!allowStateLoss) { checkStateLoss(); } synchronized (this) { if (mActivity == null) { throw new IllegalStateException("Activity has been destroyed"); } if (mPendingActions == null) { mPendingActions = new ArrayList(); } mPendingActions.add(action); if (mPendingActions.size() == 1) { mActivity.mHandler.removeCallbacks(mExecCommit); mActivity.mHandler.post(mExecCommit); } } }
BackStackRecord.java: Op op = mHead; while (op != null) { ... }
Op.java: case OP_ADD: { Fragment f = op.fragment; f.mNextAnim = op.enterAnim; mManager.addFragment(f, false); } break;
FragmentManager.java: public void addFragment(Fragment fragment, boolean moveToStateNow) { if (mAdded == null) { mAdded = new ArrayList(); } makeActive(fragment); if (!fragment.mDetached) { if (mAdded.contains(fragment)) { throw new IllegalStateException("Fragment already added: " + fragment); } mAdded.add(fragment); fragment.mAdded = true; fragment.mRemoving = false; if (fragment.mHasMenu && fragment.mMenuVisible) { mNeedMenuInvalidate = true; } if (moveToStateNow) { moveToState(fragment); } } }
在使用TextView的過程中,有時候會需要將一串文本中的部分文字做特別的顯示效果處理,比如加粗、改變顏色、加著重標識、超鏈接等等,我們可以通過多個TextView拼湊來
android矢量動畫!直接來個例子就明白了!(這裡我把與動畫無關的屬性都用…表示)首先你要有個矢量圖比如這個矢量圖xml文件叫”vector1
在前一章中講的是Android使用HttpURLConnection下載圖片,這一章使用HttpClient下載圖片 HttpURLConnection與HttpClie
在Android上,搜索是一個核心用戶特性。用戶可以搜索可用的任何數據,不管內容是存促於設備本身或者需要通過網絡訪問。Android提供了一個搜索框架為用戶創建一個一致的