一.Fragment使用:

要在你的activity中管理Fragment,须要使用FragmentManager,能够通过getFragmentManager(),这里注意要是在v4包要用getSupportFragmentManager()方法。

FragmentManager能够开一个事物调用beginTransaction()方法返回FragmentTransaction对象,这是我们能够用FragmentTransaction去运行删除添加Fragment的一些操作:

(1).add():往activity加入一个Fragment。

(2).remove():从Activity移除一个Fragment,当这个Fragment没有加入进回退栈,则会被销毁。

当加入进入回退栈之后则会销毁视图层,在显示这个Fragment则会调用onDestoryView和onCreateView。

(3).replace():使用还有一个Fragment替换当前的Fragment,也就是remove操作和add合体运行。

(4).hide():隐藏当前的Fragment,不过设为不可见,并不会销毁。

(5).show():显示之前隐藏的Fragment。

(6).detach():会将view从UI中移除,和remove()不同,此时fragment的状态依旧由FragmentManager维护。

(7).attach():重建view视图。附加到UI上并显示。

(8).addToBackStack():加入Fragment事务到回退栈中。

传null表示当前Fragment。

(9).commit():提交事务。

注意当以使用了add加入Fragment之后,你不过想隐藏Fragment,并且保持当前Fragment输入的数据,你只须要用hide(),假设你不想用户在看到数据,直接使用replace()比使用add()在remove()方便的多。

二.怎样使用回退栈保持数据:

实现效果,当进入第一个Fragment后点击改变文字button,会把显示在屏幕中央的TextView内容改变,然后点击进入第二个Fragment,在点击返回button,屏幕中央的TextView内容会还原成未改变前的,这就是说明了退回栈把Fragment的视图销毁了,可是实例并没有销毁。假设你想要不把数据也销毁,则就像上面说的使用hide()和show().

图片就不上传了。大家但是自行測试源代码(要源代码留下邮箱,这个是在上篇文章基础上改动的):

主Activity:

public class MainActivity extends Activity  {
RelativeLayout r1;
RelativeLayout r2;
RelativeLayout r3;
RelativeLayout view = null; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.bottom_layout); r1 = (RelativeLayout) findViewById(R.id.layout1);
r2 = (RelativeLayout) findViewById(R.id.layout2);
r3 = (RelativeLayout) findViewById(R.id.layout3); setDefaultFragment();
}
private void setDefaultFragment() {
FragmentManager fm = getFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
MyFragment my = new MyFragment();
transaction.add(R.id.frame_layout1, my,"ONE");
transaction.addToBackStack(null);
transaction.commit();
} }

Fragment1:

public class MyFragment extends Fragment implements OnClickListener {

	private Button button1;
private Button button2;
private TextView textView1; @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
System.out.println("onCreateView");
View view = inflater.inflate(R.layout.fragment_1, container, false);
button1 = (Button) view.findViewById(R.id.button1);
button1.setOnClickListener(this);
button2 = (Button) view.findViewById(R.id.button2);
button2.setOnClickListener(this);
textView1 = (TextView) view.findViewById(R.id.textView1);
return view;
} @Override
public void onClick(View arg0) {
switch (arg0.getId()) {
case R.id.button1:
textView1.append("哈");
break;
case R.id.button2:
MyFragment2 f2 = new MyFragment2();
FragmentManager fm = getFragmentManager();
FragmentTransaction tx = fm.beginTransaction();
tx.replace(R.id.frame_layout1, f2, "TWO");
tx.addToBackStack(null);
tx.commit();
break;
}
} }

Fragment2:

public class MyFragment2 extends Fragment implements OnClickListener {
private Button button1;
private Button button2;
private TextView textView1; @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
System.out.println("onCreateView");
View view = inflater.inflate(R.layout.fragment_2, container, false);
button1 = (Button) view.findViewById(R.id.button11);
button1.setOnClickListener(this);
button2 = (Button) view.findViewById(R.id.button21);
button2.setOnClickListener(this);
textView1 = (TextView) view.findViewById(R.id.textView2);
return view;
} @Override
public void onClick(View arg0) {
switch (arg0.getId()) {
case R.id.button11:
textView1.append("哈");
break;
case R.id.button21:
MyFragment3 f3 = new MyFragment3();
FragmentManager fm = getFragmentManager();
FragmentTransaction tx = fm.beginTransaction();
tx.hide(this);
tx.add(R.id.frame_layout1, f3, "THREE");
// tx.replace(R.id.id_content, fThree, "THREE");
tx.addToBackStack(null);
tx.commit();
break;
}
} }

Fragment1布局:

