• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2019 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.ui.paintbooth.caruirecyclerview;
18 
19 import android.app.Activity;
20 import android.content.Context;
21 import android.os.Bundle;
22 import android.widget.Toast;
23 
24 import androidx.annotation.NonNull;
25 import androidx.recyclerview.widget.RecyclerView;
26 
27 import com.android.car.ui.FocusArea;
28 import com.android.car.ui.baselayout.Insets;
29 import com.android.car.ui.baselayout.InsetsChangedListener;
30 import com.android.car.ui.core.CarUi;
31 import com.android.car.ui.paintbooth.R;
32 import com.android.car.ui.recyclerview.CarUiContentListItem;
33 import com.android.car.ui.recyclerview.CarUiHeaderListItem;
34 import com.android.car.ui.recyclerview.CarUiListItem;
35 import com.android.car.ui.recyclerview.CarUiRecyclerView;
36 import com.android.car.ui.toolbar.NavButtonMode;
37 import com.android.car.ui.toolbar.ToolbarController;
38 
39 import java.util.ArrayList;
40 
41 /**
42  * Activity that shows {@link CarUiRecyclerView} with sample {@link CarUiContentListItem} entries
43  */
44 public class CarUiListItemActivity extends Activity implements InsetsChangedListener {
45 
46     private final ArrayList<CarUiListItem> mData = new ArrayList<>();
47     private RecyclerView.Adapter<? extends RecyclerView.ViewHolder> mAdapter;
48 
49     @Override
onCreate(Bundle savedInstanceState)50     protected void onCreate(Bundle savedInstanceState) {
51         super.onCreate(savedInstanceState);
52         setContentView(R.layout.car_ui_recycler_view_activity);
53 
54         ToolbarController toolbar = CarUi.requireToolbar(this);
55         toolbar.setTitle(getTitle());
56         toolbar.setNavButtonMode(NavButtonMode.BACK);
57 
58         CarUiRecyclerView recyclerView = findViewById(R.id.list);
59         mAdapter = CarUi.createListItemAdapter(this, generateSampleData());
60         recyclerView.setAdapter(mAdapter);
61     }
62 
generateSampleData()63     private ArrayList<CarUiListItem> generateSampleData() {
64         Context context = this;
65 
66         CarUiHeaderListItem header = new CarUiHeaderListItem(getString(R.string.first_header));
67         mData.add(header);
68 
69         CarUiContentListItem item = new CarUiContentListItem(CarUiContentListItem.Action.NONE);
70         item.setTitle(getString(R.string.test_title));
71         item.setBody(getString(R.string.test_body));
72         mData.add(item);
73 
74         item = new CarUiContentListItem(CarUiContentListItem.Action.NONE);
75         item.setTitle(getString(R.string.test_title_no_body));
76         mData.add(item);
77 
78         header = new CarUiHeaderListItem(getString(R.string.random_header),
79                 getString(R.string.header_with_body));
80         mData.add(header);
81 
82         item = new CarUiContentListItem(CarUiContentListItem.Action.NONE);
83         item.setBody(getString(R.string.test_body_no_title));
84         mData.add(item);
85 
86         item = new CarUiContentListItem(CarUiContentListItem.Action.NONE);
87         item.setTitle(getString(R.string.test_title));
88         item.setIcon(getDrawable(R.drawable.ic_launcher));
89         mData.add(item);
90 
91         item = new CarUiContentListItem(CarUiContentListItem.Action.NONE);
92         item.setTitle(getString(R.string.test_title));
93         item.setBody(getString(R.string.test_body));
94         item.setIcon(getDrawable(R.drawable.ic_launcher));
95         mData.add(item);
96 
97         item = new CarUiContentListItem(CarUiContentListItem.Action.NONE);
98         item.setTitle(getString(R.string.title_with_content_icon));
99         item.setPrimaryIconType(CarUiContentListItem.IconType.CONTENT);
100         item.setIcon(getDrawable(R.drawable.ic_sample_logo));
101         mData.add(item);
102 
103         item = new CarUiContentListItem(CarUiContentListItem.Action.NONE);
104         item.setTitle(getString(R.string.test_title));
105         item.setBody(getString(R.string.with_avatar_icon));
106         item.setIcon(getDrawable(R.drawable.ic_sample_logo));
107         item.setPrimaryIconType(CarUiContentListItem.IconType.AVATAR);
108 
109         item = new CarUiContentListItem(CarUiContentListItem.Action.NONE);
110         item.setTitle(getString(R.string.test_title));
111         item.setBody(getString(R.string.display_toast_on_click));
112         item.setIcon(getDrawable(R.drawable.ic_launcher));
113         item.setOnItemClickedListener(item1 -> {
114             Toast.makeText(context, "Item clicked", Toast.LENGTH_SHORT).show();
115         });
116         mData.add(item);
117 
118         item = new CarUiContentListItem(CarUiContentListItem.Action.CHECK_BOX);
119         item.setIcon(getDrawable(R.drawable.ic_launcher));
120         item.setTitle(getString(R.string.title_item_with_checkbox));
121         item.setBody(getString(R.string.toast_on_selection_changed));
122         item.setOnCheckedChangeListener(
123                 (listItem, isChecked) -> Toast.makeText(context,
124                         "Item checked state is: " + isChecked, Toast.LENGTH_SHORT).show());
125         mData.add(item);
126 
127         item = new CarUiContentListItem(CarUiContentListItem.Action.CHECK_BOX);
128         item.setIcon(getDrawable(R.drawable.ic_launcher));
129         item.setEnabled(false);
130         item.setTitle(getString(R.string.title_with_disabled_checkbox));
131         item.setBody(getString(R.string.click_should_have_no_effect));
132         item.setOnCheckedChangeListener(
133                 (listItem, isChecked) -> Toast.makeText(context,
134                         "Item checked state is: " + isChecked, Toast.LENGTH_SHORT).show());
135         mData.add(item);
136 
137         item = new CarUiContentListItem(CarUiContentListItem.Action.SWITCH);
138         item.setIcon(getDrawable(R.drawable.ic_launcher));
139         item.setBody(getString(R.string.body_item_with_switch));
140         item.setOnItemClickedListener(item1 -> {
141             Toast.makeText(context, "Click on item with switch", Toast.LENGTH_SHORT).show();
142         });
143         mData.add(item);
144 
145         item = new CarUiContentListItem(CarUiContentListItem.Action.CHECK_BOX);
146         item.setIcon(getDrawable(R.drawable.ic_launcher));
147         item.setTitle(getString(R.string.title_item_with_checkbox));
148         item.setBody(getString(R.string.item_initially_checked));
149         item.setChecked(true);
150         mData.add(item);
151 
152         CarUiContentListItem radioItem1 = new CarUiContentListItem(
153                 CarUiContentListItem.Action.RADIO_BUTTON);
154         CarUiContentListItem radioItem2 = new CarUiContentListItem(
155                 CarUiContentListItem.Action.RADIO_BUTTON);
156 
157         radioItem1.setTitle(getString(R.string.title_item_with_radio_button));
158         radioItem1.setBody(getString(R.string.item_initially_checked));
159         radioItem1.setChecked(false);
160         radioItem1.setOnCheckedChangeListener((listItem, isChecked) -> {
161             if (isChecked) {
162                 radioItem2.setChecked(false);
163                 mAdapter.notifyItemChanged(mData.indexOf(radioItem2));
164             }
165         });
166         mData.add(radioItem1);
167 
168         radioItem2.setIcon(getDrawable(R.drawable.ic_launcher));
169         radioItem2.setTitle(getString(R.string.item_mutually_exclusive_with_item_above));
170         radioItem2.setChecked(true);
171         radioItem2.setOnCheckedChangeListener((listItem, isChecked) -> {
172             if (isChecked) {
173                 radioItem1.setChecked(false);
174                 mAdapter.notifyItemChanged(mData.indexOf(radioItem1));
175             }
176         });
177         mData.add(radioItem2);
178 
179         item = new CarUiContentListItem(CarUiContentListItem.Action.ICON);
180         item.setIcon(getDrawable(R.drawable.ic_launcher));
181         item.setTitle(getString(R.string.test_title));
182         item.setBody(getString(R.string.random_body_text_with_action_divider));
183         item.setActionDividerVisible(true);
184         item.setSupplementalIcon(getDrawable(R.drawable.ic_launcher));
185         item.setChecked(true);
186         mData.add(item);
187 
188         item = new CarUiContentListItem(CarUiContentListItem.Action.ICON);
189         item.setIcon(getDrawable(R.drawable.ic_launcher));
190         item.setTitle(getString(R.string.null_supplement_icon));
191         item.setChecked(true);
192         mData.add(item);
193 
194         item = new CarUiContentListItem(CarUiContentListItem.Action.ICON);
195         item.setTitle(getString(R.string.supplemental_icon_with_listener));
196         item.setPrimaryIconType(CarUiContentListItem.IconType.CONTENT);
197         item.setIcon(getDrawable(R.drawable.ic_launcher));
198         item.setBody(getString(R.string.test_body));
199         item.setOnItemClickedListener(v -> Toast.makeText(context, "Clicked item",
200                 Toast.LENGTH_SHORT).show());
201         item.setSupplementalIcon(getDrawable(R.drawable.ic_launcher),
202                 v -> Toast.makeText(context, "Clicked supplemental icon",
203                         Toast.LENGTH_SHORT).show());
204         item.setChecked(true);
205         mData.add(item);
206 
207         item = new CarUiContentListItem(CarUiContentListItem.Action.CHEVRON);
208         item.setTitle(getString(R.string.item_with_chevron));
209         item.setBody(getString(R.string.test_body));
210         mData.add(item);
211 
212         return mData;
213     }
214 
215     @Override
onCarUiInsetsChanged(@onNull Insets insets)216     public void onCarUiInsetsChanged(@NonNull Insets insets) {
217         FocusArea focusArea = requireViewById(R.id.focus_area);
218         focusArea.setBoundsOffset(0, insets.getTop(), 0, insets.getBottom());
219         focusArea.setHighlightPadding(0, insets.getTop(), 0, insets.getBottom());
220         requireViewById(R.id.list)
221                 .setPadding(0, insets.getTop(), 0, insets.getBottom());
222         requireViewById(android.R.id.content)
223                 .setPadding(insets.getLeft(), 0, insets.getRight(), 0);
224     }
225 }
226