FragmentCustomAnimation实现Fragment的界面切换
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文件内容为:
- <?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:gravity="center_horizontal"
- android:orientation="vertical"
- android:padding="4dip" >
- <LinearLayout
- android:id="@+id/simple_fragment"
- android:layout_width="match_parent"
- android:layout_height="0px"
- android:layout_weight="1"
- android:background="#ffff0000" >
- </LinearLayout>
- <Button
- android:id="@+id/new_fragment"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_weight="0"
- android:text="new fragment" >
- <requestFocus />
- </Button>
- </LinearLayout>
hello_world.xml文件的内容为:
- <?xml version="1.0" encoding="utf-8"?>
- <TextView xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/text"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:gravity="center_vertical|center_horizontal"
- android:text=""
- android:textSize="40sp" />
类FragmentCustomAnimations的内容为:
- package com.fs.act;
- import android.app.Activity;
- import android.app.Fragment;
- import android.app.FragmentTransaction;
- import android.os.Bundle;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.TextView;
- public class FragmentCustomAnimations extends Activity {
- private int mStackLevel = 100;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.fragment_stack);
- Button button = (Button) findViewById(R.id.new_fragment);
- button.setOnClickListener(new OnClickListener() {
- public void onClick(View v) {
- addFragmentToStack();
- }
- });
- }
- void addFragmentToStack() {
- Fragment newFragment = CountingFragment.newInstance(++mStackLevel);
- FragmentTransaction ft = getFragmentManager().beginTransaction();
- ft.replace(R.id.simple_fragment, newFragment);
- ft.addToBackStack(null);
- ft.commit();
- }
- public static class CountingFragment extends Fragment {
- static CountingFragment newInstance(int num) {
- CountingFragment f = new CountingFragment();
- Bundle args = new Bundle();
- args.putInt("num", num);
- f.setArguments(args);
- return f;
- }
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
- View v = inflater.inflate(R.layout.hello_world, container, false);
- View tv = v.findViewById(R.id.text);
- ((TextView) tv).setText("Fragment #"+ this.getArguments().getInt("num"));
- return v;
- }
- }
- }
- <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的内容改为
- package com.fs.act;
- import android.annotation.SuppressLint;
- import android.app.Activity;
- import android.app.Fragment;
- import android.app.FragmentTransaction;
- import android.os.Bundle;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.TextView;
- public class FragmentCustomAnimations extends Activity {
- private int mStackLevel = 100;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.fragment_stack);
- Button button = (Button) findViewById(R.id.new_fragment);
- button.setOnClickListener(new OnClickListener() {
- public void onClick(View v) {
- addFragmentToStack();
- }
- });
- }
- @SuppressLint("NewApi")
- void addFragmentToStack() {
- Fragment newFragment = CountingFragment.newInstance(++mStackLevel);
- FragmentTransaction ft = getFragmentManager().beginTransaction();
- ft.setCustomAnimations(R.animator.fragment_slide_left_enter,
- R.animator.fragment_slide_left_exit,
- R.animator.fragment_slide_right_enter,
- R.animator.fragment_slide_right_exit);
- ft.replace(R.id.simple_fragment, newFragment);
- ft.addToBackStack(null);
- ft.commit();
- }
- public static class CountingFragment extends Fragment {
- static CountingFragment newInstance(int num) {
- CountingFragment f = new CountingFragment();
- Bundle args = new Bundle();
- args.putInt("num", num);
- f.setArguments(args);
- return f;
- }
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
- View v = inflater.inflate(R.layout.hello_world, container, false);
- View tv = v.findViewById(R.id.text);
- ((TextView) tv).setText("Fragment #"
- + this.getArguments().getInt("num"));
- return v;
- }
- }
- }
完
FragmentCustomAnimation实现Fragment的界面切换的更多相关文章
- Android Viewpager加Fragment做界面切换时数据消失的解决方式
今天遇到多个Fragment切换,回来后页面空白的情况,找到这个博客方法设置了一下,就可以了 vpAdapter = new VpAdapter(getSupportFragmentManager() ...
- Unity iOS混合开发界面切换思路
Unity iOS混合开发界面切换思路 最近有很多博友QQ 私信 或则 留言联系我,请教iOS和Unity界面之前相互切换的问题,源代码就不私下发你们了,界面跳转功能的代码我直接贴到下面好了,顺带说i ...
- Linux启动界面切换:图形界面-字符界面(转)
Linux字符界面切换到图形界面 由字符界面切换到图形界面可用两种简单方法实现: 1.在字符界面输入startx或init 5 . 2.通过编辑/etc/inittab文件实现默认进入图形界面. 把其 ...
- 使用ViewPager+Fragment实现选项卡切换效果
实现效果 本实例主要实现用ViewPage和Fragment实现选项卡切换效果,选项卡个数为3个,点击选项卡或滑动屏幕会切换Fragment并实现选项卡下方下边框条跟随移动效果. 本程序用androi ...
- Android控件Gridview实现仿支付宝首页,Fragment底部按钮切换和登录圆形头像
此案例主要讲的是Android控件Gridview(九宫格)完美实现仿支付宝首页,包含添加和删除功能:Fragment底部按钮切换的效果,包含四个模块,登录页面圆形头像等,一个小项目的初始布局. 效果 ...
- Html5 Egret游戏开发 成语大挑战(五)界面切换和数据处理
经过前面的制作,使用Egret的Wing很快完成了开始界面和选关卡界面,下面通常来说就是游戏界面,但此时界面切换和关卡数据还没有准备好,这次讲解界面的切换和关卡数据的解析.前面多次修改了Main.ts ...
- php-- Linux图形界面与字符界面切换
转自:http://blog.163.com/wang_ly2442/blog/static/9494340720128355054551/ 1. 启动时进入字符界面,后来想切换到图形界面:使用sta ...
- 参考_Android中,如何新建一个界面,并且实现从当前界面切换到到刚才新建的(另外一个)界面
参考地址: http://www.crifan.com/android_how_to_create_new_ui_and_switch_to_another_new_ui/ 想要实现,在Android ...
- WPF如何实现类似iPhone界面切换的效果(转载)
WPF如何实现类似iPhone界面切换的效果 (version .1) 转自:http://blog.csdn.net/fallincloud/article/details/6968764 在论坛上 ...
随机推荐
- MSSQL - SQL Server2008附加数据库失败 错误号:5120
附加数据库时,显示错误,错误信息为 一种解决方法为,设置mdf文件所在文件夹的权限(有些资料说只设置mdf文件的权限就好,但我试了不管用),在文件夹上右击——属性——安全,如图所示: 选择组或用户名中 ...
- Delphi XE2 升级项目编译ralease版本,无法添加UAC解决方法
我今天把一个原来是Delphi2007的工程升级到了Delphi XE2,在编译ralease版本时候,发现无法添加UAC,我觉得可能是升级的原因,随后我用XE2新建了一个工程,但是在编译raleas ...
- ORM增删改查询例题
public partial class Form1 : Form { private MydbInfoDataContext context = new MydbInfoDa ...
- 搜索:POJ2251&POJ1426&POJ3087&POJ2488
图的遍历也称为搜索,就是从图中某个顶点出发,沿着一些边遍历图中所有的顶点,且每个顶点仅被访问一次,遍历可采取两种不同的方式:深度优先搜索(DFS)和广度优先搜索(BFS). 1.DFS算法思想` 从顶 ...
- 《转》 Openstack Grizzly 指定 compute node 创建 instance
声明:此文档仅仅做学习交流使用,请勿用作其它商业用途 作者:朝阳_tony 邮箱:linzhaolover@gmail.com 2013年6月4日9:37:44 星期二 转载请注明出处:http:// ...
- js动态添加Div
利用JavaScript动态添加Div的方式有很多,在这次开发中有用到,就搜集了一下比较常用的. 一.在一个Div前添加Div <html> <body> <div id ...
- CountDownLatch和CyclicBarrier的区别(转)
在网上看到很多人对于CountDownLatch和CyclicBarrier的区别简单理解为CountDownLatch是一次性的,而CyclicBarrier在调用reset之后还可以继续使用.那如 ...
- opencv 训练自己的分类器汇总
原地址:http://www.cnblogs.com/zengqs/archive/2009/02/12/1389208.html OpenCV训练分类器 OpenCV训练分类器 一.简介 目标检测方 ...
- Zigbee开发(1)
只是研究zigbee的技术,也许后续的博客不会有很及时的更新,有时间 写一点东西能让大家有所收获吧. 环境搭建 Windows 64位的操作系统 IAR7.6 for 8051 ZStack CC25 ...
- Hibernate学习之createSQLQuery与createQuery的区别及使用
hibernate中createQuery与createSQLQuery:前者用的hql语句进行查询,后者可以用sql语句查询,前者以hibernate生成的Bean为对象装入list返回,后者则是以 ...