[Android学习系列9]关于Fragment的一些事
在界面上加入fragment?
方法1.用fragmentManager 和 fragmentTransaction 把fragment add上去
方法2.用viewpager 绑定一个 对应fragment的适配器
Fragment动画过渡效果
http://blog.csdn.net/mayingcai1987/article/details/6341608
http://www.cnblogs.com/lcyty/archive/2013/10/23/3383960.html
Android基础之使用Fragment控制切换多个页面
http://www.jb51.net/article/40108.htm
fragment 和 activity 的生命周期对比
http://blog.csdn.net/think_soft/article/details/7272869
理解fragment事务的栈
在事务操作里每一次操作fragment后 addToBackStack( "tag名" ) 把当前操作状态压入栈里
popBackStack() 可以弹出一个状态
popBackStack("tag名",0) 可以弹栈一直弹到栈顶为"tag名"的那个状态
popBackStack("tag名",FragmentManager.POP_BACK_STACK_INCLUSIVE) 可以弹栈一直弹到"tag名"前的那个状态,(也就是把"tag名")弹出去
作用:
用户按下后退键后,将会返回栈里的前一个状态,而不是直接关闭当前fragment所在的activity
参考资料:
android Fragments详解一:概述
android Fragments详解二:创建Fragment
android Fragments详解三:实现Fragment的界面
android Fragments详解四:管理fragment
android Fragments详解五:与activity通讯
android Fragments详解六:处理fragement的生命周期
android Fragments详解七:fragement示例
附上一个练习代码,有时间再补充说明
package com.example.test_fragment; import android.app.Activity; import android.app.Fragment; import android.app.FragmentManager; import android.app.FragmentTransaction; import android.os.Bundle; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Toast; public class MainActivity extends Activity { FragmentManager fragmentManager; //fragment管理器 FragmentTransaction fragmentTransaction; //事务 Fragment fragmentA; Fragment fragmentB; //各种按钮 Button btA,btB,btRemoveA,btRemoveB,btReplaceA,btReplaceB; Button btAttachA,btDetachA; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //创建fragment管理器 fragmentManager = getFragmentManager(); btA = (Button)findViewById(R.id.button1); btB = (Button)findViewById(R.id.button2); btRemoveA = (Button)findViewById(R.id.button3); btRemoveB = (Button)findViewById(R.id.button4); btReplaceA = (Button)findViewById(R.id.button5); //把A换成B btReplaceB = (Button)findViewById(R.id.button6); //把B换成A btAttachA = (Button)findViewById(R.id.button7); //attach fragmentA btDetachA = (Button)findViewById(R.id.button8); //detach fragmentA initListener(); } private void initListener() { btA.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { addFragmentA(); } }); btB.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { addFragmentB(); } }); btRemoveA.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { removeFramgent("A"); } }); btRemoveB.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { removeFramgent("B"); } }); btReplaceA.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { replaceFragment("A"); } }); btReplaceB.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { replaceFragment("B"); } }); btAttachA.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { attachA(); } }); btDetachA.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { detachA(); } }); } public void addFragmentA() { //先建立好fragment对象 fragmentA = new FragmentA(); //开始事务 fragmentTransaction = fragmentManager.beginTransaction(); //添加fragment //参数1 fragment所在layout的id //参数2 fragment对象 //参数3 用来代表这个fragment 的一个tag 之后可以通过tag来找这个fragment fragmentTransaction.add(R.id.fragmentholder, fragmentA, "A"); //结束事务 fragmentTransaction.commit(); } public void addFragmentB() { fragmentB = new FragmentB(); //开始事务 fragmentTransaction = fragmentManager.beginTransaction(); fragmentTransaction.add(R.id.fragmentholder, fragmentB, "B"); //结束 fragmentTransaction.commit(); } public void removeFramgent(String str) { Fragment deletedFragment = (Fragment)fragmentManager.findFragmentByTag(str); if(deletedFragment != null) { //开始事务 fragmentTransaction = fragmentManager.beginTransaction(); fragmentTransaction.remove(deletedFragment); //结束 fragmentTransaction.commit(); } else { Toast.makeText(getApplicationContext(), "没有找到要remove的fragment", Toast.LENGTH_LONG).show(); } } public void replaceFragment(String str) { if(str == "A") { Fragment fragment = new FragmentB(); //开始事务喽 fragmentTransaction = fragmentManager.beginTransaction(); //第一个参数 fragment所在的layout id //第二个参数 新的fragment //第三个参数 新的fragment的tag fragmentTransaction.replace(R.id.fragmentholder, fragment, "B"); //结束事务喽 fragmentTransaction.commit(); } else if(str == "B") { Fragment fragment = new FragmentA(); fragmentTransaction = fragmentManager.beginTransaction(); fragmentTransaction.replace(R.id.fragmentholder, fragment, "A"); fragmentTransaction.commit(); } } public void attachA() { //获得fragmentA的引用,第一次一定要先add 然后才能通过tag找到 //流程如下 //先add(add里面包括了attach) 然后fragment就会 和activity 联系 //然后 detach 此时fragment就会和activity解除联系 //(注意,detach和remove不同的地方就是解除联系后没有删掉它,还可以通过它的tag找到它) //最后通过attach 可以把解除联系后的fragment在重新绑定到activity界面 Fragment fragment = (Fragment)fragmentManager.findFragmentByTag("A"); //开启事务 fragmentTransaction = fragmentManager.beginTransaction(); if( fragment != null ) { fragmentTransaction.attach(fragment); fragmentTransaction.commit(); } else if( fragment == null) { Toast.makeText(this, "没找到要attach的fragment", Toast.LENGTH_LONG).show(); } } public void detachA() { //detach 的minSdkVersion 居然是13,所以我的minSdkVersion 从8 改到 11 再改到 13 Fragment fragment = (Fragment)fragmentManager.findFragmentByTag("A"); //开启事务 fragmentTransaction = fragmentManager.beginTransaction(); if( fragment != null ) { fragmentTransaction.detach(fragment); fragmentTransaction.commit(); } else if( fragment == null) { Toast.makeText(this, "没找到要detach的fragment", Toast.LENGTH_LONG).show(); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
主界面布局 一些按钮 和一个用来装 fragment的linearlayout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" tools:context=".MainActivity" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="add fragmeA" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="add fragmentB" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="remove fragmentA" /> <Button android:id="@+id/button5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="用B换A" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <Button android:id="@+id/button4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="remove fragmentB" /> <Button android:id="@+id/button6" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="用A换B" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <Button android:id="@+id/button7" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Attach A" /> <Button android:id="@+id/button8" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="detach A" /> </LinearLayout> <LinearLayout android:id="@+id/fragmentholder" android:layout_width="fill_parent" android:layout_height="120dp" android:orientation="vertical" > </LinearLayout> </LinearLayout>
fragment a 的布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="100dp" android:orientation="vertical" android:background="#ddd" > <TextView android:text="这里是fragment a" android:layout_width="fill_parent" android:layout_height="fill_parent"/> </LinearLayout>
fragment b 的布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="100dp" android:orientation="vertical" android:background="#CEDDED" > <TextView android:text="这里是fragment b" android:layout_width="fill_parent" android:layout_height="fill_parent"/> </LinearLayout>