Home | History | Annotate | Download | only in com.example.android.fragmenttransition
      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.os.Bundle;
     22 import android.support.v4.app.Fragment;
     23 import android.view.LayoutInflater;
     24 import android.view.View;
     25 import android.view.ViewGroup;
     26 import android.view.animation.Animation;
     27 import android.view.animation.AnimationUtils;
     28 import android.widget.AdapterView;
     29 import android.widget.GridView;
     30 
     31 public class FragmentTransitionFragment extends Fragment implements AdapterView.OnItemClickListener {
     32 
     33     private static final String TAG = "FragmentTransitionFragment";
     34 
     35     private MeatAdapter mAdapter;
     36 
     37     public static FragmentTransitionFragment newInstance() {
     38         return new FragmentTransitionFragment();
     39     }
     40 
     41     public FragmentTransitionFragment() {
     42     }
     43 
     44     @Override
     45     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
     46         // This is the adapter we use to populate the grid.
     47         mAdapter = new MeatAdapter(inflater, R.layout.item_meat_grid);
     48         // Inflate the layout with a GridView in it.
     49         return inflater.inflate(R.layout.fragment_fragment_transition, container, false);
     50     }
     51 
     52     @Override
     53     public void onViewCreated(View view, Bundle savedInstanceState) {
     54         GridView grid = (GridView) view.findViewById(R.id.grid);
     55         grid.setAdapter(mAdapter);
     56         grid.setOnItemClickListener(this);
     57     }
     58 
     59     @Override
     60     public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
     61         Meat meat = mAdapter.getItem(position);
     62         Log.i(TAG, meat.title + " clicked. Replacing fragment.");
     63         // We start the fragment transaction here. It is just an ordinary fragment transaction.
     64         getActivity().getSupportFragmentManager()
     65                 .beginTransaction()
     66                 .replace(R.id.sample_content_fragment,
     67                         DetailFragment.newInstance(meat.resourceId, meat.title,
     68                                 (int) view.getX(), (int) view.getY(),
     69                                 view.getWidth(), view.getHeight())
     70                 )
     71                 // We push the fragment transaction to back stack. User can go back to the
     72                 // previous fragment by pressing back button.
     73                 .addToBackStack("detail")
     74                 .commit();
     75     }
     76 
     77     @Override
     78     public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
     79         return AnimationUtils.loadAnimation(getActivity(),
     80                 enter ? android.R.anim.fade_in : android.R.anim.fade_out);
     81     }
     82 
     83 }
     84