FragmentCustomAnimationSupport.java revision fa2e2acf79d791a90410025daad438968550d18c
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 android.os.Bundle; 20import android.support.v4.app.Fragment; 21import android.support.v4.app.FragmentActivity; 22import android.support.v4.app.FragmentTransaction; 23import android.support.v4.content.ContextCompat; 24import android.support.v4.view.ViewCompat; 25import android.view.LayoutInflater; 26import android.view.View; 27import android.view.View.OnClickListener; 28import android.view.ViewGroup; 29import android.widget.Button; 30import android.widget.TextView; 31 32import com.example.android.supportv4.R; 33 34public class FragmentCustomAnimationSupport extends FragmentActivity { 35 int mStackLevel = 1; 36 37 @Override 38 protected void onCreate(Bundle savedInstanceState) { 39 super.onCreate(savedInstanceState); 40 setContentView(R.layout.fragment_stack); 41 42 // Watch for button clicks. 43 Button button = findViewById(R.id.new_fragment); 44 button.setOnClickListener(new OnClickListener() { 45 @Override 46 public void onClick(View v) { 47 addFragmentToStack(); 48 } 49 }); 50 51 if (savedInstanceState == null) { 52 // Do first time initialization -- add initial fragment. 53 Fragment newFragment = CountingFragment.newInstance(mStackLevel); 54 FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); 55 ft.add(R.id.simple_fragment, newFragment).commit(); 56 } else { 57 mStackLevel = savedInstanceState.getInt("level"); 58 } 59 } 60 61 @Override 62 public void onSaveInstanceState(Bundle outState) { 63 super.onSaveInstanceState(outState); 64 outState.putInt("level", mStackLevel); 65 } 66 67//BEGIN_INCLUDE(add_stack) 68 void addFragmentToStack() { 69 mStackLevel++; 70 71 // Instantiate a new fragment. 72 Fragment newFragment = CountingFragment.newInstance(mStackLevel); 73 74 // Add the fragment to the activity, pushing this transaction 75 // on to the back stack. 76 FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); 77 ft.setCustomAnimations(R.anim.fragment_slide_left_enter, 78 R.anim.fragment_slide_left_exit, 79 R.anim.fragment_slide_right_enter, 80 R.anim.fragment_slide_right_exit); 81 ft.replace(R.id.simple_fragment, newFragment); 82 ft.addToBackStack(null); 83 ft.commit(); 84 } 85//END_INCLUDE(add_stack) 86 87//BEGIN_INCLUDE(fragment) 88 public static class CountingFragment extends Fragment { 89 int mNum; 90 91 /** 92 * Create a new instance of CountingFragment, providing "num" 93 * as an argument. 94 */ 95 static CountingFragment newInstance(int num) { 96 CountingFragment f = new CountingFragment(); 97 98 // Supply num input as an argument. 99 Bundle args = new Bundle(); 100 args.putInt("num", num); 101 f.setArguments(args); 102 103 return f; 104 } 105 106 /** 107 * When creating, retrieve this instance's number from its arguments. 108 */ 109 @Override 110 public void onCreate(Bundle savedInstanceState) { 111 super.onCreate(savedInstanceState); 112 mNum = getArguments() != null ? getArguments().getInt("num") : 1; 113 } 114 115 /** 116 * The Fragment's UI is just a simple text view showing its 117 * instance number. 118 */ 119 @Override 120 public View onCreateView(LayoutInflater inflater, ViewGroup container, 121 Bundle savedInstanceState) { 122 View v = inflater.inflate(R.layout.hello_world, container, false); 123 View tv = v.findViewById(R.id.text); 124 ((TextView) tv).setText("Fragment #" + mNum); 125 ViewCompat.setBackground(tv, 126 ContextCompat.getDrawable(getContext(), android.R.drawable.gallery_thumb)); 127 return v; 128 } 129 } 130//END_INCLUDE(fragment) 131} 132