• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.squareup.picasso.Picasso;
20 
21 import android.app.Activity;
22 import android.content.Intent;
23 import android.os.Bundle;
24 import android.support.v4.app.ActivityCompat;
25 import android.support.v4.app.ActivityOptionsCompat;
26 import android.support.v4.util.Pair;
27 import android.view.View;
28 import android.view.ViewGroup;
29 import android.widget.AdapterView;
30 import android.widget.BaseAdapter;
31 import android.widget.GridView;
32 import android.widget.ImageView;
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     @Override
onCreate(Bundle savedInstanceState)46     public void onCreate(Bundle savedInstanceState) {
47         super.onCreate(savedInstanceState);
48         setContentView(R.layout.grid);
49 
50         // Setup the GridView and set the adapter
51         mGridView = (GridView) findViewById(R.id.grid);
52         mGridView.setOnItemClickListener(this);
53         mAdapter = new GridAdapter();
54         mGridView.setAdapter(mAdapter);
55     }
56 
57     /**
58      * Called when an item in the {@link android.widget.GridView} is clicked. Here will launch the
59      * {@link DetailActivity}, using the Scene Transition animation functionality.
60      */
61     @Override
onItemClick(AdapterView<?> adapterView, View view, int position, long id)62     public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
63         Item item = (Item) adapterView.getItemAtPosition(position);
64 
65         // Construct an Intent as normal
66         Intent intent = new Intent(this, DetailActivity.class);
67         intent.putExtra(DetailActivity.EXTRA_PARAM_ID, item.getId());
68 
69         // BEGIN_INCLUDE(start_activity)
70         /**
71          * Now create an {@link android.app.ActivityOptions} instance using the
72          * {@link ActivityOptionsCompat#makeSceneTransitionAnimation(Activity, Pair[])} factory
73          * method.
74          */
75         ActivityOptionsCompat activityOptions = ActivityOptionsCompat.makeSceneTransitionAnimation(
76                 this,
77 
78                 // Now we provide a list of Pair items which contain the view we can transitioning
79                 // from, and the name of the view it is transitioning to, in the launched activity
80                 new Pair<View, String>(view.findViewById(R.id.imageview_item),
81                         DetailActivity.VIEW_NAME_HEADER_IMAGE),
82                 new Pair<View, String>(view.findViewById(R.id.textview_name),
83                         DetailActivity.VIEW_NAME_HEADER_TITLE));
84 
85         // Now we can start the Activity, providing the activity options as a bundle
86         ActivityCompat.startActivity(this, intent, activityOptions.toBundle());
87         // END_INCLUDE(start_activity)
88     }
89 
90     /**
91      * {@link android.widget.BaseAdapter} which displays items.
92      */
93     private class GridAdapter extends BaseAdapter {
94 
95         @Override
getCount()96         public int getCount() {
97             return Item.ITEMS.length;
98         }
99 
100         @Override
getItem(int position)101         public Item getItem(int position) {
102             return Item.ITEMS[position];
103         }
104 
105         @Override
getItemId(int position)106         public long getItemId(int position) {
107             return getItem(position).getId();
108         }
109 
110         @Override
getView(int position, View view, ViewGroup viewGroup)111         public View getView(int position, View view, ViewGroup viewGroup) {
112             if (view == null) {
113                 view = getLayoutInflater().inflate(R.layout.grid_item, viewGroup, false);
114             }
115 
116             final Item item = getItem(position);
117 
118             // Load the thumbnail image
119             ImageView image = (ImageView) view.findViewById(R.id.imageview_item);
120             Picasso.with(image.getContext()).load(item.getThumbnailUrl()).into(image);
121 
122             // Set the TextView's contents
123             TextView name = (TextView) view.findViewById(R.id.textview_name);
124             name.setText(item.getName());
125 
126             return view;
127         }
128     }
129 }
130