由于看客的要求,我就把读者所要的写出来。

  由于上一篇是每一个Fragment 实例了同一个layout.xml ,造成了读者的困惑,这篇我就让每一个Fragment 加载一个不同的layout.xml.

  首先我们要准备四个layout.xml ,用来给每个点击跳转页面。

  第一个fg_contnet.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:background="@color/cornsilk"
  6. android:orientation="vertical" >
  7.  
  8. <TextView
  9. android:id="@+id/txt_content"
  10. android:layout_width="match_parent"
  11. android:layout_height="match_parent"
  12. android:gravity="center"
  13. android:text="第一个"
  14. android:textColor="@android:color/holo_green_light"
  15. android:textSize="20sp"/>
  16.  
  17. </LinearLayout>

  第二个fg_contnet2.xml :

  

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:background="@color/cornsilk"
  6. android:gravity="center_vertical"
  7. android:orientation="vertical" >
  8.  
  9. <TextView
  10. android:id="@+id/txt_content"
  11. android:layout_width="match_parent"
  12. android:layout_height="wrap_content"
  13. android:gravity="center"
  14. android:text="第二个"
  15. android:textColor="@android:color/holo_green_light"
  16. android:textSize="20sp"/>
  17. <Button
  18. android:layout_width="match_parent"
  19. android:layout_height="wrap_content"
  20. android:text="Load。。。。"
  21. />
  22. </LinearLayout>

  第三个 fg_content3.xml :

  

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:background="@color/cornsilk"
  6. android:orientation="vertical" >
  7.  
  8. <ImageView
  9. android:layout_width="match_parent"
  10. android:layout_height="wrap_content"
  11. android:src="@drawable/ic_launcher"
  12. />
  13.  
  14. </LinearLayout>

  第四个 fg_content4.xml :  

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:background="@color/cornsilk"
  6. android:orientation="vertical" >
  7.  
  8. <TimePicker
  9. android:id="@+id/timePicker1"
  10. android:layout_width="wrap_content"
  11. android:layout_height="wrap_content" />
  12.  
  13. </LinearLayout>

  因为要每个Fragemnt 加载一个layout.xml ,所以MyFragment 也要修改:

  我们在构造器中传入一个content_layout.xml ,动态改变FragmentLayout.

  1. import android.app.Fragment;
  2. import android.os.Bundle;
  3. import android.view.LayoutInflater;
  4. import android.view.View;
  5. import android.view.ViewGroup;
  6.  
  7. public class MyFragment extends Fragment{
  8. private int content_layout;
  9. public MyFragment(int content) {
  10. this.content_layout = content;
  11. }
  12. @Override
  13. public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  14. View view = inflater.inflate(content_layout,container,false);
  15. return view;
  16. }
  17. }

  最后在MainActivity 中:

  

  1. import android.app.Activity;
  2. import android.app.FragmentManager;
  3. import android.app.FragmentTransaction;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.view.Window;
  7. import android.widget.TextView;
  8.  
  9. public class MainActivity extends Activity implements View.OnClickListener{
  10.  
  11. //UI Object
  12. private TextView txt_channel;
  13. private TextView txt_message;
  14. private TextView txt_better;
  15. private TextView txt_setting;
  16.  
  17. //Fragment Object
  18. private MyFragment fg1,fg2,fg3,fg4;
  19. private FragmentManager fManager;
  20.  
  21. @Override
  22. protected void onCreate(Bundle savedInstanceState) {
  23. super.onCreate(savedInstanceState);
  24. requestWindowFeature(Window.FEATURE_NO_TITLE);
  25. setContentView(R.layout.activity_main);
  26. fManager = getFragmentManager();
  27. bindViews();
  28. txt_channel.performClick(); //模拟一次点击,既进去后选择第一项
  29. }
  30.  
  31. //UI组件初始化与事件绑定
  32. private void bindViews() {
  33. txt_channel = (TextView) findViewById(R.id.txt_channel);
  34. txt_message = (TextView) findViewById(R.id.txt_message);
  35. txt_better = (TextView) findViewById(R.id.txt_better);
  36. txt_setting = (TextView) findViewById(R.id.txt_setting);
  37.  
  38. txt_channel.setOnClickListener(this);
  39. txt_message.setOnClickListener(this);
  40. txt_better.setOnClickListener(this);
  41. txt_setting.setOnClickListener(this);
  42. }
  43.  
  44. //重置所有文本的选中状态
  45. private void setSelected(){
  46. txt_channel.setSelected(false);
  47. txt_message.setSelected(false);
  48. txt_better.setSelected(false);
  49. txt_setting.setSelected(false);
  50. }
  51.  
  52. //隐藏所有Fragment
  53. private void hideAllFragment(FragmentTransaction fragmentTransaction){
  54. if(fg1 != null)fragmentTransaction.hide(fg1);
  55. if(fg2 != null)fragmentTransaction.hide(fg2);
  56. if(fg3 != null)fragmentTransaction.hide(fg3);
  57. if(fg4 != null)fragmentTransaction.hide(fg4);
  58. }
  59.  
  60. @Override
  61. public void onClick(View v) {
  62. FragmentTransaction fTransaction = fManager.beginTransaction();
  63. hideAllFragment(fTransaction);
  64. switch (v.getId()){
  65. case R.id.txt_channel:
  66. setSelected();
  67. txt_channel.setSelected(true);
  68. if(fg1 == null){
  69. fg1 = new MyFragment(R.layout.fg_content);
  70. fTransaction.add(R.id.ly_content,fg1);
  71. }else{
  72. fTransaction.show(fg1);
  73. }
  74. break;
  75. case R.id.txt_message:
  76. setSelected();
  77. txt_message.setSelected(true);
  78. if(fg2 == null){
  79. fg2 = new MyFragment(R.layout.fg_content2);
  80. fTransaction.add(R.id.ly_content,fg2);
  81. }else{
  82. fTransaction.show(fg2);
  83. }
  84. break;
  85. case R.id.txt_better:
  86. setSelected();
  87. txt_better.setSelected(true);
  88. if(fg3 == null){
  89. fg3 = new MyFragment(R.layout.fg_content3);
  90. fTransaction.add(R.id.ly_content,fg3);
  91. }else{
  92. fTransaction.show(fg3);
  93. }
  94. break;
  95. case R.id.txt_setting:
  96. setSelected();
  97. txt_setting.setSelected(true);
  98. if(fg4 == null){
  99. fg4 = new MyFragment(R.layout.fg_content4);
  100. fTransaction.add(R.id.ly_content,fg4);
  101. }else{
  102. fTransaction.show(fg4);
  103. }
  104. break;
  105. }
  106. fTransaction.commit();
  107. }
  108. }

  最后看一下效果:

