1 /* 2 * Copyright (C) 2018 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.os.Bundle 22 import android.view.View 23 import android.view.ViewGroup 24 import android.widget.AdapterView 25 import android.widget.BaseAdapter 26 import android.widget.GridView 27 import android.widget.TextView 28 29 /** 30 * A simple launcher activity offering access to the individual samples in this project. 31 */ 32 class MainActivity : Activity(), AdapterView.OnItemClickListener { 33 34 private lateinit var samples: Array<Sample> 35 36 override fun onCreate(savedInstanceState: Bundle?) { 37 super.onCreate(savedInstanceState) 38 setContentView(R.layout.activity_main) 39 40 // Prepare list of samples in this dashboard. 41 samples = arrayOf(Sample(R.string.navigationdraweractivity_title, 42 R.string.navigationdraweractivity_description, 43 Intent(this, NavigationDrawerActivity::class.java))) 44 45 // Prepare the GridView. 46 findViewById<GridView>(android.R.id.list).run { 47 adapter = SampleAdapter() 48 onItemClickListener = this@MainActivity 49 } 50 } 51 52 override fun onItemClick(container: AdapterView<*>, view: View, position: Int, id: Long) { 53 startActivity(samples[position].intent) 54 } 55 56 private inner class SampleAdapter : BaseAdapter() { 57 58 override fun getCount() = samples.size 59 60 override fun getItem(position: Int) = samples[position] 61 62 override fun getItemId(position: Int) = samples[position].hashCode().toLong() 63 64 override fun getView(position: Int, convertView: View?, container: ViewGroup): View { 65 return (convertView ?: layoutInflater.inflate(R.layout.sample_dashboard_item, 66 container, false)).apply { 67 findViewById<TextView>(android.R.id.text1)?.setText( 68 samples[position].titleResId) 69 findViewById<TextView>(android.R.id.text2)?.setText( 70 samples[position].descriptionResId) 71 } 72 } 73 74 } 75 76 } 77