1 /* 2 * Copyright (C) 2017 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.android.car.app; 18 19 import android.support.annotation.NonNull; 20 import android.support.annotation.Nullable; 21 import android.support.v7.widget.RecyclerView; 22 import android.view.View; 23 import android.widget.ImageView; 24 import android.widget.TextView; 25 26 import com.android.car.stream.ui.R; 27 28 /** 29 * Re-usable ViewHolder for displaying items in the Drawer PagedListView. 30 */ 31 public class DrawerItemViewHolder extends RecyclerView.ViewHolder { 32 private final ImageView mIcon; 33 private final TextView mTitle; 34 private final TextView mText; 35 private final ImageView mRightIcon; 36 DrawerItemViewHolder(View view)37 DrawerItemViewHolder(View view) { 38 super(view); 39 mIcon = view.findViewById(R.id.icon); 40 mTitle = view.findViewById(R.id.title); 41 if (mIcon == null || mTitle == null) { 42 throw new IllegalArgumentException("Missing required elements in provided view!"); 43 } 44 // Next two are optional and may be null. 45 mText = view.findViewById(R.id.text); 46 mRightIcon = view.findViewById(R.id.right_icon); 47 } 48 49 /** 50 * @return Icon ImageView from inflated layout. 51 */ 52 @NonNull getIcon()53 public ImageView getIcon() { 54 return mIcon; 55 } 56 57 /** 58 * @return Main title TextView from inflated layout. 59 */ 60 @NonNull getTitle()61 public TextView getTitle() { 62 return mTitle; 63 } 64 65 /** 66 * @return Main text TextView from inflated layout. Will be {@code null} for small and empty 67 * item layouts. 68 */ 69 @Nullable getText()70 public TextView getText() { 71 return mText; 72 } 73 74 /** 75 * @return Right-Icon ImageView from inflated layout. Will be {@code null} for empty-item 76 * layout. 77 */ 78 @Nullable getRightIcon()79 public ImageView getRightIcon() { 80 return mRightIcon; 81 } 82 83 /** 84 * Set click-listener on the view wrapped by this ViewHolder. 85 */ setItemClickListener(@ullable DrawerItemClickListener listener)86 void setItemClickListener(@Nullable DrawerItemClickListener listener) { 87 if (listener != null) { 88 itemView.setOnClickListener((unusedView) -> listener.onItemClick(getAdapterPosition())); 89 } else { 90 itemView.setOnClickListener(null); 91 } 92 } 93 } 94