getSupportFragmentManager要用在FragmentActivity及其子类中

mac2022-06-30  78

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;

 

@Override public 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 接口

 

@Override public 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

 

转载于:https://www.cnblogs.com/zhaocundang/p/5379500.html

最新回复(0)