• 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.view.LayoutInflater;
21 import android.view.View;
22 import android.view.ViewGroup;
23 import android.widget.ImageView;
24 import android.widget.TextView;
25 import com.android.car.settings.R;
26 
27 /**
28  * Contains logic for a line item represents text only view of a title and a description.
29  */
30 public abstract class TextLineItem extends TypedPagedListAdapter.LineItem<TextLineItem.ViewHolder> {
31     protected final CharSequence mTitle;
32 
33     protected View.OnClickListener mOnClickListener = (v) -> onClick();
34 
TextLineItem(CharSequence title)35     public TextLineItem(CharSequence title) {
36         mTitle = title;
37     }
38 
39     @Override
getType()40     public int getType() {
41         return TEXT_TYPE;
42     }
43 
44     @Override
bindViewHolder(ViewHolder viewHolder)45     public void bindViewHolder(ViewHolder viewHolder) {
46         viewHolder.titleView.setText(mTitle);
47         viewHolder.descView.setText(getDesc());
48         viewHolder.itemView.setOnClickListener(mOnClickListener);
49         viewHolder.itemView.setEnabled(isEnabled());
50         viewHolder.titleView.setEnabled(isEnabled());
51         viewHolder.descView.setEnabled(isEnabled());
52         viewHolder.rightArrow.setEnabled(isEnabled());
53         viewHolder.rightArrow.setVisibility(
54                 isExpandable() ? View.VISIBLE : View.INVISIBLE);
55         // set the dividerLine to INVISIBLE instead of GONE so the content of the line item
56         // don't need to shift.
57         viewHolder.dividerLine.setVisibility(
58                 isClickable() && isEnabled() ? View.VISIBLE : View.INVISIBLE);
59     }
60 
61     public static class ViewHolder extends RecyclerView.ViewHolder {
62         public final TextView titleView;
63         public final TextView descView;
64         final ImageView rightArrow;
65         public final View dividerLine;
66 
ViewHolder(View view)67         public ViewHolder(View view) {
68             super(view);
69             titleView = (TextView) view.findViewById(R.id.title);
70             descView = (TextView) view.findViewById(R.id.desc);
71             rightArrow = (ImageView) view.findViewById(R.id.right_chevron);
72             dividerLine = view.findViewById(R.id.line_item_divider);
73         }
74     }
75 
createViewHolder(ViewGroup parent)76     public static RecyclerView.ViewHolder createViewHolder(ViewGroup parent) {
77         View v = LayoutInflater.from(parent.getContext())
78                 .inflate(R.layout.text_line_item, parent, false);
79         return new ViewHolder(v);
80     }
81 
onClick()82     public abstract void onClick();
83 }
84