<?xml version="1.0" encoding="utf-8"?

>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="第一个页面" /> <Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:text="改变文字" /> <Button
android:id="@+id/button2"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:layout_below="@id/button1"
android:text="跳转第二个界面" /> </RelativeLayout>

Fragment3:

public class MyFragment3 extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_3, container, false);
}
}

这样就ok了。

三.Activity与Fragment之间的回调:

由于要考虑Fragment的反复使用,所以必须减少Fragment与Activity的耦合,并且Fragment更不应该直接操作别的Fragment,毕竟Fragment操作应该由它的管理者Activity来决定。

就用上边的样例,第一种回调:

public class MyFragment extends Fragment implements OnClickListener {

	private Button button1;
private Button button2;
private TextView textView1; @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
System.out.println("onCreateView");
View view = inflater.inflate(R.layout.fragment_1, container, false);
button1 = (Button) view.findViewById(R.id.button1);
button1.setOnClickListener(this);
button2 = (Button) view.findViewById(R.id.button2);
button2.setOnClickListener(this);
textView1 = (TextView) view.findViewById(R.id.textView1);
return view;
} public interface MyFragmentOneClick{
void onMyOneBtnClick();
} @Override
public void onClick(View arg0) {
switch (arg0.getId()) {
case R.id.button1:
textView1.append("哈");
break;
case R.id.button2:
//第一种回调方式
if (getActivity() instanceof MyFragmentOneClick)
{
((MyFragmentOneClick) getActivity()).onMyOneBtnClick();
}
break;
}
} }

另外一种回调:

public class MyFragment2 extends Fragment implements OnClickListener {
private Button button1;
private Button button2;
private TextView textView1;
private MyFragmentTwoClick myFragmentTwoClick ;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
System.out.println("onCreateView");
View view = inflater.inflate(R.layout.fragment_2, container, false);
button1 = (Button) view.findViewById(R.id.button11);
button1.setOnClickListener(this);
button2 = (Button) view.findViewById(R.id.button21);
button2.setOnClickListener(this);
textView1 = (TextView) view.findViewById(R.id.textView2);
return view;
} public interface MyFragmentTwoClick{
void onMyTwoBtnClick();
} //另外一种回调方式。设置回调接口
public void setMyTwoBtnClickListener(MyFragmentTwoClick myFragmentTwoClick)
{
this.myFragmentTwoClick = myFragmentTwoClick;
}
@Override
public void onClick(View arg0) {
switch (arg0.getId()) {
case R.id.button11:
textView1.append("哈");
break;
case R.id.button21:
if(myFragmentTwoClick != null)
{
myFragmentTwoClick.onMyTwoBtnClick();
}
break;
}
} }

主Activity实现:

public class MainActivity extends Activity implements MyFragmentOneClick,MyFragmentTwoClick {
RelativeLayout r1;
RelativeLayout r2;
RelativeLayout r3;
RelativeLayout view = null;
MyFragment f1;
MyFragment2 f2 ;
MyFragment3 f3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.bottom_layout); r1 = (RelativeLayout) findViewById(R.id.layout1);
r2 = (RelativeLayout) findViewById(R.id.layout2);
r3 = (RelativeLayout) findViewById(R.id.layout3); setDefaultFragment();
}
private void setDefaultFragment() {
FragmentManager fm = getFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
f1 = new MyFragment();
transaction.add(R.id.frame_layout1, f1,"ONE");
transaction.addToBackStack(null);
transaction.commit();
} @Override
public void onMyOneBtnClick() { if (f2 == null)
{
f2 = new MyFragment2();
f2.setMyTwoBtnClickListener(this);
}
FragmentManager fm = getFragmentManager();
FragmentTransaction tx = fm.beginTransaction();
tx.replace(R.id.frame_layout1, f2, "TWO");
tx.addToBackStack(null);
tx.commit();
}
@Override
public void onMyTwoBtnClick() {
if (f3 == null)
{
f3 = new MyFragment3(); }
FragmentManager fm = getFragmentManager();
FragmentTransaction tx = fm.beginTransaction();
tx.hide(f2);
tx.add(R.id.frame_layout1, f3, "THREE");
tx.addToBackStack(null);
tx.commit();
} }

这样也实现了上述的功能。

