首先写好每个Fragment:

1.在第一个Fragment写一个按钮,使其加载下一个Fragment

布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="horizontal" > <Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="加载"/> </LinearLayout>

java代码:

public class LeftFragment extends Fragment{

    OnClickButton mCallback;
//定义一个接口
public interface OnClickButton{
//并实现一个方法,用来传值并在(onAttach()中绑定activity)
public void onClickB();
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//绑定布局文件并获取到里面的控件,特别 注意里面的 view
View view = inflater.inflate(R.layout.fragment_left,null);
Button button = (Button) view.findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mCallback.onClickB();
}
});
return view;
} /**
* 绑定到activity
* @param activity
*/
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mCallback = (OnClickButton) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnHeadlineSelectedListener");
}
}
}

加载显示出来的布局文件:

<?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:orientation="vertical" > <TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="新闻内容" /> <Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" /> </LinearLayout>

java文件:

public class RightFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_right, null);
Button button = (Button) view.findViewById(R.id.button2);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//getActivity() 获取父类的activity
Toast.makeText(getActivity(), "我是fragment", Toast.LENGTH_SHORT).show();
}
});
return view;
}
}

主类:

布局

给Fragment创建一个容器activity_main.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

注意:一定要写明id。

然后就在activity中实现Fragment add进去就行了!

//实现LeftFragment中定义的接口,主要用来传值或者按钮点击事件
public class MainActivity extends Activity implements LeftFragment.OnClickButton {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); //初始化第一个Fragment
if (findViewById(R.id.fragment_container) != null){
if (savedInstanceState != null) {
return;
}
LeftFragment leftFragment = new LeftFragment();
leftFragment.setArguments(getIntent().getExtras());
getFragmentManager().beginTransaction().add(R.id.fragment_container,leftFragment).commit();
}
} /**
* 实现接口中的方法和点击按钮后加载的fragment
*/
@Override
public void onClickB() {
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
RightFragment rightFragment = new RightFragment();
transaction.replace(R.id.fragment_container, rightFragment);
transaction.addToBackStack(null);
transaction.commit();
}
}

这样就实现了一个很小的demo!

动态添加Fragment


首先新建两个fragment的布局文件

fragment1

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00ff00" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is fragment 1"
android:textColor="#000000"
android:textSize="25sp" /> </LinearLayout>

fragment2

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#0000ff" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is fragment 2"
android:textColor="#000000"
android:textSize="25sp" /> </LinearLayout>

新建两个Fragment类继承Fragment

Fragment1

public class Fragmet1 extends Fragment {

    @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment1,container,false);
}
}

Fragment2

public class Fragmet2 extends Fragment {

    @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment2,container,false);
}
}

然后定义一个显示fragment的mainactivity.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:baselineAligned="false" > <Button
android:id="@+id/btn_show_fragment1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="显示Fragment1"/> <Button
android:id="@+id/btn_show_fragment2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="显示Fragment2"/> <FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"/> </LinearLayout>

其中FrameLayout是用来显示Fragment的,在MainActivity中实现

public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity); Button button1 = (Button) findViewById(R.id.btn_show_fragment1);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//动态添加Fragment
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction ft = manager.beginTransaction();
Fragmet1 fragmet1 = new Fragmet1();
ft.add(R.id.fragment_container,fragmet1);
ft.commit();
}
});
Button button2 = (Button) findViewById(R.id.btn_show_fragment2);
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction ft = manager.beginTransaction();
Fragmet2 fragmet2 = new Fragmet2();
ft.add(R.id.fragment_container,fragmet2);
ft.commit();
}
});
}
}

ok 基本就完成了。

注意:

动态添加Fragment主要分为4步:

    • 1.获取到FragmentManager,在V4包中通过getSupportFragmentManager,在系统中原生的Fragment是通过getFragmentManager获得的。
    • 2.开启一个事务,通过调用beginTransaction方法开启。
    • 3.向容器内加入Fragment,一般使用add或者replace方法实现,需要传入容器的id和Fragment的实例。
    • 4.提交事务,调用commit方法提交。

