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 ...
随机推荐
- 国内使用pip / pip with GFW / pip 镜像
sudo pip install -i https://pypi.doubanio.com/simple/ YOUR_PACKAGE_NAME --trusted-host pypi.doubanio ...
- LN : leetcode 399 Evaluate Division
lc 399 Evaluate Division 399 Evaluate Division Equations are given in the format A / B = k, where A ...
- MVC之参数验证(一)
ASP.NET MVC采用Model绑定为目标Action生成了相应的参数列表,但是在真正执行目标Action方法之前,还需要对绑定的参数实施验证以确保其数据的准确性.总地来说,我们可以采用Syste ...
- Angular JS中自定义标签 属性绑定的解释
看到自定义标签的文档时,文档作者解释的能力实在太弱,也可能是本人太笨,一下绕不过来. 看了一个stackoverflow答案,才算明白,在此贴出翻译,以供大家参考. .csharpcode, .csh ...
- Paxos,Raft,Zab一致性协议-Raft篇
Raft是一个一致性算法,旨在易于理解.它提供了Paxos的容错和性能.不同之处在于它被分解为相对独立的子问题,它清楚地解决了实际系统所需的所有主要部分.我们希望Raft能够为更广泛的受众提供共识,并 ...
- 93. [NOIP2001] 数的划分
问题描述 将整数n分成k份,且每份不能为空,任意两种方案不能相同(不考虑顺序). 例如:n=7,k=3,下面三种分法被认为是相同的. 1,1,5; 1,5,1; 5,1,1; 问有多少种不同的分法. ...
- <mybatis:scan>与<MapperScannerConfigurer/>
使用Mybatis作为持久层的框架,对dao层的bean对象的注解扫描有两种方式:<mybatis:san>.<MapperScannerConfigurer> 一:<m ...
- Qt 杂记——QTableWidget列表添加、删除(备份)
1.列表的添加 需求:向一个有两列的Table中添加一条数据 思路:新建一个inputDialog,通过按钮打开Qt自带的inputDialog,传递回输入的数据,再添加到列表中 界面: 代码: in ...
- chr()返回值是当前整数对应的 ASCII 字符。
#chr() 用一个范围在 range(256)内的(就是0-255)整数作参数,返回一个对应的字符.#返回值是当前整数对应的 ASCII 字符.1 import random input_m =10 ...
- 服务器 获取用户 真实ip
在有代理的情况下,因为要代替客户端去访问服务器,所以,当请求包经过反向代理后,在代理服务器这里这个IP数据包的IP包头做了修改,最终后端WEB服务器得到的数据包的头部源IP地址是代理服务器的IP地址. ...