getSupportFragmentManager要用在FragmentActivity及其子类中
来源:互联网 发布:淘宝网卖围巾的店 编辑:程序博客网 时间:2024/05/23 23:31
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
0 0
- getSupportFragmentManager要用在FragmentActivity及其子类中
- 在Struts中使用 DispatchAction 及其子类
- 在Activity中没有getSupportFragmentManager()这个方法
- AppCompatActivity在Androidstudio可以用,在eclipse中getSupportFragmentManager()报错
- 如何解决在Activity中无法获取getSupportFragmentManager()
- iOS中如何在Xib中为UIView及其子类设置圆角
- 不能在viewgroup及其子类的构造函数中中获取child
- 在FragmentActivity中调用Fragment方法字段等
- 在Activity中使用Fragment需要先集成FragmentActivity
- 如何在Fragment中获取FragmentActivity中的布局????
- //如果要在子类中调用父类的方法,则应在子类的构造方法中调用
- 关于UIScollView及其子类的在导航控制器
- FragmentActivity
- FragmentActivity
- fragmentActivity
- java中关于collection和map及其子类
- Android中Fragment子类及其PreferenceFragment的创建过程演示
- FragmentActivity中不显示ActionBar
- linux系统下怎么安装.deb文件?
- stm32 hid 键盘描述
- openwrt 安装 ser2net 配置
- java支付宝无密退款
- nginx 配置php
- getSupportFragmentManager要用在FragmentActivity及其子类中
- 如何利用zendstudio新建 或导入php项目
- 关于stm32定时器的理解
- 每建一个Activity都要注册权限Manifest.xml
- Android带有边框的RelativeLayout、LinearLayout
- Fragment 与Activity
- android nagative drawer图标跟标题适配
- android 项目中出现红色感叹号的解决方法
- 【译】MySQL – The Error Log