Activity的fragment间的切换

转自http://blog.youkuaiyun.com/gsw333/article/details/51858524


使用过app的人都知道,几乎所有的app都用到fragment进行首页分割,还有的加上了Indicator或者Tablayout或者Viewpager之类的结合使用。总之,fragment是必须掌握的知识,因为在app中使用率几乎高达100%了。对于fragment的使用就我所知就我会的只有两种吧,一种是add方式来进行show和add,这种方式你切换fragment不会让fragment重新刷新,而用replace方式会使fragment重新刷新,因为add方式是将fragment隐藏了而不是销毁再创建,replace方式每次都是重新创建。具体我们还是看代码来说吧,这种基础的东西看看代码就懂了。

我们做个QQ首页那种,3个fragment,首先,我们需要一个首页MainActivity和三个Fragment以及他们的布局。


首页MainActivity布局文件:

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:background="#ffffff"  
  5.     android:layout_width="match_parent"  
  6.     android:layout_height="match_parent">  
  7.   
  8.     <FrameLayout  
  9.         android:id="@+id/main_frame_layout"  
  10.         android:background="#dddddd"  
  11.         android:layout_weight="1"  
  12.         android:layout_width="match_parent"  
  13.         android:layout_height="0dp">  
  14.     </FrameLayout>  
  15.   
  16.     <LinearLayout  
  17.         android:layout_width="match_parent"  
  18.         android:layout_height="48dp">  
  19.         <Button  
  20.             android:layout_weight="1"  
  21.             android:layout_width="0dp"  
  22.             android:layout_height="match_parent"  
  23.             android:background="#ffffff"  
  24.             android:text="消息"/>  
  25.         <Button  
  26.             android:layout_weight="1"  
  27.             android:layout_width="0dp"  
  28.             android:layout_height="match_parent"  
  29.             android:background="#ffffff"  
  30.             android:text="联系人"/>  
  31.         <Button  
  32.             android:layout_weight="1"  
  33.             android:layout_width="0dp"  
  34.             android:layout_height="match_parent"  
  35.             android:background="#ffffff"  
  36.             android:text="动态"/>  
  37.     </LinearLayout>  
  38.   
  39. </LinearLayout>  
布局的下方放三个按钮,中间用Framelayout显示不同的内容用于存放fragment。布局效果如下:


其中灰色的部分就是Framelayout用于显示fragment的。


然后现在建立我们的Fragment的布局文件,代码如下:

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:background="#dddddd">  
  6.   
  7.     <TextView  
  8.         android:id="@+id/fragment_tv"  
  9.         android:layout_centerInParent="true"  
  10.         android:layout_width="wrap_content"  
  11.         android:layout_height="wrap_content"  
  12.         android:textColor="#000000"/>  
  13.   
  14. </RelativeLayout>  

TextView用于显示我们当前是第几个fragment


然后Fragment逻辑实现代码:

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package com.xjj.demo.fragment;  
  2.   
  3. import android.os.Bundle;  
  4. import android.support.v4.app.Fragment;  
  5. import android.view.LayoutInflater;  
  6. import android.view.View;  
  7. import android.view.ViewGroup;  
  8. import android.widget.TextView;  
  9.   
  10. /**  
  11.  * Created by Administrator on 2016/7/8.  
  12.  */  
  13. public class MyFragment extends Fragment {  
  14.   
  15.     private TextView tv;  
  16.     private String name;  
  17.   
  18.     public MyFragment(String fName){  
  19.         this.name = fName;  
  20.     }  
  21.   
  22.     @Override  
  23.     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {  
  24.         View view = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_my,container,false);  
  25.         tv = (TextView) view.findViewById(R.id.fragment_tv);  
  26.         tv.setText(name);  
  27.         tv.setOnClickListener(new View.OnClickListener() {  
  28.             @Override  
  29.             public void onClick(View v) {  
  30.                 tv.setText("我变了-" + name);  
  31.             }  
  32.         });  
  33.         return view;  
  34.     }  
  35. }  
当点击fragment中的文字时,文字会发生改变。


为了方便,我们创建三个Fragment都用这个的实例,传个name过去表示是哪个fragment。后面看代码就懂了。