谢谢大家的关注~     Can't get is forever, forget was once.     ----到不了的就是永远, 忘不了的就是曾经。

  

Android Fragment (二) 实例2的更多相关文章

  1. Android Fragment (二) 实例1

    千呼万唤始出来,今天就也写一篇Frament 的简单实例.先看效果: 一看这效果,首先我们的配置资源文件:new -->android xml -->selector --> 四个图 ...

  2. Android Fragment 简单实例

    Android上的界面展示都是通过Activity实现的.Activity实在是太经常使用了.我相信大家都已经很熟悉了,这里就不再赘述. 可是Activity也有它的局限性,相同的界面在手机上显示可能 ...

  3. Android fragment (二)

    怎样使用fragment? 1.首先你要确定下你有多少个fragment要使用在一个activity里. 2.依据你的fragment的数量,创建继承自fragment的class.然后依据实际需求重 ...

  4. Android Fragment使用(二) 嵌套Fragments (Nested Fragments) 的使用及常见错误

    嵌套Fragment的使用及常见错误 嵌套Fragments (Nested Fragments), 是在Fragment内部又添加Fragment. 使用时, 主要要依靠宿主Fragment的 ge ...

  5. Android Fragment 实例

    Fragment是Android honeycomb 3.0新增的概念,在Android——Fragment介绍.Android Fragment使用.Android FragmentManage F ...

  6. android viewpager 拿到当前显示的 fragment 的实例

    一个 ViewPager 通过 FragmentPagerAdapter 绑定了 3 个 fragment 可以通过 Fragment fragment = getSupportFragmentMan ...

  7. Android进阶(二十三)Android开发过程之实例讲解

    Android开发过程之实例讲解 前言 回过头来审视之前做过的Android项目,发觉自己重新开发时忽然间不知所措了,间隔了太长时间没有开发导致自己的Android技能知识急剧下降.温故而知新. 废话 ...

  8. Android Fragment使用(三) Activity, Fragment, WebView的状态保存和恢复

    Android中的状态保存和恢复 Android中的状态保存和恢复, 包括Activity和Fragment以及其中View的状态处理. Activity的状态除了其中的View和Fragment的状 ...

  9. Android Fragment使用(一) 基础篇 温故知新

    Fragment使用的基本知识点总结, 包括Fragment的添加, 参数传递和通信, 生命周期和各种操作. Fragment使用基础 Fragment添加 方法一: 布局里的标签 标识符: tag, ...

随机推荐

  1. 底部漂浮DIV

    .buttonDiv{background-color: #4e4f50; border: 2px solid #83ABD3; border-radius: 4px; -khtml-opacity: ...

  2. 设置DataSource后DateGridView不显示的问题

    在一个WinForm小程序中,有两处需要用DataGridView控件显示数据.设置DataGridView.DataSource为数据查询结果后,第一个DataGridView可以正常显示数据,而第 ...

  3. SQLServer字符操作

    1.CHARINDEX('A',‘VALUE’)    result:2 style:PATINDEX(varchar,varchar) 解释:A在字符串VALUE的位置次序. 2.PATINDEX( ...

  4. Apache Commons BeanUtils

    http://commons.apache.org/proper/commons-beanutils/javadocs/v1.9.2/apidocs/org/apache/commons/beanut ...

  5. 0.读书笔记之The major advancements in Deep Learning in 2016

    The major advancements in Deep Learning in 2016 地址:https://tryolabs.com/blog/2016/12/06/major-advanc ...

  6. Luogu 魔法学院杯-第二弹(萌新的第一法blog)

    虽然有点久远  还是放一下吧. 传送门:https://www.luogu.org/contest/show?tid=754 第一题  沉迷游戏,伤感情 #include <queue> ...

  7. 学习PYTHON之路, DAY 8 - PYTHON 基础 8 (面向对象进阶)

    类的成员 类的成员可以分为三大类:字段.方法和属性 注:所有成员中,只有普通字段的内容保存对象中,即:根据此类创建了多少对象,在内存中就有多少个普通字段.而其他的成员,则都是保存在类中,即:无论对象的 ...

  8. php案列4

    一个最简单的利用php生成随机数或者随机字符串的函数.$chars变量中的字符自己修改就能达到数字或者字符串的目的     $len表示长度,代码如下: 复制代码 代码如下: /** * 产生随机字符 ...

  9. c# 判断当前时间是否在 工作日时间段内

    #region //获取当前周几 private string _strWorkingDayAM = "08:30";//工作时间上午08:00 private string _s ...

  10. C#中REF和OUT的区别

    在C# 中,既可以通过值也可以通过引用传递参数.通过引用传递参数允许函数成员更改参数的值,并保持该更改.若要通过引用传递参数, 可使用ref或out关键字.ref和out这两个关键字都能够提供相似的功 ...