1 /* 2 * Copyright 2013 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.navigationdrawer; 18 19 import android.app.Activity; 20 import android.content.Intent; 21 import android.content.res.Resources; 22 import android.os.Bundle; 23 import android.view.View; 24 import android.view.ViewGroup; 25 import android.view.ViewTreeObserver; 26 import android.widget.AdapterView; 27 import android.widget.BaseAdapter; 28 import android.widget.GridView; 29 import android.widget.TextView; 30 31 /** 32 * A simple launcher activity offering access to the individual samples in this project. 33 */ 34 public class MainActivity extends Activity implements AdapterView.OnItemClickListener { 35 private Sample[] mSamples; 36 private GridView mGridView; 37 onCreate(Bundle savedInstanceState)38 protected void onCreate(Bundle savedInstanceState) { 39 super.onCreate(savedInstanceState); 40 setContentView(R.layout.activity_main); 41 42 // Prepare list of samples in this dashboard. 43 mSamples = new Sample[]{ 44 new Sample(R.string.navigationdraweractivity_title, R.string.navigationdraweractivity_description, 45 NavigationDrawerActivity.class), 46 }; 47 48 // Prepare the GridView 49 mGridView = (GridView) findViewById(android.R.id.list); 50 mGridView.setAdapter(new SampleAdapter()); 51 mGridView.setOnItemClickListener(this); 52 } 53 54 @Override onItemClick(AdapterView<?> container, View view, int position, long id)55 public void onItemClick(AdapterView<?> container, View view, int position, long id) { 56 startActivity(mSamples[position].intent); 57 } 58 59 private class SampleAdapter extends BaseAdapter { 60 @Override getCount()61 public int getCount() { 62 return mSamples.length; 63 } 64 65 @Override getItem(int position)66 public Object getItem(int position) { 67 return mSamples[position]; 68 } 69 70 @Override getItemId(int position)71 public long getItemId(int position) { 72 return mSamples[position].hashCode(); 73 } 74 75 @Override getView(int position, View convertView, ViewGroup container)76 public View getView(int position, View convertView, ViewGroup container) { 77 if (convertView == null) { 78 convertView = getLayoutInflater().inflate(R.layout.sample_dashboard_item, 79 container, false); 80 } 81 82 ((TextView) convertView.findViewById(android.R.id.text1)).setText( 83 mSamples[position].titleResId); 84 ((TextView) convertView.findViewById(android.R.id.text2)).setText( 85 mSamples[position].descriptionResId); 86 return convertView; 87 } 88 } 89 90 private class Sample { 91 int titleResId; 92 int descriptionResId; 93 Intent intent; 94 Sample(int titleResId, int descriptionResId, Intent intent)95 private Sample(int titleResId, int descriptionResId, Intent intent) { 96 this.intent = intent; 97 this.titleResId = titleResId; 98 this.descriptionResId = descriptionResId; 99 } 100 Sample(int titleResId, int descriptionResId, Class<? extends Activity> activityClass)101 private Sample(int titleResId, int descriptionResId, 102 Class<? extends Activity> activityClass) { 103 this(titleResId, descriptionResId, 104 new Intent(MainActivity.this, activityClass)); 105 } 106 } 107 } 108