有多种fragment之间切换的效果,效果是这样的:

Demo的实现是很简单的。

在res/values中,新建一个arrays.xml文件,存放Fragment动画效果的名称,在spinner中使用:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3. <string-array name="array_spinner">
  4. <item>SCALEX</item>
  5. <item>SCALEY</item>
  6. <item>SCALEXY</item>
  7. <item>FADE</item>
  8. <item>FLIP_HORIZONTAL</item>
  9. <item>FLIP_VERTICAL</item>
  10. <item>SLIDE_VERTICAL</item>
  11. <item>SLIDE_HORIZONTAL</item>
  12. <item>SLIDE_HORIZONTAL_PUSH_TOP</item>
  13. <item>SLIDE_VERTICAL_PUSH_LEFT</item>
  14. <item>GLIDE</item>
  15. <item>SLIDING</item>
  16. <item>STACK</item>
  17. <item>CUBE</item>
  18. <item>ROTATE_DOWN</item>
  19. <item>ROTATE_UP</item>
  20. <item>ACCORDION</item>
  21. <item>TABLE_HORIZONTAL</item>
  22. <item>TABLE_VERTICAL</item>
  23. <item>ZOOM_FROM_LEFT_CORNER</item>
  24. <item>ZOOM_FROM_RIGHT_CORNER</item>
  25. <item>ZOOM_SLIDE_HORIZONTAL</item>
  26. <item>ZOOM_SLIDE_VERTICAL</item>
  27. </string-array>
  28. </resources>

两个空的fragment的布局(都有一样):

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <com.desarrollodroide.libraryfragmenttransactionextended.SlidingRelativeLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:background="@drawable/back1" >
  7. <TextView
  8. android:layout_centerInParent="true"
  9. android:layout_width="wrap_content"
  10. android:layout_height="wrap_content"
  11. android:text="Fragment1"
  12. android:textColor="#FF009922"
  13. android:textSize="20dp"/>
  14. </com.desarrollodroide.libraryfragmenttransactionextended.SlidingRelativeLayout>

主部局:

  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:orientation="vertical">
  6. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  7. android:layout_width="match_parent"
  8. android:layout_height="wrap_content"
  9. android:orientation="horizontal">
  10. <Spinner
  11. android:layout_width="wrap_content"
  12. android:layout_height="wrap_content"
  13. android:layout_weight="1"
  14. android:id="@+id/spinner" />
  15. <Button
  16. android:id="@+id/button"
  17. android:layout_width="wrap_content"
  18. android:layout_height="wrap_content"
  19. android:onClick="addTransition"
  20. android:text="Push" />
  21. </LinearLayout>
  22. <com.desarrollodroide.libraryfragmenttransactionextended.SlidingRelativeLayout
  23. android:id="@+id/fragment_place"
  24. android:layout_marginLeft="10dp"
  25. android:layout_marginRight="10dp"
  26. android:layout_marginBottom="10dp"
  27. android:layout_marginTop="20dp"
  28. android:layout_width="match_parent"
  29. android:layout_height="match_parent">
  30. </com.desarrollodroide.libraryfragmenttransactionextended.SlidingRelativeLayout>
  31. </LinearLayout>

MAinActivity:

  1. package com.example.scxh.testfragmenttransaction;
  2. import android.app.FragmentManager;
  3. import android.app.FragmentTransaction;
  4. import android.os.Bundle;
  5. import android.support.v7.app.AppCompatActivity;
  6. import android.view.View;
  7. import android.widget.AdapterView;
  8. import android.widget.ArrayAdapter;
  9. import android.widget.Button;
  10. import android.widget.Spinner;
  11. import com.desarrollodroide.libraryfragmenttransactionextended.FragmentTransactionExtended;
  12. public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
  13. private int optionSelected = 0;
  14. private BlankFragment mFirstFragment;
  15. @Override
  16. protected void onCreate(Bundle savedInstanceState) {
  17. super.onCreate(savedInstanceState);
  18. setContentView(R.layout.activity_main);
  19. Spinner spinner = (Spinner) findViewById(R.id.spinner);
  20. ArrayAdapter adapter=ArrayAdapter.createFromResource(this,R.array.array_spinner,android.R.layout.simple_spinner_item);
  21. adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  22. spinner.setAdapter(adapter);
  23. spinner.setOnItemSelectedListener(this);
  24. mFirstFragment=new BlankFragment();
  25. getFragmentManager().beginTransaction()
  26. .add(R.id.fragment_place,mFirstFragment).commit();
  27. }
  28. public void addTransition(View view) {
  29. Button button = (Button) findViewById(R.id.button);
  30. if (getFragmentManager().getBackStackEntryCount() == 0) {
  31. BlankFragment2 fragment2 = new BlankFragment2();
  32. FragmentManager manager = getFragmentManager();
  33. FragmentTransaction transaction=manager.beginTransaction();
  34. FragmentTransactionExtended fragmentTransactionExtended = new FragmentTransactionExtended(this, transaction, mFirstFragment, fragment2, R.id.fragment_place);
  35. fragmentTransactionExtended.addTransition(optionSelected);
  36. fragmentTransactionExtended.commit();
  37. button.setText("Back");
  38. }else{
  39. getFragmentManager().popBackStack();
  40. button.setText("Push");
  41. }
  42. }
  43. @Override
  44. public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
  45. optionSelected = position;
  46. }
  47. @Override
  48. public void onNothingSelected(AdapterView<?> parent) {
  49. }
  50. @Override
  51. public void onBackPressed(){
  52. Button button = (Button) findViewById(R.id.button);
  53. button.setText("Push");
  54. super.onBackPressed();
  55. }
  56. }

