怎么通过activity里面的一个按钮跳转到另一个fragment(android FragmentTransaction.replace的用法介绍)
即:android FragmentTransaction.replace的用法介绍
Fragment的生命周期和它的宿主Activity密切相关,几乎和宿主Activity的生命周期一致,他们之间最大的不同在于Activity可以增加或删除Fragment。
使用FragmentTransaction
FragmentTransaction可以在运行时添加,删除或替换Fragment,从而实现UI的动态变化。Fragment Transaction由Fragment Manager的beginTransaction()方法创建,然后可以进行Fragment的添加,删除和替换,最后通过commit()方法提交修改。
添加,删除和替换Fragment
使用FragmentTransaction的add方法可以添加一个新的Fragment,add()方法的主要参数是Fragment的容器View(或其ID)及Fragment实例。
删除Fragment需要FragmentTransaction的remove()方法,参数为Fragment对象,Fragment对象可以通过FragmentManager的findFragmentById()方法获得。
替换Fragment使用的是FragmentTransaction的replace()方法,参数分别为所要替代Fragment所在容器的ID和新的Fragment。
获取指定的Fragment
有两种方法可以获取某个特定的Fragment,如果这个Fragment已经被添加到某个layout文件中,则可以使用xml文件中的id作为参数:
[java] view plaincopyprint?
- MyFragment myFragment = (MyFragment)fragmentManager.findFragmentById(R.id.MyFragment);
也可以通过创建Fragment时添加的tag获取特定的Fragment:
[java] view plaincopyprint?
- MyFragment myFragment = (MyFragment)fragmentManager.findFragmentByTag(MY_FRAGMENT_TAG);
删除Fragment容器
在配置文件中将visibility的属性设为"gone",即可删除某个Fragment
Fragment和Back Stack
Activity拥有Activity Stack,从而在用户按”返回”按钮时,回到前一个Activity。Fragment也可以响应”返回”事件,方法是FragmentTransaction在commit之前调用addToBackStack()方法。这样,在用户按返回键后,Android会首先重现之前的UI布局。
原理和Activity类似,调用addToBackStack()后,Fragment会被push到back stack中,而不是销毁。
Fragment Transaction的动画效果
Fragment Transaction有两种方法实现动画效果,分别是:
- 设置渐进:
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
- 设置动画效果:
[java] view plaincopyprint?
- fragmentTransaction.setCustomAnimations(R.animator.slide_in_left, R.animator.slide_out_right);
Fragment和宿主Activity之间的接口
Fragment可以通过getActivity()方法获得宿主Activity对象:
[java] view plaincopyprint?
- TextView textView = (TextView)getActivity().findViewById(R.id.textview);
另一种常见的Fragment和Activity之间的交互方式是使用回调函数:
[java] view plaincopyprint?
- <span style="font-size:12px">public interface OnSeasonSelectedListener {
- public void onSeasonSelected(Season season);
- }
- private OnSeasonSelectedListener onSeasonSelectedListener;
- private Season currentSeason;
- @Override
- public void onAttach(Activity activity) {
- super.onAttach(activity);
- try {
- onSeasonSelectedListener = (OnSeasonSelectedListener)activity;
- } catch (ClassCastException e) {
- throw new ClassCastException(activity.toString() +"must implement OnSeasonSelectedListener");
- }
- }
- private void setSeason(Season season) {
- currentSeason = season;
- onSeasonSelectedListener.onSeasonSelected(season);
- }</span>
没有UI的Fragment
尽管不常见,但Fragment的确是可以没有UI的,好处也许是拥有了更灵活的生命周期控制。没有UI的Fragment生命周期事件有这些:[java] view plaincopyprint?
- public class NewItemFragment extends Fragment {
- @Override
- public void onAttach(Activity activity) {
- <span style="white-space:pre"> </span>super.onAttach(activity);
- <span style="white-space:pre"> </span>// Get a type-safe reference to the parent Activity.
- }
- @Override
- public void onCreate(Bundle savedInstanceState) {
- <span style="white-space:pre"> </span> super.onCreate(savedInstanceState);
- <span style="white-space:pre"> </span> // Create background worker threads and tasks.
- }
- @Override
- public void onActivityCreated(Bundle savedInstanceState) {
- <span style="white-space:pre"> </span> super.onActivityCreated(savedInstanceState);
- <span style="white-space:pre"> </span> // Initiate worker threads and tasks.
- }
- }
常用的Fragment类
- DiagFragment
- ListFragment
- webViewFragment
下面小例子关于这一用法的介绍:
thisManager.beginTransaction()
.replace(R.id.tab_container,
new PolicyAssociationCardInfo(framework_))
.addToBackStack("TAG").commit();
package com.bokeyuan.tai.components.PolicyAssociation; import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener; import com.bokeyuan.tai.R;
import com.bokeyuan.tai.framework.BaseFragmentActivity; /**
* 保单关联
* @author
*
*/
public class PolicyAssociationActivity extends BaseFragmentActivity {
private static final String TAG = "PolicyAssociationActivity";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
thisManager
.beginTransaction()
.replace(R.id.tab_container,
new PolicyAssociationCardInfo(framework_))
.addToBackStack("TAG").commit();
} catch (Exception e) {
// TODO: handle exception
} } @Override
protected void onStart() {
super.onStart();
topBarObject.topViewMoreButton.setVisibility(View.INVISIBLE); // 标题栏返回
topBarObject.topViewBackButton
.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (thisManager.getBackStackEntryCount() <= 1) {
context.finish();
} else {
thisManager.popBackStackImmediate();
} }
});
} }
另外:FragmentTransaction add 和 replace 区别
使用 FragmentTransaction 的时候,它提供了这样两个方法,一个 add , 一个 replace .
add 和 replace 影响的只是界面,而控制回退的,是事务。
public abstract FragmentTransaction add (int containerViewId, Fragment fragment, String tag)
Add a fragment to the activity state. This fragment may optionally also have its view (if Fragment.onCreateView returns non-null) into a container view of the activity.
add 是把一个fragment添加到一个容器 container 里。
public abstract FragmentTransaction replace (int containerViewId, Fragment fragment, String tag)
Replace an existing fragment that was added to a container. This is essentially the same as calling remove(Fragment) for all currently added fragments that were added with the same containerViewId and then add(int, Fragment, String) with the same arguments given here.
replace 是先remove掉相同id的所有fragment,然后在add当前的这个fragment。是替换页面的意思!
在大部分情况下,这两个的表现基本相同。因为,一般,咱们会使用一个FrameLayout来当容器,而每个Fragment被add 或者 replace 到这个FrameLayout的时候,都是显示在最上层的。所以你看到的界面都是一样的。但是, 使用add的情况下,这个FrameLayout其实有2层,多层肯定要比一层的来得浪费,所以还是推荐使用replace。 当然有时候还是需要使用add的。比如要实现轮播图的效果,每个轮播图都是一个独立的Fragment,而他的容器FrameLayout需要add多个Fragment,这样他就可以根据提供的逻辑进行轮播了。
而至于返回键的时候,这个跟事务有关,跟使用add还是replace没有任何关系。
下面网上搜得一些理解:
android FragmentTransaction.replace的用法介绍
http://www.07net01.com/2015/04/824163.html Android FragmentManage FragmentTransaction介绍
http://blog.csdn.net/xyz_lmn/article/details/6927763 Android 管理Fragments
http://www.cnblogs.com/mengdd/archive/2013/01/09/2853254.html
怎么通过activity里面的一个按钮跳转到另一个fragment(android FragmentTransaction.replace的用法介绍)的更多相关文章
- Android由一个activity 间隔5秒自动跳转到另外一个activity
Android由一个activity 间隔5秒自动跳转到另外一个activity 2013年10月10日18:03:42 //一.写一个定时器 5秒后开启 final Intent lo ...
- PHP——0128练习相关2——js点击button按钮跳转到另一个新页面
js点击button按钮跳转到另一个新页面 投稿:whsnow 字体:[增加 减小] 类型:转载 时间:2014-10-10我要评论 点击按钮怎么跳转到另外一个页面呢?点击图片要跳转到新的页面时,怎么 ...
- js点击button按钮跳转到另一个新页面
点击按钮怎么跳转到另外一个页面呢?我们在网站制作中可能是需要的,因为有时我们需要做这样的效果,尤其是将按钮做成一个图片,而点击图片要跳转到新的页面时,怎么做到呢? 这样的效果可以:onclick=&q ...
- struts2 从一个action跳转到另一个action的struts.xml文件的配置
解释: 想要用<result>跳转到另一个action,原来的配置代码是: <action name="insertDept" class="strut ...
- 基于jquery的从一个页面跳转到另一个页面的指定位置的实现代码
比如 想跳到 mao.aspx 的页面 的div id="s" 的位置 那么 只用<a href="mao.aspx#s"> 就可实现跳转到指定位置 ...
- 从一个App跳转到另一个App
在跳入App的info中配置Bundle identifier 在跳入App的info中配置URL Schemes 在另一个应用程序中按照上边的操作添加openURL并运行,就可以跳转了 调用open ...
- jsp中点击一个图片跳转到另一个页面的方法
1.这是jsp页面中的关于图片的那段代码 <img src="images/tj1.png " id="tj1"></img> 2.跳转 ...
- vue从一个页面跳转到另一个页面并携带参数
1.需求: 点击商场跳转到商业体列表 解决方案: 元页面: a标签中添加跳转函数 <a class="orderBtn1 sIRicon2" href="javas ...
- vue从一个组件跳转到另一个组件页面router-link的试用
需求从helloworld.vue页面跳到good.vue页面 1.helloworld.vue页面代码 <template> <div class="hello" ...
随机推荐
- 用手机地图GPS导航费流量吗?
如果你的手机带有GPS芯片,那么使用手机导航是不会耗费手机流量的.但是如果你的手机没有GPS芯片,而使用的导航软件又是类似于移动提供的导航服务那样的导航功能,那就耗费手机流量了. 目前,导航软件导航主 ...
- Ranorex 5 发布,支持SAP、Oracle Forms、MS Dynamics等
Ranorex 5 发布,支持SAP.Oracle Forms.MS Dynamics等 http://blog.csdn.net/testing_is_believing/article/detai ...
- LintCode-- Remove Linked List Elements
Remove all elements from a linked list of integers that have valueval. 样例 Given 1->2->3->3- ...
- Android对话框之Context
代码就这么一段: new AlertDialog.Builder(getApplicationContext(),R.style.MyAlertDialogStyle) .setTitle(" ...
- HTML5[2]:使用viewport控制手机浏览器布局
基本 <meta name="viewport" content="width=device-width, initial-scale=1">192 ...
- C#基础课程之五集合(HashTable,Dictionary)
HashTable例子: #region HashTable #region Add Hashtable hashTable = new Hashtable(); Hashtable hashTabl ...
- 自定义ISPF面板
1)登录的时候可以看到登录执行的PROCEDURE,此处为DBSPROC 2.登录后,进入SDSF,再进入LOG,输入命令TOP,再输入命令F JOB,按F11把屏幕向右翻页,看到哪下界面 找到//I ...
- 浅析LRU(K-V)缓存
LRU(Least Recently Used)算法是缓存技术中的一种常见思想,顾名思义,最近最少使用,也就是说有两个维度来衡量,一个是时间(最近),一个频率(最少).如果需要按优先级来对缓存中的K- ...
- nodejs 常用全局包
1.nodemon 更改node程序后程序自动启动 (nodemon app.js) npm install nodemon -g 2.gulp 压缩合并代码等 npm install gulp - ...
- Mysql学习笔记(三)运算符和控制流函数
本章学习内容: 1.操作符 2.控制流程函数 操作符: i.圆括号.. 简单的介绍一下圆括号,圆括号的使用的目的是规定计算表达式的顺序...这个想必大家都熟悉例如 mysql>select 1 ...