getSupportFragmentManager要用在FragmentActivity及其子類中,fragmentactivity

來源:互聯網
上載者:User

getSupportFragmentManager要用在FragmentActivity及其子類中,fragmentactivity

getSupportFragmentManager要用在FragmentActivity及其子類中!!

 

關於安卓抽屜導航!!

 * 自訂側邊欄

 

 

 

 

 

      建立一個Fragment:CarlozLibFragment.java,並為其建立一個布局carloz_lib_webview.xml,內部有一個WebView控制項,順便在AndroidManifest.xml中添加Intent存取權限;在CarlozLibFragment中重寫onCreateView方法,讓WebView訪問我的個人網站(http://carloz.duapp.com);

 

public class CarlozLibFragment extends Fragment {    private String TAG = "CARLOZ";    @Override    public View onCreateView(LayoutInflater inflater, ViewGroup container,            Bundle savedInstanceState) {        // TODO Auto-generated method stub        View root = inflater.inflate(R.layout.carloz_lib_webview, container, false);                WebView wv =(WebView)root.findViewById(R.id.wv);        Log.d(TAG, "load url: carloz lib");        wv.loadUrl("http://carloz.duapp.com");                return root;    }}

 

      將NavigationDrawerFragment.java 中 onCreateView中ListView相關內容刪除,用自訂布局 diy_slider_content.xml (目錄res/layout)替換;diy_slider_content中定義了一個按鈕,用來開啟剛剛建立的CarlozLibFragment;

 

@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,        Bundle savedInstanceState) {    View root = inflater.inflate(R.layout.diy_slider_content, container, false);        root.findViewById(R.id.btnJump).setOnClickListener(new OnClickListener() {        @Override        public void onClick(View v) {            if (mDrawerLayout != null) {                // 隱藏側邊欄                mDrawerLayout.closeDrawer(mFragmentContainerView);            }            if(mCallbacks != null) {                mCallbacks.onGotoCarlozLibClicked();            }        }    });        return root;}

 

      onGotoCarlozLibClicked()這個介面是自訂介面,在 靜態介面 NavigationDrawerCallbacks 中新增定義

 

public static interface NavigationDrawerCallbacks {    /**     * Called when an item in the navigation drawer is selected.     */    void onNavigationDrawerItemSelected(int position);        // 通過回調傳給主介面    void onGotoCarlozLibClicked();}

 

    需要在主介面SliderActivity中實現該回調方法, 因為主介面實現了 NavigationDrawerFragment.NavigationDrawerCallbacks 介面

 

@Overridepublic void onGotoCarlozLibClicked() {    // 需要實現 NavigationDrawerFragment.java Callback中新增的方法    // 在容器 container 中添加 fragment CarlozLibFragment    getSupportFragmentManager().beginTransaction()            .replace(R.id.container, new CarlozLibFragment())            .commit();}

 

運行結果如下:

 

 

這裡引用:http://www.cnblogs.com/carlo/p/4726719.html

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.