使用Fragment 实现动态UI 和 动态添加Fragment的更多相关文章

  1. Android - 用Fragments实现动态UI - 创建Fragment

    你可以把fragment当作activity中的一个活动模块,它有自己的生命周期,自己接收输入消息,可以在activity运行的时候添加和删除(就像可以在其他activity中重用的"子ac ...

  2. Android Fragment用法详解(2)--动态添加Fragment

    在上一篇文章<Android Fragment用法详解(1)--静态使用Fragment>我们讲解了Fragment的最简单的用法.这次我们来说一说Fragment复杂一丢丢的用法.在代码 ...

  3. 9) 十分钟学会android--使用Fragment建立动态UI

    为了在 Android 上为用户提供动态的.多窗口的交互体验,需要将 UI 组件和 Activity 操作封装成模块进行使用,这样我们就可以在 Activity 中对这些模块进行切入切出操作.可以用  ...

  4. Android学习路径(22)应用Fragment建立动态UI——构建一个灵活UI

    当你设计你的应用来支持多个屏幕尺寸.你能够基于可用的屏幕空间通过在不同的布局上重用fragment来优化用户体验. 比如,在一个手机上.使用单面板(一次仅仅显示一个fragment)的用户体验更加合适 ...

  5. Android学习路径(23)应用Fragment建立动态UI——Fragment之间的通信

    为了要重用Fragment的UI组件.你应该为它们每个都构建一个完整独立的,模块化的组件来定义他自身的布局和行为. 一旦你定义了这些可重用的Fragments.你能够通过activity关联它们同一时 ...

  6. Android - 用Fragments实现动态UI - 创建灵活的UI

    当设计程序来支持各种不一样的屏幕尺寸时,可以在不同的布局中重用fragment来根据可用的屏幕大小来优化用户体验. 例如,在手机上可能使用一个fragment来使用单窗口用户体验比较合适.但是,你可能 ...

  7. Android - 用Fragments实现动态UI - 使用Android Support Library

    Android Support Library提供了一个带有API库的JAR文件来让你可以在使用最新的Android API的同时也也已在早期版本的Android上运行.例如,Support Libr ...

  8. Fragment碎片的创建和动态更新

    Fragment,在平板应用中较为参见,把视图分为两个甚至多个模块. 一,一个简单的fragment 1.创建两个局部文件,用于等待被调用 (1)left_fragment (2)right_frag ...

  9. 【ASP.NET Web API教程】2.3.5 用Knockout.js创建动态UI

    原文:[ASP.NET Web API教程]2.3.5 用Knockout.js创建动态UI 注:本文是[ASP.NET Web API系列教程]的一部分,如果您是第一次看本博客文章,请先看前面的内容 ...

随机推荐

  1. Kafka的配置文件详细描述

    在kafka/config/目录下面有3个配置文件: producer.properties consumer.properties server.properties (1).producer.pr ...

  2. HMM隐马尔科夫模型

    这是一个非常重要的模型,凡是学统计学.机器学习.数据挖掘的人都应该彻底搞懂. python包: hmmlearn 0.2.0 https://github.com/hmmlearn/hmmlearn ...

  3. vitamio 缓冲一部分时,loading还没消失,直接点击播放,loading未能消失

    在videoView的start()中 添加loading消失语句,效果很好 ,也没有出现任何问题

  4. 抛弃优启Grub4dos和PE大多数时间可以这样用

    在能够进入Windows的情况下,Grub4dos和PE大多数时间可以这样用 http://files.cnblogs.com/files/liuzhaoyzz/boot_moban.rar Grub ...

  5. 企业用户选择Java多于.NET的 5个原因

    .NET 和 Java 是当今社会最受欢迎的两种编程语言, 长期的发展和强大的功能使他们足以在编程界立足. 十余年的争论也没得到结果的一个话题就是——他们哪个更好一些? 今天, 我们不再去讨论JAVA ...

  6. 非常好的Java反射例子

    1.Java反射的概念 反射含义:可以获取正在运行的Java对象. 2.Java反射的功能 1)可以判断运行时对象所属的类 2)可以判断运行时对象所具有的成员变量和方法 3)通过反射甚至可以调用到pr ...

  7. IDEA 创建Java Web项目

    发现项目目录没有classes和lib目录,所以自己创建 点击OK,选中"Jar Directroy"-->点击"OK" 然后直接把jar复制到这个目录下 ...

  8. Oracle后台进程

    后台进程简介 启动例程时,Oracle不仅会分配SGA,还会启动后台进程:关闭例程时,Oracle不仅会释放SGA所占用的内存空间,而且还会释放后台进程所占用的Cpu和内存资源.Oracle提供了很多 ...

  9. (转载整理)SAP ERP常用表

    abap 常用表 GL部分:FAGLFLEXT(FMGLFLEXT)   新总账汇总表 GLT0        旧总帐汇总表           SKA1        总账科目主记录 (科目表)   ...

  10. 用JavaScript操作Media Queries

    在响应式(或自适应)设计中要用到Media Queries这个CSS属性,但在某些时候我们需要对Media Queries进行动态操作,这时候可以使用Javascript. 如以下Media Quer ...