Home | History | Annotate | Download | only in activityscenetransitionbasic
      1 /*
      2  * Copyright (C) 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.activityscenetransitionbasic;
     18 
     19 import com.android.volley.toolbox.ImageLoader;
     20 import com.android.volley.toolbox.NetworkImageView;
     21 import com.android.volley.toolbox.Volley;
     22 
     23 import android.app.Activity;
     24 import android.app.ActivityOptions;
     25 import android.content.Intent;
     26 import android.os.Bundle;
     27 import android.util.Pair;
     28 import android.view.View;
     29 import android.view.ViewGroup;
     30 import android.widget.AdapterView;
     31 import android.widget.BaseAdapter;
     32 import android.widget.GridView;
     33 import android.widget.TextView;
     34 
     35 /**
     36  * Our main Activity in this sample. Displays a grid of items which an image and title. When the
     37  * user clicks on an item, {@link DetailActivity} is launched, using the Activity Scene Transitions
     38  * framework to animatedly do so.
     39  */
     40 public class MainActivity extends Activity implements AdapterView.OnItemClickListener {
     41 
     42     private GridView mGridView;
     43     private GridAdapter mAdapter;
     44 
     45     private ImageLoader mImageLoader;
     46 
     47     @Override
     48     public void onCreate(Bundle savedInstanceState) {
     49         super.onCreate(savedInstanceState);
     50         setContentView(R.layout.grid);
     51 
     52         // Retrieve the ImageLoader we are going to use for NetworkImageView
     53         mImageLoader = new ImageLoader(Volley.newRequestQueue(this), ImageMemoryCache.INSTANCE);
     54 
     55         // Setup the GridView and set the adapter
     56         mGridView = (GridView) findViewById(R.id.grid);
     57         mGridView.setOnItemClickListener(this);
     58         mAdapter = new GridAdapter();
     59         mGridView.setAdapter(mAdapter);
     60     }
     61 
     62     /**
     63      * Called when an item in the {@link android.widget.GridView} is clicked. Here will launch the
     64      * {@link DetailActivity}, using the Scene Transition animation functionality.
     65      */
     66     @Override
     67     public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
     68         Item item = (Item) adapterView.getItemAtPosition(position);
     69 
     70         // Construct an Intent as normal
     71         Intent intent = new Intent(this, DetailActivity.class);
     72         intent.putExtra(DetailActivity.EXTRA_PARAM_ID, item.getId());
     73 
     74         // BEGIN_INCLUDE(start_activity)
     75         /**
     76          * Now create an {@link android.app.ActivityOptions} instance using the
     77          * {@link android.app.ActivityOptions#makeSceneTransitionAnimation(android.app.Activity, android.util.Pair[])} factory method.
     78          */
     79         ActivityOptions activityOptions = ActivityOptions.makeSceneTransitionAnimation(
     80                 this,
     81 
     82                 // Now we provide a list of Pair items which contain the view we can transitioning
     83                 // from, and the name of the view it is transitioning to, in the launched activity
     84                 new Pair<View, String>(
     85                         view.findViewById(R.id.imageview_item),
     86                         DetailActivity.VIEW_NAME_HEADER_IMAGE),
     87                 new Pair<View, String>(
     88                         view.findViewById(R.id.textview_name),
     89                         DetailActivity.VIEW_NAME_HEADER_TITLE)
     90         );
     91 
     92         // Now we can start the Activity, providing the activity options as a bundle
     93         startActivity(intent, activityOptions.toBundle());
     94         // END_INCLUDE(start_activity)
     95     }
     96 
     97     /**
     98      * {@link android.widget.BaseAdapter} which displays items.
     99      */
    100     private class GridAdapter extends BaseAdapter {
    101 
    102         @Override
    103         public int getCount() {
    104             return Item.ITEMS.length;
    105         }
    106 
    107         @Override
    108         public Item getItem(int position) {
    109             return Item.ITEMS[position];
    110         }
    111 
    112         @Override
    113         public long getItemId(int position) {
    114             return getItem(position).getId();
    115         }
    116 
    117         @Override
    118         public View getView(int position, View view, ViewGroup viewGroup) {
    119             if (view == null) {
    120                 view = getLayoutInflater().inflate(R.layout.grid_item, viewGroup, false);
    121             }
    122 
    123             final Item item = getItem(position);
    124 
    125             // Load the thumbnail image
    126             NetworkImageView image = (NetworkImageView) view.findViewById(R.id.imageview_item);
    127             image.setImageUrl(item.getThumbnailUrl(), mImageLoader);
    128 
    129             // Set the TextView's contents
    130             TextView name = (TextView) view.findViewById(R.id.textview_name);
    131             name.setText(item.getName());
    132 
    133             // BEGIN_INCLUDE(grid_set_view_name)
    134             /**
    135              * As we're in an adapter we need to set each view's name dynamically, using the
    136              * item's ID so that the names are unique.
    137              */
    138             image.setViewName("grid:image:" + item.getId());
    139             name.setViewName("grid:name:" + item.getId());
    140             // END_INCLUDE(grid_set_view_name)
    141 
    142             return view;
    143         }
    144     }
    145 }
    146