• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.settings.common;
18 
19 import android.support.v7.widget.RecyclerView;
20 import android.text.TextUtils;
21 import android.view.LayoutInflater;
22 import android.view.View;
23 import android.view.ViewGroup;
24 import android.widget.ImageView;
25 import android.widget.TextView;
26 
27 import com.android.car.settings.R;
28 
29 /**
30  * Contains logic for a line item represents icon and texts of a title and a description.
31  */
32 public abstract class IconTextLineItem
33         extends TypedPagedListAdapter.LineItem<IconTextLineItem.ViewHolder> {
34     private final CharSequence mTitle;
35 
36     private View.OnClickListener mOnClickListener = (v) -> onClick();
37 
IconTextLineItem(CharSequence title)38     public IconTextLineItem(CharSequence title) {
39         mTitle = title;
40     }
41 
42     @Override
getType()43     public int getType() {
44         return ICON_TEXT_TYPE;
45     }
46 
47     @Override
bindViewHolder(ViewHolder viewHolder)48     public void bindViewHolder(ViewHolder viewHolder) {
49         viewHolder.titleView.setText(mTitle);
50         setIcon(viewHolder.iconView);
51         CharSequence desc = getDesc();
52         if (TextUtils.isEmpty(desc)) {
53             viewHolder.descView.setVisibility(View.GONE);
54         } else {
55             viewHolder.descView.setVisibility(View.VISIBLE);
56             viewHolder.descView.setText(desc);
57         }
58         viewHolder.itemView.setOnClickListener(mOnClickListener);
59         viewHolder.rightArrow.setVisibility(
60                 isExpandable() ? View.VISIBLE : View.INVISIBLE);
61         viewHolder.dividerLine.setVisibility(
62                 isClickable() && isEnabled() ? View.VISIBLE : View.INVISIBLE);
63     }
64 
65     public static class ViewHolder extends RecyclerView.ViewHolder {
66         public final TextView titleView;
67         final TextView descView;
68         final ImageView iconView;
69         final ImageView rightArrow;
70         public final View dividerLine;
71 
ViewHolder(View view)72         public ViewHolder(View view) {
73             super(view);
74             iconView = (ImageView) view.findViewById(R.id.icon);
75             titleView = (TextView) view.findViewById(R.id.title);
76             descView = (TextView) view.findViewById(R.id.desc);
77             rightArrow = (ImageView) view.findViewById(R.id.right_chevron);
78             dividerLine = view.findViewById(R.id.line_item_divider);
79         }
80     }
81 
createViewHolder(ViewGroup parent)82     public static RecyclerView.ViewHolder createViewHolder(ViewGroup parent) {
83         View v = LayoutInflater.from(parent.getContext())
84                 .inflate(R.layout.icon_text_line_item, parent, false);
85         return new ViewHolder(v);
86     }
87 
setIcon(ImageView iconView)88     public abstract void setIcon(ImageView iconView);
89 
onClick()90     public abstract void onClick();
91 }
92