• <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)

            積分與排名

            最新評論

            閱讀排行榜

            評論排行榜

            麻豆久久久9性大片| 97久久超碰成人精品网站| 久久久久国产成人精品亚洲午夜| 成人免费网站久久久| 久久精品国产黑森林| 久久久久久久女国产乱让韩| 久久91精品国产91久久麻豆| 一本大道久久东京热无码AV| 2021久久国自产拍精品| 久久亚洲精品成人无码网站| 久久午夜电影网| 亚洲国产精品久久电影欧美| 久久久久久极精品久久久| MM131亚洲国产美女久久| 亚洲国产综合久久天堂| 四虎国产精品免费久久5151| 午夜精品久久久久久99热| 免费一级做a爰片久久毛片潮| 亚洲熟妇无码另类久久久| 人人狠狠综合久久亚洲婷婷| 久久久久亚洲av成人网人人软件| 国产精品欧美久久久久天天影视| 久久99国产综合精品女同| 亚洲天堂久久久| 日韩中文久久| 久久一区二区三区免费| 久久综合狠狠综合久久激情 | 狼狼综合久久久久综合网| 久久无码AV中文出轨人妻| 精品国产乱码久久久久软件| 久久久久久A亚洲欧洲AV冫 | 亚洲性久久久影院| 久久久噜噜噜久久| 久久电影网| 狠狠色综合久久久久尤物| 国产精品免费看久久久香蕉| 7国产欧美日韩综合天堂中文久久久久 | 久久婷婷五月综合97色直播| 久久国产成人精品国产成人亚洲| 久久久久国产| 久久精品国产亚洲AV影院|