Home | History | Annotate | Download | only in app
      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 
     37     DrawerItemViewHolder(View view) {
     38         super(view);
     39         mIcon = (ImageView)view.findViewById(R.id.icon);
     40         mTitle = (TextView)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 = (TextView)view.findViewById(R.id.text);
     46         mRightIcon = (ImageView)view.findViewById(R.id.right_icon);
     47     }
     48 
     49     /**
     50      * @return Icon ImageView from inflated layout.
     51      */
     52     @NonNull
     53     public ImageView getIcon() {
     54         return mIcon;
     55     }
     56 
     57     /**
     58      * @return Main title TextView from inflated layout.
     59      */
     60     @NonNull
     61     public TextView getTitle() {
     62         return mTitle;
     63     }
     64 
     65     /**
     66      * @return Main text TextView from inflated layout. May be null.
     67      */
     68     @Nullable
     69     public TextView getText() {
     70         return mText;
     71     }
     72 
     73     /**
     74      * @return Right-Icon ImageView from inflated layout. May be null.
     75      */
     76     @Nullable
     77     public ImageView getRightIcon() {
     78         return mRightIcon;
     79     }
     80 
     81     /**
     82      * Set click-listener on the view wrapped by this ViewHolder.
     83      */
     84     void setItemClickListener(@Nullable DrawerItemClickListener listener) {
     85         if (listener != null) {
     86             itemView.setOnClickListener((unusedView) -> listener.onItemClick(getAdapterPosition()));
     87         } else {
     88             itemView.setOnClickListener(null);
     89         }
     90     }
     91 }
     92