FragmentCustomAnimationSupport.java revision 0157041980358a806cf32e53bd814347aa155231
1/* 2 * Copyright (C) 2011 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.example.android.supportv4.app; 18 19import com.example.android.supportv4.R; 20 21import android.support.v4.app.Fragment; 22import android.support.v4.app.FragmentActivity; 23import android.support.v4.app.FragmentTransaction; 24 25import android.os.Bundle; 26import android.view.LayoutInflater; 27import android.view.View; 28import android.view.ViewGroup; 29import android.view.View.OnClickListener; 30import android.widget.Button; 31import android.widget.TextView; 32 33public class FragmentCustomAnimationSupport extends FragmentActivity { 34 int mStackLevel = 1; 35 36 @Override 37 protected void onCreate(Bundle savedInstanceState) { 38 super.onCreate(savedInstanceState); 39 setContentView(R.layout.fragment_stack); 40 41 // Watch for button clicks. 42 Button button = (Button)findViewById(R.id.new_fragment); 43 button.setOnClickListener(new OnClickListener() { 44 public void onClick(View v) { 45 addFragmentToStack(); 46 } 47 }); 48 49 if (savedInstanceState == null) { 50 // Do first time initialization -- add initial fragment. 51 Fragment newFragment = CountingFragment.newInstance(mStackLevel); 52 FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); 53 ft.add(R.id.simple_fragment, newFragment).commit(); 54 } else { 55 mStackLevel = savedInstanceState.getInt("level"); 56 } 57 } 58 59 @Override 60 public void onSaveInstanceState(Bundle outState) { 61 super.onSaveInstanceState(outState); 62 outState.putInt("level", mStackLevel); 63 } 64 65//BEGIN_INCLUDE(add_stack) 66 void addFragmentToStack() { 67 mStackLevel++; 68 69 // Instantiate a new fragment. 70 Fragment newFragment = CountingFragment.newInstance(mStackLevel); 71 72 // Add the fragment to the activity, pushing this transaction 73 // on to the back stack. 74 FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); 75 ft.setCustomAnimations(R.anim.fragment_slide_left_enter, 76 R.anim.fragment_slide_left_exit, 77 R.anim.fragment_slide_right_enter, 78 R.anim.fragment_slide_right_exit); 79 ft.replace(R.id.simple_fragment, newFragment); 80 ft.addToBackStack(null); 81 ft.commit(); 82 } 83//END_INCLUDE(add_stack) 84 85//BEGIN_INCLUDE(fragment) 86 public static class CountingFragment extends Fragment { 87 int mNum; 88 89 /** 90 * Create a new instance of CountingFragment, providing "num" 91 * as an argument. 92 */ 93 static CountingFragment newInstance(int num) { 94 CountingFragment f = new CountingFragment(); 95 96 // Supply num input as an argument. 97 Bundle args = new Bundle(); 98 args.putInt("num", num); 99 f.setArguments(args); 100 101 return f; 102 } 103 104 /** 105 * When creating, retrieve this instance's number from its arguments. 106 */ 107 @Override 108 public void onCreate(Bundle savedInstanceState) { 109 super.onCreate(savedInstanceState); 110 mNum = getArguments() != null ? getArguments().getInt("num") : 1; 111 } 112 113 /** 114 * The Fragment's UI is just a simple text view showing its 115 * instance number. 116 */ 117 @Override 118 public View onCreateView(LayoutInflater inflater, ViewGroup container, 119 Bundle savedInstanceState) { 120 View v = inflater.inflate(R.layout.hello_world, container, false); 121 View tv = v.findViewById(R.id.text); 122 ((TextView)tv).setText("Fragment #" + mNum); 123 tv.setBackgroundDrawable(getResources().getDrawable(android.R.drawable.gallery_thumb)); 124 return v; 125 } 126 } 127//END_INCLUDE(fragment) 128} 129