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

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

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

  第一个fg_contnet.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/cornsilk"
android:orientation="vertical" > <TextView
android:id="@+id/txt_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="第一个"
android:textColor="@android:color/holo_green_light"
android:textSize="20sp"/> </LinearLayout>

  第二个fg_contnet2.xml :

  

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/cornsilk"
android:gravity="center_vertical"
android:orientation="vertical" > <TextView
android:id="@+id/txt_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="第二个"
android:textColor="@android:color/holo_green_light"
android:textSize="20sp"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Load。。。。"
/>
</LinearLayout>

  第三个 fg_content3.xml :

  

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/cornsilk"
android:orientation="vertical" > <ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"
/> </LinearLayout>

  第四个 fg_content4.xml :  

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/cornsilk"
android:orientation="vertical" > <TimePicker
android:id="@+id/timePicker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" /> </LinearLayout>

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

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

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup; public class MyFragment extends Fragment{
private int content_layout;
public MyFragment(int content) {
this.content_layout = content;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(content_layout,container,false);
return view;
}
}

  最后在MainActivity 中:

  

import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.TextView; public class MainActivity extends Activity implements View.OnClickListener{ //UI Object
private TextView txt_channel;
private TextView txt_message;
private TextView txt_better;
private TextView txt_setting; //Fragment Object
private MyFragment fg1,fg2,fg3,fg4;
private FragmentManager fManager; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
fManager = getFragmentManager();
bindViews();
txt_channel.performClick(); //模拟一次点击,既进去后选择第一项
} //UI组件初始化与事件绑定
private void bindViews() {
txt_channel = (TextView) findViewById(R.id.txt_channel);
txt_message = (TextView) findViewById(R.id.txt_message);
txt_better = (TextView) findViewById(R.id.txt_better);
txt_setting = (TextView) findViewById(R.id.txt_setting); txt_channel.setOnClickListener(this);
txt_message.setOnClickListener(this);
txt_better.setOnClickListener(this);
txt_setting.setOnClickListener(this);
} //重置所有文本的选中状态
private void setSelected(){
txt_channel.setSelected(false);
txt_message.setSelected(false);
txt_better.setSelected(false);
txt_setting.setSelected(false);
} //隐藏所有Fragment
private void hideAllFragment(FragmentTransaction fragmentTransaction){
if(fg1 != null)fragmentTransaction.hide(fg1);
if(fg2 != null)fragmentTransaction.hide(fg2);
if(fg3 != null)fragmentTransaction.hide(fg3);
if(fg4 != null)fragmentTransaction.hide(fg4);
} @Override
public void onClick(View v) {
FragmentTransaction fTransaction = fManager.beginTransaction();
hideAllFragment(fTransaction);
switch (v.getId()){
case R.id.txt_channel:
setSelected();
txt_channel.setSelected(true);
if(fg1 == null){
fg1 = new MyFragment(R.layout.fg_content);
fTransaction.add(R.id.ly_content,fg1);
}else{
fTransaction.show(fg1);
}
break;
case R.id.txt_message:
setSelected();
txt_message.setSelected(true);
if(fg2 == null){
fg2 = new MyFragment(R.layout.fg_content2);
fTransaction.add(R.id.ly_content,fg2);
}else{
fTransaction.show(fg2);
}
break;
case R.id.txt_better:
setSelected();
txt_better.setSelected(true);
if(fg3 == null){
fg3 = new MyFragment(R.layout.fg_content3);
fTransaction.add(R.id.ly_content,fg3);
}else{
fTransaction.show(fg3);
}
break;
case R.id.txt_setting:
setSelected();
txt_setting.setSelected(true);
if(fg4 == null){
fg4 = new MyFragment(R.layout.fg_content4);
fTransaction.add(R.id.ly_content,fg4);
}else{
fTransaction.show(fg4);
}
break;
}
fTransaction.commit();
}
}

  最后看一下效果:

谢谢大家的关注~     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. Evolutionary Computing: Assignments

    Assignment 1: TSP Travel Salesman Problem Assignment 2: TTP Travel Thief Problem The goal is to find ...

  2. 第二章 NIO入门

    传统的同步阻塞式I/O编程 基于NIO的非阻塞编程 基于NIO2.0的异步非阻塞(AIO)编程 为什么要使用NIO编程 为什么选择Netty 第二章 NIO 入门 2.1 传统的BIO编程 2.1.1 ...

  3. SPSS数据分析—多维尺度分析

    在市场研究中,有一种分析是研究消费者态度或偏好,收集的数据是某些对象的评分数据,这些评分数据可以看做是对象间相似性或差异性的表现,也就是一种距离,距离近的差异性小,距离远的差异性大.而我们的分析目的也 ...

  4. ionic实现双击返回键退出功能

    实现这个功能需要四个步骤: 步骤一: 说明:因为需要和手机的硬件(返回按钮)打交道,而ionic本身是不具备该功能的,但是有一个东西可以:ng-cordova插件,这个插件是phoneGap为了能让i ...

  5. 如何在VISIO 2010/2013 中关闭Shape protection(图形保护)

    最近在画UML图,用到MS visio 2010, 在使用一些网络查找到的图形的时候发现无法编辑,在网上找了找,翻译了下. Visio 2013 的图形保护功能,可以锁定图形的某些特定属性,使其无法被 ...

  6. nginx的ngx_http_request_t结构体

    struct ngx_http_request_s { uint32_t signature; /* "HTTP" */ //请求对应的客户端连接 ngx_connection_t ...

  7. Android开发环境搭建(jdk+eclip+android sdk)

    在开启Android 开发之旅之前,首先要把准备工作做好---搭建开发环境 一.环境搭建: 1.Java JDK 安装 2.Eclipse 安装 3.Android SDK 安装 4.ADT安装 5. ...

  8. C++ 多态、虚函数机制以及虚函数表

    1.非virtual函数,调用规则取决于对象的显式类型.例如 A* a  = new B(); a->display(); 调用的就是A类中定义的display().和对象本体是B无关系. 2. ...

  9. C# WebForm内置对象2+Repeater的Command

    内置对象:用于页面之间的数据交互 为什么要使用这么内置对象?因为HTTP的无状态性. Session:在计算机中,尤其是在网络应用中,称为“会话控制”.Session 对象存储特定用户会话所需的属性及 ...

  10. applicationContext配置文件中的属性说明

    lazy-init:设置只对scop属性为singleton的bean起作用. 1.true:延迟加载:这时在第一次向容器通过getBean索取bean时实例化的. 2.false:表示spring启 ...