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.navigationdrawer; 18 19 import android.content.Context; 20 import android.support.v7.widget.RecyclerView; 21 import android.view.LayoutInflater; 22 import android.view.View; 23 import android.view.ViewGroup; 24 import android.widget.AdapterView; 25 import android.widget.TextView; 26 27 /** 28 * Adapter for the planet data used in our drawer menu, 29 */ 30 public class PlanetAdapter extends RecyclerView.Adapter<PlanetAdapter.ViewHolder> { 31 private String[] mDataset; 32 private OnItemClickListener mListener; 33 34 /** 35 * Interface for receiving click events from cells. 36 */ 37 public interface OnItemClickListener { onClick(View view, int position)38 public void onClick(View view, int position); 39 } 40 41 /** 42 * Custom viewholder for our planet views. 43 */ 44 public static class ViewHolder extends RecyclerView.ViewHolder { 45 public final TextView mTextView; 46 ViewHolder(TextView v)47 public ViewHolder(TextView v) { 48 super(v); 49 mTextView = v; 50 } 51 } 52 PlanetAdapter(String[] myDataset, OnItemClickListener listener)53 public PlanetAdapter(String[] myDataset, OnItemClickListener listener) { 54 mDataset = myDataset; 55 mListener = listener; 56 } 57 58 @Override onCreateViewHolder(ViewGroup parent, int viewType)59 public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 60 LayoutInflater vi = LayoutInflater.from(parent.getContext()); 61 View v = vi.inflate(R.layout.drawer_list_item, parent, false); 62 TextView tv = (TextView) v.findViewById(android.R.id.text1); 63 return new ViewHolder(tv); 64 } 65 66 @Override onBindViewHolder(ViewHolder holder, final int position)67 public void onBindViewHolder(ViewHolder holder, final int position) { 68 holder.mTextView.setText(mDataset[position]); 69 holder.mTextView.setOnClickListener(new View.OnClickListener() { 70 @Override 71 public void onClick(View view) { 72 mListener.onClick(view, position); 73 } 74 }); 75 } 76 77 @Override getItemCount()78 public int getItemCount() { 79 return mDataset.length; 80 } 81 } 82