1.知识点:FragmentCustomAnimation

2.演示样例:来自于官网演示样例的简化,这样更方便于学习该知识点。

本演示样例的源代码下载地址为:http://download.csdn.net/detail/far_sight/7932287

3.项目FragmentCustomAnimationTest1效果:反复点buttonnew fragment,第点一次,数字加一,实现原因是第点一次加了一个新的Fragment在栈中。 当点返回键时,数字会降低,原因是Fragment在出栈。当最后一个出栈后再点返回键,程序退出。

两个在layout中的xml文件fragment_stack.xml与hello_world.xml

fragment_stack.xml文件内容为:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:gravity="center_horizontal"
  6. android:orientation="vertical"
  7. android:padding="4dip" >
  8.  
  9. <LinearLayout
  10. android:id="@+id/simple_fragment"
  11. android:layout_width="match_parent"
  12. android:layout_height="0px"
  13. android:layout_weight="1"
  14. android:background="#ffff0000" >
  15. </LinearLayout>
  16. <Button
  17. android:id="@+id/new_fragment"
  18. android:layout_width="wrap_content"
  19. android:layout_height="wrap_content"
  20. android:layout_weight="0"
  21. android:text="new fragment" >
  22. <requestFocus />
  23. </Button>
  24.  
  25. </LinearLayout>

hello_world.xml文件的内容为:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <TextView xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:id="@+id/text"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:gravity="center_vertical|center_horizontal"
  7. android:text=""
  8. android:textSize="40sp" />

类FragmentCustomAnimations的内容为:

  1. package com.fs.act;
  2. import android.app.Activity;
  3. import android.app.Fragment;
  4. import android.app.FragmentTransaction;
  5. import android.os.Bundle;
  6. import android.view.LayoutInflater;
  7. import android.view.View;
  8. import android.view.ViewGroup;
  9. import android.view.View.OnClickListener;
  10. import android.widget.Button;
  11. import android.widget.TextView;
  12. public class FragmentCustomAnimations extends Activity {
  13. private int mStackLevel = 100;
  14. @Override
  15. protected void onCreate(Bundle savedInstanceState) {
  16. super.onCreate(savedInstanceState);
  17. setContentView(R.layout.fragment_stack);
  18. Button button = (Button) findViewById(R.id.new_fragment);
  19. button.setOnClickListener(new OnClickListener() {
  20. public void onClick(View v) {
  21. addFragmentToStack();
  22. }
  23. });
  24. }
  25. void addFragmentToStack() {
  26. Fragment newFragment = CountingFragment.newInstance(++mStackLevel);
  27. FragmentTransaction ft = getFragmentManager().beginTransaction();
  28. ft.replace(R.id.simple_fragment, newFragment);
  29. ft.addToBackStack(null);
  30. ft.commit();
  31. }
  32.  
  33. public static class CountingFragment extends Fragment {
  34. static CountingFragment newInstance(int num) {
  35. CountingFragment f = new CountingFragment();
  36. Bundle args = new Bundle();
  37. args.putInt("num", num);
  38. f.setArguments(args);
  39. return f;
  40. }
  41. @Override
  42. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  43. Bundle savedInstanceState) {
  44. View v = inflater.inflate(R.layout.hello_world, container, false);
  45. View tv = v.findViewById(R.id.text);
  46. ((TextView) tv).setText("Fragment #"+ this.getArguments().getInt("num"));
  47. return v;
  48. }
  49. }
  50.  
  51. }
  52. <img src="http://img.blog.csdn.net/20140917172541900?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd2FuZ2d1YW5ncm9uZw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />

4.项目FragmentCustomAnimationTest2加了界面切换时滑动效果。下官是在栈中加Fragment还是按返回键让Fragment从栈中出栈,都有滑动效果。其他与项目

FragmentCustomAnimationTest1同样。

(1) 在上一项目基础上在res以下添加�目录animator,里面加下四个文件

fragment_slide_left_enter.xml

