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