Android FragmentTransactionExtended:使Fragment以多种样式动画切换的更多相关文章

  1. Android Activity和Fragment的转场动画

    Android Activity和Fragment的转场动画 Activity转场动画 Activity的转场动画是通过overridePendingTransition(int enterAnim, ...

  2. [转]Android Activity和Fragment的转场动画

    Android Activity和Fragment的转场动画 Activity转场动画 Activity的转场动画是通过overridePendingTransition(int enterAnim, ...

  3. Android中通过Fragment进行简单的页面切换

    首先是activity中的布局 <?xml version="1.0" encoding="utf-8"?> <androidx.constr ...

  4. android中viewPager+fragment实现的屏幕左右切换(进阶篇)

    Fragment支持在不同的Activity中使用并且可以处理自己的输入事件以及生命周期方法等.可以看做是一个子Activity. 先看一下布局: 1 <LinearLayout xmlns:a ...

  5. Android编程之Fragment使用动画造成Unknown animation name: objectAnimator异常

    在为Fragment做切换动画.启动后遇到了一个异常: Caused by: java.lang.RuntimeException: Unknown animation name: objectAni ...

  6. 【Android初级】如何实现一个有动画效果的自定义下拉菜单

    我们在购物APP里面设置收货地址时,都会有让我们选择省份及城市的下拉菜单项.今天我将使用Android原生的 Spinner 控件来实现一个自定义的下拉菜单功能,并配上一个透明渐变动画效果. 要实现的 ...

  7. Android系列之Fragment(四)----ListFragment的使用

    ​[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/ ...

  8. Android类参考---Fragment

    Android类参考---Fragment public final boolean isAdded() 如果该Fragment对象被添加到了它的Activity中,那么它返回true,否则返回fal ...

  9. Android -- TabHost、Fragment、状态保存、通信

    工程结构                                                                                       TabAFm到Ta ...

随机推荐

  1. PSR-1:基本的代码风格

    PHP标签 必须把PHP代码放在<?php ?>或<?= ?>标签中.不得使用其他PHP标签句法. 编码 所有PHP文件都必须使用UTF-8字符集编码,而且不能有字节顺序标记( ...

  2. SLES 10安装Oracle10gR2笔记

    SLES 10安装Oracle10gR2笔记 一. 数据库安装 . 安装C/C++ Compiler gcc --version验证是否安装 . 验证Service Pack版本 SPident –v ...

  3. JS判断日期是否在同一个星期内,和同一个月内

    今天要用到判断日期是否在同一个星期内和是否在同一个月内,在网上找了好一会儿也没找到合适的,然后自己写了一个方法来处理这个问题,思路就不详细介绍了,直接附上代码,自己测试了一下 没有问题,若有问题请在评 ...

  4. Spark在Hadoop集群上的配置(spark-1.1.0-bin-hadoop2.4)

    运行Spark服务,需要在每个节点上部署Spark. 可以先从主节点上将配置修改好,然后把Spark直接scp到其他目录. 关键配置 修改conf/spark-env.sh文件: export JAV ...

  5. RSA5、RSA6

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  6. Vs2012(Vs2013) 编译 64位 Qt (动态库), 并使用自编译Qt建立工程(悲催经历)。(含遗留问题)

    仅供参考. 体会:我个人此次编译不该使用Vs2013编译Qt. 使用以下程序: Qt : qt-opensource-windows-x86-msvc2012_64_opengl-5.2.1.exe ...

  7. Ubuntu中配置Thunderbird登录qq邮箱

    1.打开thunderbird 2.开启QQ邮箱pop功能 登录网页版QQ邮箱(email.qq.com) 设置---->>账户---->> 3.配置thunderbird 注 ...

  8. Jenkins的安装与配置

    资源下载: --jenkins的war包 首先从http://jenkins-ci.org/下载最新的jenkins版本,最新版本为1.594,下载的文件载体为jenkins.war: Jenkins ...

  9. 贪心算法(Greedy Algorithm)

    参考: 五大常用算法之三:贪心算法 算法系列:贪心算法 贪心算法详解 从零开始学贪心算法 一.基本概念: 所谓贪心算法是指,在对问题求解时,总是做出在当前看来是最好的选择.也就是说,不从整体最优上加以 ...

  10. navicat 快捷键

    1.ctrl+q           打开查询窗口 2.ctrl+/            注释sql语句 3.ctrl+shift +/  解除注释 4.ctrl+r           运行查询窗 ...