android122 zhihuibeijing 主页面使用fragment搭建

fragment的生命周期:
onAttach()当fragment添加进Activity的时候调用(这个时候Activity对象已经存在了,不然就依赖不上去的)
onCreate()当fragment创建的时候调用。
onCreateView()当fragment画界面的时候调用。
onActivityCreated()当fragment依附的Activity创建完成的时候调用。
onStart(),onResume(),onPause(),onStop(),onDestroyView(),onDestroy().
onStart()对着onStop(),
onResume()对着onPause(),
onDestroyView()对着onCreateView(),
onDestroy()对着onCreate()。
Mainactivity
package com.itheima.zhbj52; import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction; import com.itheima.zhbj52.fragment.ContentFragment;
import com.itheima.zhbj52.fragment.LeftMenuFragment;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.jeremyfeinstein.slidingmenu.lib.app.SlidingFragmentActivity; /**
* 主页面
*/
public class MainActivity extends SlidingFragmentActivity {//使用开源SlidingMenu框架实现侧边栏, private static final String FRAGMENT_LEFT_MENU = "fragment_left_menu";
private static final String FRAGMENT_CONTENT = "fragment_content"; @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);//主页面是一个空的FrameLayout,后面会用Fragment填充。
setBehindContentView(R.layout.left_menu);//侧边栏也是一个空的FrameLayout,后面会用Fragment填充,SlidingFragmentActivity这个库的方法,设置左侧边栏布局。
SlidingMenu slidingMenu = getSlidingMenu();// 获取侧边栏对象
slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);// 设置主屏幕可以全屏触摸进行滑动,
slidingMenu.setBehindOffset(200);// 设置预留屏幕的宽度,左右侧边栏滑动的时候主屏幕最少有100像素。 initFragment();
} /**
* 初始化fragment, 将fragment数据填充给布局文件
*/
private void initFragment() {
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();// 开启事务
//fl_left_menu和fl_content位于不同的xml
transaction.replace(R.id.fl_left_menu, new LeftMenuFragment(),
FRAGMENT_LEFT_MENU);// 用fragment替换framelayout,FRAGMENT_LEFT_MENU是名字。
transaction.replace(R.id.fl_content, new ContentFragment(),
FRAGMENT_CONTENT);
transaction.commit();// 提交事务
// Fragment leftMenuFragment = fm.findFragmentByTag(FRAGMENT_LEFT_MENU);根据名字找fragment,
} }
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
空布局,就是一个容器。
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fl_content"
android:layout_width="match_parent"
android:layout_height="match_parent" > </FrameLayout>
left_menu.xml
<?xml version="1.0" encoding="utf-8"?>
空布局,就是一个容器。
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fl_left_menu"
android:layout_width="match_parent"
android:layout_height="match_parent" > </FrameLayout>
LeftMenuFragment
package com.itheima.zhbj52.fragment; import com.itheima.zhbj52.R; import android.view.View; /**
* 侧边栏
*
* @author Kevin
*
*/
public class LeftMenuFragment extends BaseFragment { @Override
public View initViews() {
View view = View.inflate(mActivity, R.layout.fragment_left_menu, null);
return view;
} }
fragment_left_menu.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f00" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我是侧边栏!" /> </RelativeLayout>
ContentFragment
package com.itheima.zhbj52.fragment; import com.itheima.zhbj52.R; import android.view.View; /**
* 主页内容
*
* @author Kevin
*
*/
public class ContentFragment extends BaseFragment { @Override
public View initViews() {
View view = View.inflate(mActivity, R.layout.fragment_content, null);
return view;
} }
fragment_content.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我是主页面哦!" /> </RelativeLayout>
BaseFragment
package com.itheima.zhbj52.fragment; import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup; /**
* fragment基类
*/
public abstract class BaseFragment extends Fragment { public Activity mActivity; // fragment创建
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mActivity = getActivity();//拿到fragment所依赖Activity的对象,fragment创建创建的时候Activity对象肯定是已经存在了,所以这个对象不会是空的。
} // 处理fragment的布局
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return initViews();//子类实现,因为每个布局不一样。
} // 依附的activity创建完成
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState); initData();
} // 子类必须实现初始化布局的方法
public abstract View initViews(); // 初始化数据, 可以不实现
public void initData() {
} }
android122 zhihuibeijing 主页面使用fragment搭建的更多相关文章
- android122 zhihuibeijing 主页面搭建
右边主页面布局设计: 文字颜色选择器和是否点击的图片选择器 路径和写法: <?xml version="1.0" encoding="utf-8"?&g ...
- android122 zhihuibeijing 新闻中心NewsCenterPager加载网络数据实现
新闻中心NewsCenterPager.java package com.itheima.zhbj52.base.impl; import java.util.ArrayList; import an ...
- Android主页导航:fragment+viewpager
简单实现Fragment+ViewPager实现主页导航控制,效果如下: 一.activity_main.xml布局文件: <?xml version="1.0" encod ...
- 用Fragment制作的Tab页面产生的UI重叠问题
本文出处:http://blog.csdn.net/twilight041132/article/details/43812745 在用Fragment做Tab页面,发现有时候进入应用会同时显示多个T ...
- Hexo+Github搭建博客
要使用Hexo,需要在你的系统中支持Nodejs以及Git,如果还没有,那就开始安装吧! 安装Node.js 下载Node.js 参考地址:安装Node.js 安装Git 下载地址:http://gi ...
- android TranslateAnimation 顶部segment分段移动动画
这里实现的功能是从主页布局的fragment点击跳转到一个acitivity,然后顶部是一个切换的segment顶部是一个listview,点击segment分段让listview加载不同的内容.我这 ...
- 自动化测试-20.selenium之FireFox下载项配置
前言: 当我们在使用Selenium运行自动化测试时,偶尔需要用到下载功能,但浏览器的下载可能会弹出下载窗口,或者下载路径不是我们想要保存的位置,所以在通过Selenium启动浏览器时需要做相关的设置 ...
- BBS项目架构
数据库设计 用户表(用的是auth_user那张表,通过自定义表继承AbstractUser) phone 电话 avatar 头像 create_time 创建时间#外键 blog 一对一个人站点表 ...
- webservice的使用-axis1-01
1.搭建axis服务器 1.1 下载axis-bin-1_4.zip文件并解压 1.2 拷贝\axis-1_4\webapps目录下的axis到tomcat目录下的webapps目录下并启动 1.3 ...
随机推荐
- Win7下的DragEnter、DragDrop事件不触发的解决方案
Win7与原来的XP和Win2003相比,安全控制方面更严格.比如,当我们以administrator登陆XP或Win2003时,运行所有的程序即是以管理员的身份启动的.但当以administrato ...
- 发送一个简单的HTTP GET请求并且取回响应。
string uri="http//www.baidu.com"; WebClient wc = new WebClient(); Console.WriteLine(" ...
- windows串口通信的一个活动图
1,打开串口的活动图: 2,关闭串口的活动图:
- leetcode@ [84/85] Largest Rectangle in Histogram & Maximal Rectangle
https://leetcode.com/problems/largest-rectangle-in-histogram/ https://leetcode.com/problems/maximal- ...
- When to Redis ? when to MongoDB?
120down voteaccepted I would say, it depends on kind of dev team you are and your application needs. ...
- Visual Studio 2008 – ASP.NET “System.Runtime.InteropServices.COMException”
The Issue When openning an existing ASP.NET project for the first time in Visual Studio 2008 it retu ...
- RecyclerView设置verticalSapcing等
RecyclerView没有像GridView那样有提供verticalSpacing属性,上StackOverflow找到了一种替代方法,代码如下 public class SpacesItemDe ...
- hdoj 2178 猜数字
猜数字 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- 把存储过程获取的数据输出到报表的html模板中
制作报表的html模板 <HTML><meta http-equiv="Content-Type" content="text/html; charse ...
- C#操作JSON学习
JSON(全称为JavaScript Object Notation) 是一种轻量级的数据交换格式.它是基于JavaScript语法标准的一个子集. JSON采用完全独立于语言的文本格式,可以很容易在 ...