fragment_slide_left_exit.xml

fragment_slide_right_enter.xml

fragment_slide_right_exit.xml

(2)FragmentCustomAnimations.java的内容改为

  1. package com.fs.act;
  2.  
  3. import android.annotation.SuppressLint;
  4. import android.app.Activity;
  5. import android.app.Fragment;
  6. import android.app.FragmentTransaction;
  7. import android.os.Bundle;
  8. import android.view.LayoutInflater;
  9. import android.view.View;
  10. import android.view.ViewGroup;
  11. import android.view.View.OnClickListener;
  12. import android.widget.Button;
  13. import android.widget.TextView;
  14.  
  15. public class FragmentCustomAnimations extends Activity {
  16. private int mStackLevel = 100;
  17.  
  18. @Override
  19. protected void onCreate(Bundle savedInstanceState) {
  20. super.onCreate(savedInstanceState);
  21. setContentView(R.layout.fragment_stack);
  22. Button button = (Button) findViewById(R.id.new_fragment);
  23. button.setOnClickListener(new OnClickListener() {
  24. public void onClick(View v) {
  25. addFragmentToStack();
  26. }
  27. });
  28. }
  29.  
  30. @SuppressLint("NewApi")
  31. void addFragmentToStack() {
  32. Fragment newFragment = CountingFragment.newInstance(++mStackLevel);
  33. FragmentTransaction ft = getFragmentManager().beginTransaction();
  34. ft.setCustomAnimations(R.animator.fragment_slide_left_enter,
  35. R.animator.fragment_slide_left_exit,
  36. R.animator.fragment_slide_right_enter,
  37. R.animator.fragment_slide_right_exit);
  38. ft.replace(R.id.simple_fragment, newFragment);
  39. ft.addToBackStack(null);
  40. ft.commit();
  41. }
  42.  
  43. public static class CountingFragment extends Fragment {
  44. static CountingFragment newInstance(int num) {
  45. CountingFragment f = new CountingFragment();
  46. Bundle args = new Bundle();
  47. args.putInt("num", num);
  48. f.setArguments(args);
  49. return f;
  50. }
  51.  
  52. @Override
  53. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  54. Bundle savedInstanceState) {
  55. View v = inflater.inflate(R.layout.hello_world, container, false);
  56. View tv = v.findViewById(R.id.text);
  57. ((TextView) tv).setText("Fragment #"
  58. + this.getArguments().getInt("num"));
  59. return v;
  60. }
  61. }
  62.  
  63. }

