2013.12.04 (5)——— android SlidingMenu之FragmentChangeActivity
package com.jeremyfeinstein.slidingmenu.example.fragments;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import com.jeremyfeinstein.slidingmenu.example.BaseActivity;
import com.jeremyfeinstein.slidingmenu.example.R;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
public class FragmentChangeActivity extends BaseActivity {
private Fragment mContent;
public FragmentChangeActivity() {
super(R.string.changing_fragments);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// set the Above View
if (savedInstanceState != null)
mContent = getSupportFragmentManager().getFragment(savedInstanceState, "mContent");
if (mContent == null)
mContent = new ColorFragment(R.color.red);
// set the Above View
setContentView(R.layout.content_frame);
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.content_frame, mContent)
.commit();
// set the Behind View
setBehindContentView(R.layout.menu_frame);
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.menu_frame, new ColorMenuFragment())
.commit();
// customize the SlidingMenu
getSlidingMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
getSupportFragmentManager().putFragment(outState, "mContent", mContent);
}
public void switchContent(Fragment fragment) {
mContent = fragment;
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.content_frame, fragment)
.commit();
getSlidingMenu().showContent();
}
}
package com.jeremyfeinstein.slidingmenu.example.fragments;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import com.jeremyfeinstein.slidingmenu.example.R;
public class ColorMenuFragment extends ListFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.list, null);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
String[] colors = getResources().getStringArray(R.array.color_names);
ArrayAdapter<String> colorAdapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, android.R.id.text1, colors);
setListAdapter(colorAdapter);
}
@Override
public void onListItemClick(ListView lv, View v, int position, long id) {
Fragment newContent = null;
switch (position) {
case 0:
newContent = new ColorFragment(R.color.red);
break;
case 1:
newContent = new ColorFragment(R.color.green);
break;
case 2:
newContent = new ColorFragment(R.color.blue);
break;
case 3:
newContent = new ColorFragment(android.R.color.white);
break;
case 4:
newContent = new ColorFragment(android.R.color.black);
break;
}
if (newContent != null)
switchFragment(newContent);
}
// the meat of switching the above fragment
private void switchFragment(Fragment fragment) {
if (getActivity() == null)
return;
if (getActivity() instanceof FragmentChangeActivity) {
FragmentChangeActivity fca = (FragmentChangeActivity) getActivity();
fca.switchContent(fragment);
} else if (getActivity() instanceof ResponsiveUIActivity) {
ResponsiveUIActivity ra = (ResponsiveUIActivity) getActivity();
ra.switchContent(fragment);
}
}
}
这个例子主要介绍了,如何实现主界面fragmen的切换
1、在侧边栏的Fragment里面
private void switchFragment(Fragment fragment) {
if (getActivity() == null)
return;
if (getActivity() instanceof FragmentChangeActivity) {
FragmentChangeActivity fca = (FragmentChangeActivity) getActivity();
fca.switchContent(fragment);
} else if (getActivity() instanceof ResponsiveUIActivity) {
ResponsiveUIActivity ra = (ResponsiveUIActivity) getActivity();
ra.switchContent(fragment);
}
}
调用上层activity的switchContent方法
public void switchContent(Fragment fragment) {
mContent = fragment;
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.content_frame, fragment)
.commit();
getSlidingMenu().showContent();
}
分享到:
相关推荐
基于因子分析的我国A股上市...争力评价——以医药企业为例_张澳.caj
1,安装说明.txt ——————————安装手册 2,jdk-170.tar.gz ——————————JDK1.7deb包 3,switch_java.sh -------------------------java其它版本切换 4,check_java.sh———————————版本...
嵌入式成品项目——无线接收时钟.zip嵌入式成品项目——无线接收时钟.zip嵌入式成品项目——无线接收时钟.zip嵌入式成品项目——无线接收时钟.zip嵌入式成品项目——无线接收时钟.zip嵌入式成品项目——无线接收时钟...
04 java面试——杭州-阿里云-Java中级.pdf 05 java面试——杭州-蚂蚁金服-Java高级.pdf 06 java面试——杭州-蚂蚁金服-资深工程师.pdf 07 java面试——南京-软通动力-Java中级.pdf 08 java面试——厦门-中软国际-...
python项目——Word助手.zip python项目——Word助手.zip python项目——Word助手.zip python项目——Word助手.zip python项目——Word助手.zip python项目——Word助手.zip python项目——Word助手.zip python项目...
C语言项目——MP3音乐播放器.zip C语言项目——MP3音乐播放器.zip C语言项目——MP3音乐播放器.zip C语言项目——MP3音乐播放器.zip C语言项目——MP3音乐播放器.zip C语言项目——MP3音乐播放器.zip C语言项目——...
python项目——RCQ读者书库.zip python项目——RCQ读者书库.zip python项目——RCQ读者书库.zip python项目——RCQ读者书库.zip python项目——RCQ读者书库.zip python项目——RCQ读者书库.zip python项目——RCQ...
微信小程序——移动端商城(截图+源码).zip 微信小程序——移动端商城(截图+源码).zip 微信小程序——移动端商城(截图+源码).zip 微信小程序——移动端商城(截图+源码).zip 微信小程序——移动端商城(截图+...
微信小程序——用户反馈组件(截图+源码).zip 微信小程序——用户反馈组件(截图+源码).zip 微信小程序——用户反馈组件(截图+源码).zip 微信小程序——用户反馈组件(截图+源码).zip 微信小程序——用户反馈...
5. 甜——苦 6. 慢——快 7. 黑——白 8. 近——远 9. 前——后 10. 冷淡——热情 11. 黑暗——光明 12. 南——北 13. 低——高 14. 闲——忙 15. 开——关 16. 哭——笑 17. 臭——香 18. 丑——美 19. 出——入 20...
———————————————— 版权声明:本文为CSDN博主「MechMaster」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。 原文链接:...
IBExpert 2013.2.20 单文件和谐版,谁用谁知道。
发福利了,Aspose Total for NET 2013.09.10 最新破解,danny_su 大作,花了10个贡献分才下载的了!现免费提供大家:) This package contains the following .NET Components: - Aspose.BarCode for .NET v5.7.0.0 ...
"安卓Android源码——android仪表盘.zip" 这个标题明确指出,我们即将探讨的是与安卓(Android)操作系统相关的源代码,特别是关于“仪表盘”部分。在Android系统中,仪表盘通常指的是用户界面中的控件或者应用,...
微信小程序——面包旅行:界面设计,文本展示(截图+源码).zip 微信小程序——面包旅行:界面设计,文本展示(截图+源码).zip 微信小程序——面包旅行:界面设计,文本展示(截图+源码).zip 微信小程序——面包...
微信小程序——小游戏-别踩白块(截图+源码).zip 微信小程序——小游戏-别踩白块(截图+源码).zip 微信小程序——小游戏-别踩白块(截图+源码).zip 微信小程序——小游戏-别踩白块(截图+源码).zip 微信小程序...
java毕业设计——基于ssm的仿微博系统设计与实现(源码+数据库).zip java毕业设计——基于ssm的仿微博系统设计与实现(源码+数据库).zip java毕业设计——基于ssm的仿微博系统设计与实现(源码+数据库).zip java毕业...
第4 章 Linux——App 后台应用最广泛的系统 .. 107 第5 章 Nginx——App 后台HTTP 服务的利器 . 126 第6 章 MySQL——App 后台最常用的数据库 .. 140 第7 章 Redis——App 后台高性能的缓存系统 . 160 第8 章 ...
微信小程序——学校管理小程序以重邮为例(截图+源码).zip 微信小程序——学校管理小程序以重邮为例(截图+源码).zip 微信小程序——学校管理小程序以重邮为例(截图+源码).zip 微信小程序——学校管理小程序以...
基于IntraWeb的数据表格的多选实现 既可以单条操作,也可以多选操作。 delphi源代码。 BS开发Web网站开发,不需要安装服务器,Apache和IIS都不需要,自带企业级服务器。...————————————————