1、Fragment的静态使用
1、创建一个Fragment类
1、要继承Fragment
2、重写onCreateView方法,该方法会返回这个Fragment显示的视图
3、该Fragment需要个xml布局文件
在Activity的xml文件中使用<fragment>标签静态引入这个Fragment
1、必须要有id属性
2、必须要有name属性,包名.类名,让该标签知道要加在的是哪个Fragment类
3、Activity需要继承FragmentActivity
2、Fragment生命周期
一共11个声明周期,比Activity多出来4个,其中有5个不同的
启动时候的声明周期和Activity对比
1、Fragment 会先调用onAttach() 建立Fragment和Activity连接的
2、Fragment 会调用onCreateView来创建当前Fragment显示的视图
3、Fragment 会调用onActivityCreate() 在Activity的oncreate()方法执行完毕后 调用
其他声明周期 例如 oncreate 、 onstart 、 onresume 都差不多
关闭时候的对比
1、Fragment 会调用onDestroyView()声明周期方法 销毁Fragment显示的视图
2、Fragment 最后会调用一个onDettach()方法 和Activity解除关联
其他生命周期 onpause 、 onstop 、ondestroy;
onRestart()
Fragment中无该声明周期,Activity中有
3、Fragment动态使用
1、Activity继承FragmentActivity
2、在activity的xml布局文件中,创建一个帧布局FrameLayout,是一个容器,可以往该容器中动态的添加Fragment对象
3、在activity中获取Fragment管理器对象
4、使用管理器对象开始一个事务
5、使用这个事务可以调用replace方法把一个Fragment对象替换到FrameLayout容器中
6、提交事务
4、Fragment的数据交互
1、静态Fragment数据交互
使用管理器调用findFragmentById
2、动态Fragment数据交互
findFragmentByTag
tag值的设置:使用事务对象调用replace或者add方法可以给Fragment添加tag值
3、接口回调
1、在Fragment中创建一个接口,该接口中定义一个抽象方法。该抽象方法就是完成具体逻辑的方法,他的实现是在调用的时候重写该方法。
2、创建这个接口的全局变量,在Fragment中指定的事件中,使用这个接口类型的对象调用抽象方法
3、定义一个setXXXListener监听方法,在实现功能的时候传递一个具体的接口类型对象进来。
4、在调用setXXXListener的地方做具体的功能实现
4、在其中一个Fragment中获取到Fragment管理器,使用该管理器操作其他Fragment对象
5、FragmentTransaction的5个方法
1、add
2、remove
3、hide
4、show
5、replace
实际上就是remove和add的结合
6、回退栈
使用事务对象调用addToBackStack()把Fragment放入到回退栈中
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<RelativeLayout
android:id="@+id/rl"
android:layout_width="match_parent"
android:layout_height="50dp"
>
<TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="你好,欢迎来到海淀"
android:layout_toLeftOf="@+id/btn1"
android:gravity="center"
/>
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="进入"
android:layout_toLeftOf="@+id/btn2"
/>
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="退出"
android:layout_alignParentRight="true"
/>
</RelativeLayout>
<!-- 该标签必须是小写字母 -->
<fragment
android:name="com.example.day8fragment.FragmentLeft"
android:id="@+id/frag_left"
android:layout_width="150dp"
android:layout_height="match_parent"
android:layout_below="@+id/rl"
/>
<fragment
android:name="com.example.day8fragment.FragmentRight"
android:id="@+id/frag_right"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_toRightOf="@+id/frag_left"
android:layout_below="@+id/rl"
/>
</RelativeLayout>
android:id="@+id/frag_left_lv"
android:layout_width="match_parent"
android:layout_height="match_parent"
></ListView>
Activity类:
package com.example.day8fragment;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
/*
* 如果Activity中需要使用Fragment碎片 那么该Activity必须要管理这些碎片
* 所以需要继承FragmentActivity 因为该类会提供一个Fragment的管理器
*/
public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
左Fragment:
//推荐使用support.v4包中的fragment
//因为该包是用来支持3.0版本一下的手机的
public class FragmentLeft extends Fragment{
List<String> list;
//为该fragment 设置一个现实的 view
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
/*
* 1、作为fragment要显示的视图 可以使用View.inflate引入
* 2、在引入布局文件的时候 需要用到activity的上下文
* 问题:因为当前是在Fragment类中 如果来获取该view需要显示的Activity上下问
* 思考,之前说过 fragment的使用必须依赖于Activity,
* 所以api提供了一个方法 可以在Fragment中获取依赖的Activity对象
*/
View view = View.inflate(getActivity(), R.layout.frag_left, null);
/*
* 1
* 2
* 3
* 4
*/
ListView lv = (ListView) view.findViewById(R.id.frag_left_lv);
initData();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, list);
lv.setAdapter(adapter);
return view;
}
private void initData() {
list = new ArrayList<String>();
for (int i = 0; i < 20; i++) {
list.add("导航" + i);
}
}
}
package com.example.day8fragment;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class FragmentRight extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = View.inflate(getActivity(), R.layout.frag_right, null);
return view;
}
}
一个简单的小dome到这就完成了!