码迷,mamicode.com
首页 > 其他好文 > 详细

FragmentCustomAnimation实现Fragment的界面切换

时间:2014-10-05 20:49:48      阅读:241      评论:0      收藏:0      [点我收藏+]

标签:android   style   blog   http   io   os   ar   java   文件   

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   style   blog   http   io   os   ar   java   文件   

原文地址:http://www.cnblogs.com/hrhguanli/p/4007287.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!