FragmentCustomAnimation实现Fragment的界面切换的更多相关文章

  1. Android Viewpager加Fragment做界面切换时数据消失的解决方式

    今天遇到多个Fragment切换,回来后页面空白的情况,找到这个博客方法设置了一下,就可以了 vpAdapter = new VpAdapter(getSupportFragmentManager() ...

  2. Unity iOS混合开发界面切换思路

    Unity iOS混合开发界面切换思路 最近有很多博友QQ 私信 或则 留言联系我,请教iOS和Unity界面之前相互切换的问题,源代码就不私下发你们了,界面跳转功能的代码我直接贴到下面好了,顺带说i ...

  3. Linux启动界面切换:图形界面-字符界面(转)

    Linux字符界面切换到图形界面 由字符界面切换到图形界面可用两种简单方法实现: 1.在字符界面输入startx或init 5 . 2.通过编辑/etc/inittab文件实现默认进入图形界面. 把其 ...

  4. 使用ViewPager+Fragment实现选项卡切换效果

    实现效果 本实例主要实现用ViewPage和Fragment实现选项卡切换效果,选项卡个数为3个,点击选项卡或滑动屏幕会切换Fragment并实现选项卡下方下边框条跟随移动效果. 本程序用androi ...

  5. Android控件Gridview实现仿支付宝首页,Fragment底部按钮切换和登录圆形头像

    此案例主要讲的是Android控件Gridview(九宫格)完美实现仿支付宝首页,包含添加和删除功能:Fragment底部按钮切换的效果,包含四个模块,登录页面圆形头像等,一个小项目的初始布局. 效果 ...

  6. Html5 Egret游戏开发 成语大挑战(五)界面切换和数据处理

    经过前面的制作,使用Egret的Wing很快完成了开始界面和选关卡界面,下面通常来说就是游戏界面,但此时界面切换和关卡数据还没有准备好,这次讲解界面的切换和关卡数据的解析.前面多次修改了Main.ts ...

  7. php-- Linux图形界面与字符界面切换

    转自:http://blog.163.com/wang_ly2442/blog/static/9494340720128355054551/ 1. 启动时进入字符界面,后来想切换到图形界面:使用sta ...

  8. 参考_Android中,如何新建一个界面,并且实现从当前界面切换到到刚才新建的(另外一个)界面

    参考地址: http://www.crifan.com/android_how_to_create_new_ui_and_switch_to_another_new_ui/ 想要实现,在Android ...

  9. WPF如何实现类似iPhone界面切换的效果(转载)

    WPF如何实现类似iPhone界面切换的效果 (version .1) 转自:http://blog.csdn.net/fallincloud/article/details/6968764 在论坛上 ...

随机推荐

  1. MSSQL - SQL Server2008附加数据库失败 错误号:5120

    附加数据库时,显示错误,错误信息为 一种解决方法为,设置mdf文件所在文件夹的权限(有些资料说只设置mdf文件的权限就好,但我试了不管用),在文件夹上右击——属性——安全,如图所示: 选择组或用户名中 ...

  2. Delphi XE2 升级项目编译ralease版本,无法添加UAC解决方法

    我今天把一个原来是Delphi2007的工程升级到了Delphi XE2,在编译ralease版本时候,发现无法添加UAC,我觉得可能是升级的原因,随后我用XE2新建了一个工程,但是在编译raleas ...

  3. ORM增删改查询例题

    public partial class Form1 : Form     {         private MydbInfoDataContext context = new MydbInfoDa ...

  4. 搜索:POJ2251&POJ1426&POJ3087&POJ2488

    图的遍历也称为搜索,就是从图中某个顶点出发,沿着一些边遍历图中所有的顶点,且每个顶点仅被访问一次,遍历可采取两种不同的方式:深度优先搜索(DFS)和广度优先搜索(BFS). 1.DFS算法思想` 从顶 ...

  5. 《转》 Openstack Grizzly 指定 compute node 创建 instance

    声明:此文档仅仅做学习交流使用,请勿用作其它商业用途 作者:朝阳_tony 邮箱:linzhaolover@gmail.com 2013年6月4日9:37:44 星期二 转载请注明出处:http:// ...

  6. js动态添加Div

    利用JavaScript动态添加Div的方式有很多,在这次开发中有用到,就搜集了一下比较常用的. 一.在一个Div前添加Div <html> <body> <div id ...

  7. CountDownLatch和CyclicBarrier的区别(转)

    在网上看到很多人对于CountDownLatch和CyclicBarrier的区别简单理解为CountDownLatch是一次性的,而CyclicBarrier在调用reset之后还可以继续使用.那如 ...

  8. opencv 训练自己的分类器汇总

    原地址:http://www.cnblogs.com/zengqs/archive/2009/02/12/1389208.html OpenCV训练分类器 OpenCV训练分类器 一.简介 目标检测方 ...

  9. Zigbee开发(1)

    只是研究zigbee的技术,也许后续的博客不会有很及时的更新,有时间 写一点东西能让大家有所收获吧. 环境搭建 Windows 64位的操作系统 IAR7.6 for 8051 ZStack CC25 ...

  10. Hibernate学习之createSQLQuery与createQuery的区别及使用

    hibernate中createQuery与createSQLQuery:前者用的hql语句进行查询,后者可以用sql语句查询,前者以hibernate生成的Bean为对象装入list返回,后者则是以 ...