Android FragmentTransactionExtended:使Fragment以多种样式动画切换
有多种fragment之间切换的效果,效果是这样的:
Demo的实现是很简单的。
在res/values中,新建一个arrays.xml文件,存放Fragment动画效果的名称,在spinner中使用:
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <string-array name="array_spinner">
- <item>SCALEX</item>
- <item>SCALEY</item>
- <item>SCALEXY</item>
- <item>FADE</item>
- <item>FLIP_HORIZONTAL</item>
- <item>FLIP_VERTICAL</item>
- <item>SLIDE_VERTICAL</item>
- <item>SLIDE_HORIZONTAL</item>
- <item>SLIDE_HORIZONTAL_PUSH_TOP</item>
- <item>SLIDE_VERTICAL_PUSH_LEFT</item>
- <item>GLIDE</item>
- <item>SLIDING</item>
- <item>STACK</item>
- <item>CUBE</item>
- <item>ROTATE_DOWN</item>
- <item>ROTATE_UP</item>
- <item>ACCORDION</item>
- <item>TABLE_HORIZONTAL</item>
- <item>TABLE_VERTICAL</item>
- <item>ZOOM_FROM_LEFT_CORNER</item>
- <item>ZOOM_FROM_RIGHT_CORNER</item>
- <item>ZOOM_SLIDE_HORIZONTAL</item>
- <item>ZOOM_SLIDE_VERTICAL</item>
- </string-array>
- </resources>
两个空的fragment的布局(都有一样):
- <?xml version="1.0" encoding="utf-8"?>
- <com.desarrollodroide.libraryfragmenttransactionextended.SlidingRelativeLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:background="@drawable/back1" >
- <TextView
- android:layout_centerInParent="true"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Fragment1"
- android:textColor="#FF009922"
- android:textSize="20dp"/>
- </com.desarrollodroide.libraryfragmenttransactionextended.SlidingRelativeLayout>
主部局:
- <?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:orientation="vertical">
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:orientation="horizontal">
- <Spinner
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:id="@+id/spinner" />
- <Button
- android:id="@+id/button"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:onClick="addTransition"
- android:text="Push" />
- </LinearLayout>
- <com.desarrollodroide.libraryfragmenttransactionextended.SlidingRelativeLayout
- android:id="@+id/fragment_place"
- android:layout_marginLeft="10dp"
- android:layout_marginRight="10dp"
- android:layout_marginBottom="10dp"
- android:layout_marginTop="20dp"
- android:layout_width="match_parent"
- android:layout_height="match_parent">
- </com.desarrollodroide.libraryfragmenttransactionextended.SlidingRelativeLayout>
- </LinearLayout>
MAinActivity:
- package com.example.scxh.testfragmenttransaction;
- import android.app.FragmentManager;
- import android.app.FragmentTransaction;
- import android.os.Bundle;
- import android.support.v7.app.AppCompatActivity;
- import android.view.View;
- import android.widget.AdapterView;
- import android.widget.ArrayAdapter;
- import android.widget.Button;
- import android.widget.Spinner;
- import com.desarrollodroide.libraryfragmenttransactionextended.FragmentTransactionExtended;
- public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
- private int optionSelected = 0;
- private BlankFragment mFirstFragment;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- Spinner spinner = (Spinner) findViewById(R.id.spinner);
- ArrayAdapter adapter=ArrayAdapter.createFromResource(this,R.array.array_spinner,android.R.layout.simple_spinner_item);
- adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
- spinner.setAdapter(adapter);
- spinner.setOnItemSelectedListener(this);
- mFirstFragment=new BlankFragment();
- getFragmentManager().beginTransaction()
- .add(R.id.fragment_place,mFirstFragment).commit();
- }
- public void addTransition(View view) {
- Button button = (Button) findViewById(R.id.button);
- if (getFragmentManager().getBackStackEntryCount() == 0) {
- BlankFragment2 fragment2 = new BlankFragment2();
- FragmentManager manager = getFragmentManager();
- FragmentTransaction transaction=manager.beginTransaction();
- FragmentTransactionExtended fragmentTransactionExtended = new FragmentTransactionExtended(this, transaction, mFirstFragment, fragment2, R.id.fragment_place);
- fragmentTransactionExtended.addTransition(optionSelected);
- fragmentTransactionExtended.commit();
- button.setText("Back");
- }else{
- getFragmentManager().popBackStack();
- button.setText("Push");
- }
- }
- @Override
- public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
- optionSelected = position;
- }
- @Override
- public void onNothingSelected(AdapterView<?> parent) {
- }
- @Override
- public void onBackPressed(){
- Button button = (Button) findViewById(R.id.button);
- button.setText("Push");
- super.onBackPressed();
- }
- }
Android FragmentTransactionExtended:使Fragment以多种样式动画切换的更多相关文章
- Android Activity和Fragment的转场动画
Android Activity和Fragment的转场动画 Activity转场动画 Activity的转场动画是通过overridePendingTransition(int enterAnim, ...
- [转]Android Activity和Fragment的转场动画
Android Activity和Fragment的转场动画 Activity转场动画 Activity的转场动画是通过overridePendingTransition(int enterAnim, ...
- Android中通过Fragment进行简单的页面切换
首先是activity中的布局 <?xml version="1.0" encoding="utf-8"?> <androidx.constr ...
- android中viewPager+fragment实现的屏幕左右切换(进阶篇)
Fragment支持在不同的Activity中使用并且可以处理自己的输入事件以及生命周期方法等.可以看做是一个子Activity. 先看一下布局: 1 <LinearLayout xmlns:a ...
- Android编程之Fragment使用动画造成Unknown animation name: objectAnimator异常
在为Fragment做切换动画.启动后遇到了一个异常: Caused by: java.lang.RuntimeException: Unknown animation name: objectAni ...
- 【Android初级】如何实现一个有动画效果的自定义下拉菜单
我们在购物APP里面设置收货地址时,都会有让我们选择省份及城市的下拉菜单项.今天我将使用Android原生的 Spinner 控件来实现一个自定义的下拉菜单功能,并配上一个透明渐变动画效果. 要实现的 ...
- Android系列之Fragment(四)----ListFragment的使用
[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/ ...
- Android类参考---Fragment
Android类参考---Fragment public final boolean isAdded() 如果该Fragment对象被添加到了它的Activity中,那么它返回true,否则返回fal ...
- Android -- TabHost、Fragment、状态保存、通信
工程结构 TabAFm到Ta ...
随机推荐
- WPF 中 InitializeComponent 不存在解决方案
部门给了一个WPF应用,但是之前没学过,所以再用vs2015创建测试程序的时候,竟然报了错,一创建项目就报错的程序还真不多.大概就是 InitializeComponent不存在吧.所以在网上看了很多 ...
- bean找不到异常
和这种的 原因: 这些都是因为bean注入的时候没有找个要注入的bean 解决办法: 1.查看dubbo文件中,暴露接口是否引入bean 2.如果有引入,查看引入路径和类是否存在.
- android学习之路--------intent
正式开始学习android,没有看书和视频,所以没有系统的学,只是看到哪个知识点就去学习,今天学习界面之间的跳转,以及传值,主要的知识点是intent, @Override protected voi ...
- html4,xhtml,html5发展历史
SGML SGML 是一种很强大但很复杂的标记语言,HTML.XML 就是从中衍生出来的.SGML 的例子如下:<QUOTE TYPE="example"> typic ...
- javascript中的事件冒泡、事件捕获和事件执行顺序
谈起JavaScript的 事件,事件冒泡.事件捕获.阻止默认事件这三个话题,无论是面试还是在平时的工作中,都很难避免. DOM事件标准定义了两种事件流,这两种事件流有着显著的不同并且可能对你的应用有 ...
- Datatable+Springmvc+mybatis(分页+排序+搜索)_Jquery
一.简介 通过Jqury的Datatable插件,构造数据列表,并且增加或者隐藏相应的列,已达到数据显示要求.同时, jQuery Datatable 强大的功能支持:排序,分页,搜索等. 二.前台分 ...
- python 中字典实用操作
1.字典转化为列表 a={"username":"12","password":"89"} print a.items( ...
- Android之拨号界面图片风格,无信息默认显示界面修改
Android之拨号界面图片风格,无信息默认显示界面修改 点开Dialer app,出现拨号,联系人,收藏三个选项卡,也就是三个Fragment,在三个界面都没有信息的时候会显示一个时钟,联系人,收藏 ...
- Docker实践,来自沪江、滴滴、蘑菇街架构师的交流分享
架构师小组交流会:每期选一个时下最热门的技术话题进行实践经验分享. 第一期主题:容器实践.Docker 作为当前最具颠覆性的开源技术之一,其轻量虚拟化.可移植性是CI/CD,DevOps,微服务的重要 ...
- Excel字符串连接
1.利用&连接. ="('"&A4&"','"&B4&"','"&C4&" ...