然后我们就开始进入MainActivity逻辑开始设置Fragment的显示。

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package com.xjj.demo.fragment;  
  2.   
  3. import android.support.v4.app.FragmentTransaction;  
  4. import android.support.v7.app.AppCompatActivity;  
  5. import android.os.Bundle;  
  6. import android.view.View;  
  7. import android.widget.Button;  
  8.   
  9. public class MainActivity extends AppCompatActivity implements View.OnClickListener{  
  10.     //三个fragment  
  11.     private MyFragment f1;  
  12.     private MyFragment f2;  
  13.     private MyFragment f3;  
  14.   
  15.     //底部三个按钮  
  16.     private Button foot1;  
  17.     private Button foot2;  
  18.     private Button foot3;  
  19.   
  20.     @Override  
  21.     protected void onCreate(Bundle savedInstanceState) {  
  22.         super.onCreate(savedInstanceState);  
  23.         setContentView(R.layout.activity_main);  
  24.   
  25.         foot1 = (Button) findViewById(R.id.btn1);  
  26.         foot2 = (Button) findViewById(R.id.btn2);  
  27.         foot3 = (Button) findViewById(R.id.btn3);  
  28.         foot1.setOnClickListener(this);  
  29.         foot2.setOnClickListener(this);  
  30.         foot3.setOnClickListener(this);  
  31.   
  32.         //第一次初始化首页默认显示第一个fragment  
  33.         initFragment1();  
  34.     }  
  35.   
  36.     //显示第一个fragment  
  37.     private void initFragment1(){  
  38.         //开启事务,fragment的控制是由事务来实现的  
  39.         FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();  
  40.   
  41.         //第一种方式(add),初始化fragment并添加到事务中,如果为null就new一个  
  42.         if(f1 == null){  
  43.             f1 = new MyFragment("消息");  
  44.             transaction.add(R.id.main_frame_layout, f1);  
  45.         }  
  46.         //隐藏所有fragment  
  47.         hideFragment(transaction);  
  48.         //显示需要显示的fragment  
  49.         transaction.show(f1);  
  50.   
  51.         //第二种方式(replace),初始化fragment  
  52. //        if(f1 == null){  
  53. //            f1 = new MyFragment("消息");  
  54. //        }  
  55. //        transaction.replace(R.id.main_frame_layout, f1);  
  56.   
  57.         //提交事务  
  58.         transaction.commit();  
  59.     }  
  60.   
  61.     //显示第二个fragment  
  62.     private void initFragment2(){  
  63.         FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();  
  64.   
  65.         if(f2 == null){  
  66.             f2 = new MyFragment("联系人");  
  67.             transaction.add(R.id.main_frame_layout,f2);  
  68.         }  
  69.         hideFragment(transaction);  
  70.         transaction.show(f2);  
  71.   
  72. //        if(f2 == null) {  
  73. //            f2 = new MyFragment("联系人");  
  74. //        }  
  75. //        transaction.replace(R.id.main_frame_layout, f2);  
  76.   
  77.         transaction.commit();  
  78.     }  
  79.   
  80.     //显示第三个fragment  
  81.     private void initFragment3(){  
  82.         FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();  
  83.   
  84.         if(f3 == null){  
  85.             f3 = new MyFragment("动态");  
  86.             transaction.add(R.id.main_frame_layout,f3);  
  87.         }  
  88.         hideFragment(transaction);  
  89.         transaction.show(f3);  
  90.   
  91. //        if(f3 == null) {  
  92. //            f3 = new MyFragment("动态");  
  93. //        }  
  94. //        transaction.replace(R.id.main_frame_layout, f3);  
  95.   
  96.         transaction.commit();  
  97.     }  
  98.   
  99.     //隐藏所有的fragment  
  100.     private void hideFragment(FragmentTransaction transaction){  
  101.         if(f1 != null){  
  102.             transaction.hide(f1);  
  103.         }  
  104.         if(f2 != null){  
  105.             transaction.hide(f2);  
  106.         }  
  107.         if(f3 != null){  
  108.             transaction.hide(f3);  
  109.         }  
  110.     }  
  111.   
  112.   
  113.     @Override  
  114.     public void onClick(View v) {  
  115.         if(v == foot1){  
  116.             initFragment1();  
  117.         }else if(v == foot2){  
  118.             initFragment2();  
  119.         }else if(v == foot3){  
  120.             initFragment3();  
  121.         }  
  122.     }  
  123. }  

代码中已经写的很清楚了,我就不多做解释了,代码中注释掉了replace方式,用的是add方式然后进行hide和show,他的效果是下图所示的:


上面这个gif图看清楚,我们点击下面三个按钮后切换成功,而且我们点击fragment里面的文字改变后,我们切换到其他fragment再切换回来时,发现他的文字是保持点击后改变的文字,而不是初始化的文字,比如消息fragment显示的是“消息”,我们点击后变成了:“我变了-消息”,当我们点击其他fragment后再点击消息fragment回来时,他显示的依然是“我变了-消息”,而不是初始的消息“”,这说明我们切换回来的时候fragment没有被重新创建,而是保持之前的那个fragment。这就是使用add的hide和show的方式的效果,大部分app都是采用这种方式。


下面我们用replace方式,就是注释掉add方式,使用replace方式就行了,他的效果是:


相信已经看出来了,用replace的效果就是,每次都会重新初始化fragment,比如在消息fragment点击“消息”,变成了“我变了-消息”,此时跳到联系人fragment再跳回消息fragment,发现消息fragment显示的“消息”而不是“我变了-消息”,没有保持消息fragment之前的状态。

所以用add方式实现fragment的效果就是:切换fragment时不会重新创建,是什么样子切换回来还是什么样子;用replace的效果就是:切换fragment时每次都会重新创建初始化。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值