android (13) Fragment使用下
一.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使用下的更多相关文章
- Android中Fragment和ViewPager那点事儿(仿微信APP)
在之前的博文<Android中使用ViewPager实现屏幕页面切换和引导页效果实现>和<Android中Fragment的两种创建方式>以及<Android中Fragm ...
- Android中Fragment与Activity之间的交互(两种实现方式)
(未给Fragment的布局设置BackGound) 之前关于Android中Fragment的概念以及创建方式,我专门写了一篇博文<Android中Fragment的两种创建方式>,就如 ...
- Android中Fragment的两种创建方式
fragment是Activity中用户界面的一个行为或者是一部分.你可以在一个单独的Activity上把多个Fragment组合成为一个多区域的UI,并且可以在多个Activity中再使用.你可以认 ...
- 【Android】Fragment懒加载和ViewPager的坑
效果 老规矩,先来看看效果 ANDROID和福利两个Fragment是设置的Fragment可见时加载数据,也就是懒加载.圆形的旋转加载图标只有一个,所以,如果当前Fragment正处于加载状态,在离 ...
- android之Fragment基础详解(一)
一.Fragment的设计哲学 Android在3.0中引入了fragments的概念,主要目的是用在大屏幕设备上--例如平板电脑上,支持更加动态和灵活的UI设计.平板电脑的屏幕比手机的大得多,有 ...
- Android使用Fragment来实现ViewPager的功能(解决切换Fragment状态不保存)以及各个Fragment之间的通信
以下内容为原创,转载请注明:http://www.cnblogs.com/tiantianbyconan/p/3364728.html 我前两天写过一篇博客<Android使用Fragment来 ...
- 33.Android之Fragment学习
Fragment Android是在Android 3.0 (API level 11)开始引入Fragment的. 可以把Fragment想成Activity中的模块,这个模块有自己的布局,有自己的 ...
- Android之Fragment(二)
本文主要内容 如何管理Fragment回退栈 Fragment如何与Activity交互 Fragment与Activity交互的最佳实践 没有视图的Fragment的用处 使用Fragment创建对 ...
- Android之Fragment(一)
Fragment的产生与介绍 Android运行在各种各样的设备中,有小屏幕的手机,超大屏的平板甚至电视.针对屏幕尺寸的差距,很多情况下,都是先针对手机开发一套App,然后拷贝一份,修改布局以适应平板 ...
- Android 进阶 Fragment 介绍和使用 (二)
管理fragment 因为FragmentManager的API是在Android 3.0,也即API level 11开始引入的,所以对于之前的版本,需要使用support library中的Fra ...
随机推荐
- PS技巧汇总
一.gif图流程 1:素材图片a 图片b 2:窗口--->时间轴/动画 3:复制所选帧--->设置帧延迟 4:文件--->存储为WEB格式--->gif格式 二.批处理--- ...
- js执行顺序——学习笔记
我们知道有个全局的 window对象,js的一切皆window上的属性和方法.window上有个window.document属性,记录了整个html的dom树,document是顶层. body 和 ...
- Tuple类型的使用
1.什么是Tuple Tuple类型,可以存放任何类型 2.Tuple有哪些分类 .Net 4.0 定义了8个泛型Tuple类,和一个Tuple静态类 3.Tuple的使用
- 网页添加qq咨询
<style>.box{ width:130px; height:150px; position:fixed; right:0px; top:30%; z-index:999; borde ...
- Java基础(七)--Exception异常处理
发现错误的理想时机是程序运行之前(编译期),然后不太现实,很多异常无法被发现(特别是业务上的数据),需要在运行时解决. 错误恢复机制保证代码健壮性的方式,异常处理在程序中很常见,也是必须的,必须考虑有 ...
- spring用来干什么,解决的问题
// 1. 实体类 class User{ } //2. dao class UserDao{ .. 访问db } //3. service class UserService{ UserDao ...
- selenium爬虫设置headers,代理IP等方法
https://blog.csdn.net/xc_zhou/article/details/80823855
- 网络编程 - socket接收大数据
通过socket,实现客户端发送命令,将服务端执行出的结果,反回到客户端,主要4个步骤:1.服务端返回数据: 2.服务端返回数据的大小: 3.客户端接收返回数据的大小: 4.客户端按返回数据大小接收数 ...
- linux vim 常用操作
vim 字符级 上k下j左h右i,键盘的方向键也可以移动 单词级 b上个单词首字母 w下个单词首字母 e下个单词的尾字母 行级 0行首 $行尾 删除 dd 删除光标所在行 文档级 gg 文档首行,首个 ...
- xmpp聊天室(5)
聊天室 //初始化聊天室 XMPPJID *roomJID = [XMPPJID jidWithString:ROOM_JID]; xmppRoom = [[XMPPRoom alloc] initW ...