android (13) Fragment使用下的更多相关文章

  1. Android中Fragment和ViewPager那点事儿(仿微信APP)

    在之前的博文<Android中使用ViewPager实现屏幕页面切换和引导页效果实现>和<Android中Fragment的两种创建方式>以及<Android中Fragm ...

  2. Android中Fragment与Activity之间的交互(两种实现方式)

    (未给Fragment的布局设置BackGound) 之前关于Android中Fragment的概念以及创建方式,我专门写了一篇博文<Android中Fragment的两种创建方式>,就如 ...

  3. Android中Fragment的两种创建方式

    fragment是Activity中用户界面的一个行为或者是一部分.你可以在一个单独的Activity上把多个Fragment组合成为一个多区域的UI,并且可以在多个Activity中再使用.你可以认 ...

  4. 【Android】Fragment懒加载和ViewPager的坑

    效果 老规矩,先来看看效果 ANDROID和福利两个Fragment是设置的Fragment可见时加载数据,也就是懒加载.圆形的旋转加载图标只有一个,所以,如果当前Fragment正处于加载状态,在离 ...

  5. android之Fragment基础详解(一)

      一.Fragment的设计哲学 Android在3.0中引入了fragments的概念,主要目的是用在大屏幕设备上--例如平板电脑上,支持更加动态和灵活的UI设计.平板电脑的屏幕比手机的大得多,有 ...

  6. Android使用Fragment来实现ViewPager的功能(解决切换Fragment状态不保存)以及各个Fragment之间的通信

    以下内容为原创,转载请注明:http://www.cnblogs.com/tiantianbyconan/p/3364728.html 我前两天写过一篇博客<Android使用Fragment来 ...

  7. 33.Android之Fragment学习

    Fragment Android是在Android 3.0 (API level 11)开始引入Fragment的. 可以把Fragment想成Activity中的模块,这个模块有自己的布局,有自己的 ...

  8. Android之Fragment(二)

    本文主要内容 如何管理Fragment回退栈 Fragment如何与Activity交互 Fragment与Activity交互的最佳实践 没有视图的Fragment的用处 使用Fragment创建对 ...

  9. Android之Fragment(一)

    Fragment的产生与介绍 Android运行在各种各样的设备中,有小屏幕的手机,超大屏的平板甚至电视.针对屏幕尺寸的差距,很多情况下,都是先针对手机开发一套App,然后拷贝一份,修改布局以适应平板 ...

  10. Android 进阶 Fragment 介绍和使用 (二)

    管理fragment 因为FragmentManager的API是在Android 3.0,也即API level 11开始引入的,所以对于之前的版本,需要使用support library中的Fra ...

随机推荐

  1. 使用SpringMvc的一个注意事项

    在Intelij Idea下,如果在新建项目时使用了自带的模板,那么自动生成的web.xml里的DispatcherServlet配置节点默认的servlet-mapping是这样的: 而习惯上,我们 ...

  2. Spinner实现列表下拉功能

    public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener { ...

  3. 我的DBDA类

    <?php class DBDA { public $host="localhost"; public $uid="root"; public $pwd= ...

  4. SQL server 上机练习题

    首先创建一个数据库,里面有 登录表 学生表   课程表   选课表 成绩表 1. 查询Student表中的所有记录的Sname.Ssex和Class列.2. 查询教师所有的单位即不重复的Depart列 ...

  5. 计算型属性 vs 懒加载

    只实现 getter 方法的属性被称为计算型属性,等同于 OC 中的 ReadOnly 属性 计算型属性本身不占用内存空间 不可以给计算型属性设置数值 计算型属性可以使用以下代码简写 var titl ...

  6. Java8(一)--lambda表达式

    相信作为一个Java程序员都会或多或少的了解过Java8中的lambda表达式.函数式编程等,本人也是用过lambda表达式,使用的都是比较简单 的实现 通过一个例子去都感受lambda: Compa ...

  7. ThinkPHP---TP功能类之验证码

    [一]验证码 验证码全称:captcha(全自动识别机器与人类的图灵测试),简单理解就是区分当前操作是人执行的还是机器执行的 常见验证码分3种:页面上图片形式.短信验证码(邮箱验证可以归类到短信验证码 ...

  8. Python字符的转义

    参考原文 廖雪峰Python教程 字符的转义 字符串是以单引号' 或双引号" 括起来的任意文本,比如'abc',"xyz".''或""本身只是一种表示 ...

  9. React和Jquery比较

    Jquery的工作方式: 假如你需要给一个按扭添加一个点击事件. 首先根据CSS规则找到对应的dom元素,挂上一个匿名事件处理函数,在事件处理函数中,选中那个需要被修改的DOM元素,读取他的文本值,加 ...

  10. 【汇总】java中数组的声明、初始化及遍历

    java中数组用来存储固定大小的同类型元素 一维数组: 1.数组的声明: //声明一维数组,推荐用第一种 int[] a; int b[]; 2.数据的初始化:有三种初始化方式 (1).静态初始化 / ...