zoukankan      html  css  js  c++  java
  • 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;
    		}
    	}
    
    }
    


  • 相关阅读:
    pycharm上运行django服务器端、以及创建app方法
    Python实现淘宝秒杀聚划算自动提醒源码
    Python版:Selenium2.0之WebDriver学习总结_实例1
    windows上使用pip下载东西时报编码错误问题解决方法
    模块购物商城和ATM机代码:
    Python网页信息采集:使用PhantomJS采集淘宝天猫商品内容
    android用户界面之ProgressBar教程实例汇总
    推荐12个亲测Android开发源码(包括应用、游戏、效果等等)
    Android开发各种demo集合
    Android Service
  • 原文地址:https://www.cnblogs.com/hrhguanli/p/4007287.html
Copyright © 2011-2022 走看看