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 17package com.android.car.settings.common; 18 19import android.support.v7.widget.RecyclerView; 20import android.text.TextUtils; 21import android.view.LayoutInflater; 22import android.view.View; 23import android.view.ViewGroup; 24import android.widget.CheckBox; 25import android.widget.Switch; 26import android.widget.TextView; 27 28import com.android.car.settings.R; 29 30/** 31 * Contains logic for a line item represents title text and a checkbox. 32 */ 33public abstract class CheckBoxLineItem 34 extends TypedPagedListAdapter.LineItem<CheckBoxLineItem.CheckboxLineItemViewHolder> { 35 private final CharSequence mTitle; 36 37 private View.OnClickListener mOnClickListener = (v) -> { 38 CheckBox checkBox = v.findViewById(R.id.checkbox); 39 CheckBoxLineItem.this.onClick(checkBox.isChecked()); 40 }; 41 42 public CheckBoxLineItem(CharSequence title) { 43 mTitle = title; 44 } 45 46 public int getType() { 47 return CHECKBOX_TYPE; 48 } 49 50 public void bindViewHolder(CheckboxLineItemViewHolder viewHolder) { 51 viewHolder.titleView.setText(mTitle); 52 viewHolder.checkbox.setChecked(isChecked()); 53 viewHolder.itemView.setOnClickListener(mOnClickListener); 54 } 55 56 public static class CheckboxLineItemViewHolder extends RecyclerView.ViewHolder { 57 final TextView titleView; 58 public final CheckBox checkbox; 59 60 public CheckboxLineItemViewHolder(View view) { 61 super(view); 62 titleView = view.findViewById(R.id.title); 63 checkbox = view.findViewById(R.id.checkbox); 64 } 65 } 66 67 public static RecyclerView.ViewHolder createViewHolder(ViewGroup parent) { 68 View v = LayoutInflater.from(parent.getContext()) 69 .inflate(R.layout.checkbox_line_item, parent, false); 70 return new CheckboxLineItemViewHolder(v); 71 } 72 73 /** 74 * Called when any part of the line is clicked. 75 * @param isChecked the state of the switch widget at the time of click. 76 */ 77 public abstract void onClick(boolean isChecked); 78 79 public abstract boolean isChecked(); 80 81 @Override 82 public boolean isClickable() { 83 return true; 84 } 85 86 @Override 87 public CharSequence getDesc() { 88 return null; 89 } 90} 91