fragment动态加载
来源:互联网 发布:万网查询域名备案号 编辑:程序博客网 时间:2024/06/06 04:27
上一篇博客讲了fragment静态加载,现在来讲讲动态加载。
想要实现的效果是一个Activity中有两个button,下方是一个FrameLayout;当点击button,就会促发相应事件,并将对应的fragment页面加载到FrameLayout中。如下图所示:
MainActivity.java:
【注意】FragmentTransaction不要设置为全局变量,否则,其commit()只能使用一次,如果再被调用则会出现 java.lang.IllegalStateException: commit already called的异常;
动态加载的5个步骤:
1.通过getFragmentManager()方法FragmentManager 对象;
2.通过fragmentManager.beginTransaction()获取FragmentTransaction对象;
3.创建一个MyFragmentone对象;
4.通过fragmentTransaction.replace(加载fragment页面的控件的id(即“容器”),MyFragmentone对象);
5.通过fragmentTransaction.commit()方法提交。
package activity.wyc.com.framentdemoone;import android.app.FragmentManager;import android.app.FragmentTransaction;import android.os.Bundle;import android.support.v7.app.ActionBarActivity;import android.view.View;import android.widget.Button;import com.wyc.fragment.MyFragmentone;import com.wyc.fragment.MyFragmenttwo;public class MainActivity extends ActionBarActivity { private Button btn01Obj,btn02Obj; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn01Obj = (Button)findViewById(R.id.btn01id); btn01Obj.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { FragmentManager fragmentManager = getFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); MyFragmentone fragment01 = new MyFragmentone(); fragmentTransaction.replace(R.id.flid, fragment01); fragmentTransaction.commit(); } }); btn02Obj = (Button)findViewById(R.id.btn02id); btn02Obj.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { FragmentManager fragmentManager = getFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); MyFragmenttwo fragment02 = new MyFragmenttwo(); fragmentTransaction.replace(R.id.flid,fragment02); fragmentTransaction.commit(); } }); }}
MyFragmentone.java:
动态加载布局文件(和fragment静态加载一样)
package com.wyc.fragment;import android.app.Fragment;import android.os.Bundle;import android.support.annotation.Nullable;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import activity.wyc.com.framentdemoone.R;/** * Created by yc on 2015/3/4. */public class MyFragmentone extends Fragment { @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment01,null); return view; }}
MyFragmenttwo.java
package com.wyc.fragment;import android.app.Fragment;import android.os.Bundle;import android.support.annotation.Nullable;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import activity.wyc.com.framentdemoone.R;/** * Created by yc on 2015/3/4. */public class MyFragmenttwo extends Fragment { @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment02,null); return view; }}
activity_main.xml:
使用fragment动态加载可以不需要写fragment标签了o(^▽^)o
<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" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" android:orientation="vertical" tools:context=".MainActivity"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="加载第一个fragment页面" android:textSize="20sp" android:id="@+id/btn01id" android:layout_weight="1"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="加载第二个fragment页面" android:textSize="20sp" android:id="@+id/btn02id" android:layout_weight="1"/> </LinearLayout> <FrameLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/flid"></FrameLayout></LinearLayout>
fragment01.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:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="这是第一个fragment页面" android:textSize="20sp" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/red" android:id="@+id/fm01imgid" /></LinearLayout>
fragment02.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:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="这是第二个fragment页面" android:textSize="20sp" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/blue" android:id="@+id/fm02imgid" /></LinearLayout>
1 1
- fragment动态加载
- Fragment的动态加载
- fragment 动态加载,删除
- Fragment动态加载
- 动态加载Fragment
- 动态加载Fragment
- 仿微信 Fragment 动态加载
- 动态加载fragment
- 动态加载Fragment
- 动态加载fragment
- Fragment动态加载
- 动态加载Fragment
- Fragment 动态加载
- 动态加载Fragment
- Android_动态加载Fragment
- Fragment 动态加载 / 静态加载
- Fragment:碎片的动态加载
- Fragment的动态加载问题
- Hook api! 如何拦截系统api, 让它做你想做的事!
- POJ 2527 : Polynomial Remains - 多项式除法
- 从零开始学C++之对象的使用(三):static 与单例模式、auto_ptr与单例模式、const 用法小结、mutable修饰符
- 在iPhone上执行后台任务
- vijos1112小胖的奇偶(并查集,区间转两端线段)
- fragment动态加载
- 从零开始学C++之数据封装与抽象:分别用C和C++来实现一个链栈
- 观察者模式
- c++智能指针
- 从零开始学C++之友元:友元函数和友元类
- Sublime Text,Latex和Skim on Mac(支持中文)
- 航班管家向怡宁:如何做APP界面设计
- 从零开始学C++之运算符重载(一):以成员函数方式重载、以友元函数方式重载
- symfony2 入门一瞥