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.support.v7.widget.RecyclerView 20 import android.view.LayoutInflater 21 import android.view.View 22 import android.view.ViewGroup 23 import android.widget.TextView 24 25 /** 26 * Adapter for the planet data used in our drawer menu. 27 */ 28 class PlanetAdapter( 29 private val dataset: Array<String>, 30 private val listener: OnItemClickListener 31 ) : RecyclerView.Adapter<PlanetAdapter.ViewHolder>() { 32 33 /** 34 * Interface for receiving click events from cells. 35 */ 36 interface OnItemClickListener { 37 fun onClick(view: View, position: Int) 38 } 39 40 /** 41 * Custom [ViewHolder] for our planet views. 42 */ 43 class ViewHolder(val textView: TextView) : RecyclerView.ViewHolder(textView) 44 45 override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = 46 ViewHolder(LayoutInflater.from(parent.context) 47 .inflate(R.layout.drawer_list_item, parent, false) 48 .findViewById(android.R.id.text1)) 49 50 override fun onBindViewHolder(holder: ViewHolder, position: Int) { 51 holder.apply { 52 textView.text = dataset[position] 53 textView.setOnClickListener { view -> listener.onClick(view, position) } 54 } 55 } 56 57 override fun getItemCount() = dataset.size 58 } 59