• <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 閱讀(1911) 評論(0)  編輯 收藏 引用 所屬分類: 隨筆而已G7

            公告

            Using C++

            導航

            統計

            留言簿(73)

            隨筆分類(513)

            積分與排名

            最新評論

            閱讀排行榜

            評論排行榜

            久久久久国产视频电影| 国产精品久久久久久久人人看| 无码国产69精品久久久久网站| 97精品依人久久久大香线蕉97| 欧美黑人又粗又大久久久| 国产精品国色综合久久| 精品久久久久久无码人妻热| 亚洲精品国产第一综合99久久| 狠狠色婷婷久久一区二区三区| 日韩欧美亚洲综合久久影院d3| 区久久AAA片69亚洲| 9久久9久久精品| 中文字幕久久亚洲一区| 久久九九有精品国产23百花影院| 中文国产成人精品久久亚洲精品AⅤ无码精品 | 精品久久久久久无码专区| 97久久精品人人做人人爽| 人妻无码αv中文字幕久久琪琪布 人妻无码精品久久亚瑟影视 | 国产精品久久久久久一区二区三区 | 久久99精品国产麻豆不卡| 亚洲精品无码久久久久去q| 久久精品国产亚洲Aⅴ蜜臀色欲| 亚洲AV无码久久精品成人 | 久久国产精品久久国产精品| 99久久香蕉国产线看观香| 久久国产精品99久久久久久老狼| 久久精品国产清自在天天线| 久久精品国产一区二区三区| 九九久久99综合一区二区| 久久综合给合久久狠狠狠97色69| 99久久香蕉国产线看观香| 久久人人爽人人爽AV片| 国产免费久久精品丫丫| 国产精品久久久久无码av| 国产精品美女久久久m| 久久婷婷激情综合色综合俺也去| 久久综合亚洲鲁鲁五月天| 久久人妻无码中文字幕| 精产国品久久一二三产区区别| 亚洲精品tv久久久久| 免费无码国产欧美久久18|