• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2014 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 
17 package com.example.android.fragmenttransition;
18 
19 import com.example.android.common.logger.Log;
20 
21 import android.content.Context;
22 import android.os.Bundle;
23 import android.support.v4.app.Fragment;
24 import android.transition.Scene;
25 import android.transition.TransitionManager;
26 import android.view.LayoutInflater;
27 import android.view.View;
28 import android.view.ViewGroup;
29 import android.view.animation.Animation;
30 import android.view.animation.AnimationUtils;
31 import android.widget.FrameLayout;
32 import android.widget.ImageView;
33 import android.widget.TextView;
34 
35 public class DetailFragment extends Fragment implements Animation.AnimationListener {
36 
37     private static final String TAG = "DetailFragment";
38 
39     private static final String ARG_RESOURCE_ID = "resource_id";
40     private static final String ARG_TITLE = "title";
41     private static final String ARG_X = "x";
42     private static final String ARG_Y = "y";
43     private static final String ARG_WIDTH = "width";
44     private static final String ARG_HEIGHT = "height";
45 
46     /**
47      * Create a new instance of DetailFragment.
48      *
49      * @param resourceId The resource ID of the Drawable image to show
50      * @param title The title of the image
51      * @param x The horizontal position of the grid item in pixel
52      * @param y The vertical position of the grid item in pixel
53      * @param width The width of the grid item in pixel
54      * @param height The height of the grid item in pixel
55      * @return a new instance of DetailFragment
56      */
newInstance(int resourceId, String title, int x, int y, int width, int height)57     public static DetailFragment newInstance(int resourceId, String title,
58                                              int x, int y, int width, int height) {
59         DetailFragment fragment = new DetailFragment();
60         Bundle args = new Bundle();
61         args.putInt(ARG_RESOURCE_ID, resourceId);
62         args.putString(ARG_TITLE, title);
63         args.putInt(ARG_X, x);
64         args.putInt(ARG_Y, y);
65         args.putInt(ARG_WIDTH, width);
66         args.putInt(ARG_HEIGHT, height);
67         fragment.setArguments(args);
68         return fragment;
69     }
70 
DetailFragment()71     public DetailFragment() {
72     }
73 
74     @Override
onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)75     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
76         return inflater.inflate(R.layout.fragment_detail, container, false);
77     }
78 
79     @Override
onViewCreated(View view, Bundle savedInstanceState)80     public void onViewCreated(View view, Bundle savedInstanceState) {
81         FrameLayout root = (FrameLayout) view;
82         Context context = view.getContext();
83         assert context != null;
84         // This is how the fragment looks at first. Since the transition is one-way, we don't need to make
85         // this a Scene.
86         View item = LayoutInflater.from(context).inflate(R.layout.item_meat_grid, root, false);
87         assert item != null;
88         bind(item);
89         // We adjust the position of the initial image with LayoutParams using the values supplied
90         // as the fragment arguments.
91         Bundle args = getArguments();
92         FrameLayout.LayoutParams params = null;
93         if (args != null) {
94             params = new FrameLayout.LayoutParams(
95                     args.getInt(ARG_WIDTH), args.getInt(ARG_HEIGHT));
96             params.topMargin = args.getInt(ARG_Y);
97             params.leftMargin = args.getInt(ARG_X);
98         }
99         root.addView(item, params);
100     }
101 
102     @Override
onResume()103     public void onResume() {
104         super.onResume();
105     }
106 
107     /**
108      * Bind the views inside of parent with the fragment arguments.
109      *
110      * @param parent The parent of views to bind.
111      */
bind(View parent)112     private void bind(View parent) {
113         Bundle args = getArguments();
114         if (args == null) {
115             return;
116         }
117         ImageView image = (ImageView) parent.findViewById(R.id.image);
118         image.setImageResource(args.getInt(ARG_RESOURCE_ID));
119         TextView title = (TextView) parent.findViewById(R.id.title);
120         title.setText(args.getString(ARG_TITLE));
121     }
122 
123     @Override
onCreateAnimation(int transit, boolean enter, int nextAnim)124     public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
125         Animation animation = AnimationUtils.loadAnimation(getActivity(),
126                 enter ? android.R.anim.fade_in : android.R.anim.fade_out);
127         // We bind a listener for the fragment transaction. We only bind it when
128         // this fragment is entering.
129         if (animation != null && enter) {
130             animation.setAnimationListener(this);
131         }
132         return animation;
133     }
134 
135     @Override
onAnimationStart(Animation animation)136     public void onAnimationStart(Animation animation) {
137         // This method is called at the end of the animation for the fragment transaction.
138         // There is nothing we need to do in this sample.
139     }
140 
141     @Override
onAnimationEnd(Animation animation)142     public void onAnimationEnd(Animation animation) {
143         // This method is called at the end of the animation for the fragment transaction,
144         // which is perfect time to start our Transition.
145         Log.i(TAG, "Fragment animation ended. Starting a Transition.");
146         final Scene scene = Scene.getSceneForLayout((ViewGroup) getView(),
147                 R.layout.fragment_detail_content, getActivity());
148         TransitionManager.go(scene);
149         // Note that we need to bind views with data after we call TransitionManager.go().
150         bind(scene.getSceneRoot());
151     }
152 
153     @Override
onAnimationRepeat(Animation animation)154     public void onAnimationRepeat(Animation animation) {
155         // This method is never called in this sample because the animation doesn't repeat.
156     }
157 
158 }
159