• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            Codejie's C++ Space

            Using C++

            EL : Fragment and FragmentManager


                考慮到為了減少UI的'刷新效果',EL使用Fragment來代替常規的Activity方式.
                Fragment代替Activity的好處很多,比如沒有不同UI間切換的'閃動',視覺是上也有加快顯示的效果.但這種效果也是有代價的, 大量Activity方式下的工作,在Fragment時,不得不自己來實現,比如UI的'壓棧'等等.
                這里記錄下EL實現中,碰到的跟Fragment相關的問題. (Fragment相關文檔的官方鏈接在這里)

                1. 參數傳遞
                    Activity切換時,可以在StartActivity()時,通過Intent的方式將所需參數傳遞給下一個Activity. 但在Fragment方式下就不行了,此時可使用/參考Fragment支持的setArguments()和getArguments(). EL中所有Fragment都繼承于BaseFragment,在顯示Fragment時,使用設計的OnArgument()接口傳遞參數.
                    fragment.onArguments(args);
                    fragmentManager.beginTransaction().show(fragment).commit();

                2. 界面切換
                    FragmentManager是用于管理Fragment的類,通過內部的FragmentTranscation對象實現Fragment的加載/顯示/隱藏/移除等操作. EL實現中,使用FragmentSwitcher類封裝了FrangmentManager,以實現某些更靈活的Fragment管理,比如某些Fragment在被切換是需要刪除,而其他的僅需要隱藏等操作.
                public enum Type {
                    
                    LIST("list", false), SHOW("show", false), ABOUT("about", true), SETTING("setting", true),
                    DOWNLOAD("download", true);
                    
                    private final String title;
                    private final boolean removed;
                    
                    private Type(final String title, boolean removed) {
                        this.title = title;
                        this.removed = removed;
                    }
                    
                    public String getTitle() {
                        return title;
                    }

                    public boolean hasRemoved() {
                        return removed;
                    }
                    
                    public static Type getType(final String title) {
                        if (title.equals(LIST.getTitle())) {
                            return LIST;
                        } else if (title.equals(SHOW.getTitle())) {
                            return SHOW;
                        } else {
                            return null;
                        }
                    }
                    
                }

                public boolean show(Type type, Bundle args) {
                    if (curType != null) {
                        if (curType == type) {
                            ((BaseFragment) fragmentManager.findFragmentByTag(type.getTitle())).onArguments(args);                
                            return true;
                        } else {
                            hide(curType);
                        }
                    }
                    
                    BaseFragment fragment = (BaseFragment) fragmentManager.findFragmentByTag(type.getTitle());
                    if (fragment == null) {
                        fragment = create(type);
                        if (fragment == null) {
                            return false;
                        }
                    }
                    
                    fragment.onArguments(args);
                    fragmentManager.beginTransaction().show(fragment).commit();
                    curType = type;
                    
                    return true;
                }

                3. Fragment的棧
                    Activity在切換時,通常情況下可以通過BACK鍵返回前一個Activity,是因為存在一個'棧'來存放前面的Activity. Fragment也有棧的概念,但在操作上就需要自己主動調用了 -- Fragment隱藏時,壓棧; Fragment退出時,出棧.
                private void hide(Type type) {
                    BaseFragment fragment = (BaseFragment) fragmentManager.findFragmentByTag(type.getTitle());
                    if (fragment != null) {
                        if (type.hasRemoved()) {
                            fragmentManager.beginTransaction().remove(fragment).commit();
                        } else {
                            FragmentTransaction ft = fragmentManager.beginTransaction();
                            ft.addToBackStack(type.getTitle());
                            ft.hide(fragment);
                            ft.commit();
            //                fragmentManager.beginTransaction().hide(fragment).commit();
                        }
                        
                        curType = null;            
                    }
                }

                public boolean showPrevFragment() {
                    int count = fragmentManager.getBackStackEntryCount();
                    if (count > 0) {
                        String name = fragmentManager.getBackStackEntryAt(count - 1).getName();
                        fragmentManager.popBackStack();// .popBackStackImmediate();
                        Type type = Type.getType(name);
                        if (type != null) {
                            show(type);
                            return true;
                        }
                    }
                    
                    return false;
                }


                總的來說,Fragment要想用著方便,自己使用FragmentSwitcher和BaseFragment來封裝下FragmentManager和Fragment還是不錯的選擇.

                Fragment看起來很美,用起來真是發瘋啊.上面這些都是實現EL中碰到的問題,這里做下記錄,怕回頭又忘記'痛苦地翻文檔'的日子...

                另,EL基本完工,這兩天可發布Alpha版了...

            posted on 2013-08-11 10:08 codejie 閱讀(1912) 評論(0)  編輯 收藏 引用 所屬分類: 隨筆而已G7

            公告

            Using C++

            導航

            統計

            留言簿(73)

            隨筆分類(513)

            積分與排名

            最新評論

            閱讀排行榜

            評論排行榜

            91精品国产综合久久四虎久久无码一级| 久久综合一区二区无码| 亚洲精品美女久久久久99| 人人狠狠综合久久88成人| 国产韩国精品一区二区三区久久| 国产成人综合久久久久久| 色妞色综合久久夜夜| 国内精品久久久久影院日本| 国产精品免费久久| 久久精品国产99久久无毒不卡| 国内精品久久久久久不卡影院| 无码日韩人妻精品久久蜜桃| 国产69精品久久久久9999| 久久亚洲春色中文字幕久久久| 久久久久国产一级毛片高清板| 久久99热这里只有精品66| 9191精品国产免费久久| 色婷婷综合久久久久中文 | 久久综合国产乱子伦精品免费| 久久噜噜电影你懂的| 97r久久精品国产99国产精| 国内精品综合久久久40p| 色诱久久av| 久久精品18| 久久久久这里只有精品| 久久亚洲精品视频| 精品免费tv久久久久久久| 人妻丰满AV无码久久不卡| 综合人妻久久一区二区精品| 欧美伊人久久大香线蕉综合69| 成人精品一区二区久久久| 色噜噜狠狠先锋影音久久| 国产亚洲婷婷香蕉久久精品| avtt天堂网久久精品| 97久久超碰成人精品网站| 久久99中文字幕久久| 国产成年无码久久久久毛片| 久久精品成人免费网站| 99久久精品九九亚洲精品| 久久综合九色综合久99| 亚洲&#228;v永久无码精品天堂久久|