TextListItemActivity.java revision 2d99408bc504324ae6038f57f8489d8ec75585bb
1/* 2 * Copyright 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.example.androidx.car; 18 19import android.app.Activity; 20import android.content.Context; 21import android.graphics.Point; 22import android.os.Bundle; 23import android.text.TextUtils; 24import android.view.View; 25import android.view.WindowManager; 26import android.widget.FrameLayout; 27import android.widget.Toast; 28 29import java.util.ArrayList; 30import java.util.Collections; 31import java.util.List; 32 33import androidx.car.widget.ListItem; 34import androidx.car.widget.ListItemAdapter; 35import androidx.car.widget.ListItemProvider; 36import androidx.car.widget.PagedListView; 37 38/** 39 * Demo activity for {@link ListItem}. 40 */ 41public class ListItemActivity extends Activity { 42 43 private static int pixelToDip(Context context, int pixels) { 44 return (int) (pixels / context.getResources().getDisplayMetrics().density); 45 } 46 47 PagedListView mPagedListView; 48 49 @Override 50 protected void onCreate(Bundle savedInstanceState) { 51 super.onCreate(savedInstanceState); 52 setContentView(R.layout.activity_paged_list_view); 53 54 mPagedListView = findViewById(R.id.paged_list_view); 55 56 ListItemAdapter adapter = new ListItemAdapter(this, 57 new SampleProvider(this), ListItemAdapter.BackgroundStyle.PANEL); 58 mPagedListView.setAdapter(adapter); 59 mPagedListView.setMaxPages(PagedListView.UNLIMITED_PAGES); 60 } 61 62 private static class SampleProvider extends ListItemProvider { 63 private Context mContext; 64 private List<ListItem> mItems; 65 66 private View.OnClickListener mOnClickListener = v -> 67 Toast.makeText(mContext, "Clicked!", Toast.LENGTH_SHORT).show(); 68 69 private View.OnClickListener mGetParentHeight = (v) -> { 70 int parentHeight = ((FrameLayout) v.getParent().getParent().getParent()).getHeight(); 71 Toast.makeText(v.getContext(), 72 "card height is " + pixelToDip(mContext, parentHeight) + " dp", 73 Toast.LENGTH_SHORT).show(); 74 }; 75 76 private ListItemProvider.ListProvider mListProvider; 77 78 SampleProvider(Context context) { 79 mContext = context; 80 mItems = new ArrayList<>(); 81 82 mItems.add(new ListItem.Builder(mContext) 83 .withOnClickListener(mOnClickListener) 84 .withPrimaryActionIcon(android.R.drawable.sym_def_app_icon, true) 85 .withTitle("clickable single line with full icon and one action") 86 .withAction("card height", true, mGetParentHeight) 87 .build()); 88 89 mItems.add(new ListItem.Builder(mContext) 90 .withTitle("primary action set by drawable") 91 .withPrimaryActionIcon(mContext.getDrawable(R.drawable.pressed_icon), true) 92 .withViewBinder(vh -> vh.getPrimaryIcon().setClickable(true)) 93 .build()); 94 95 mItems.add(new ListItem.Builder(mContext) 96 .withOnClickListener(mOnClickListener) 97 .withPrimaryActionIcon(android.R.drawable.sym_def_app_icon, false) 98 .withTitle("clickable single line with small icon and clickable end icon") 99 .withSupplementalIcon(android.R.drawable.sym_def_app_icon, true, 100 mGetParentHeight) 101 .build()); 102 103 mItems.add(new ListItem.Builder(mContext) 104 .withOnClickListener(mOnClickListener) 105 .withPrimaryActionEmptyIcon() 106 .withTitle("clickable single line with empty icon and end icon no divider") 107 .withSupplementalIcon(android.R.drawable.sym_def_app_icon, false) 108 .build()); 109 110 mItems.add(new ListItem.Builder(mContext) 111 .withTitle("title is single line and ellipsizes. " 112 + mContext.getString(R.string.long_text)) 113 .withSupplementalIcon(android.R.drawable.sym_def_app_icon, true) 114 .build()); 115 116 mItems.add(new ListItem.Builder(mContext) 117 .withPrimaryActionNoIcon() 118 .withTitle("single line with two actions and no divider") 119 .withActions("action 1", false, 120 (v) -> Toast.makeText( 121 v.getContext(), "action 1", Toast.LENGTH_SHORT).show(), 122 "action 2", false, 123 (v) -> Toast.makeText( 124 v.getContext(), "action 2", Toast.LENGTH_SHORT).show()) 125 .build()); 126 127 mItems.add(new ListItem.Builder(mContext) 128 .withPrimaryActionNoIcon() 129 .withTitle("single line with two actions and action 2 divider") 130 .withActions("action 1", false, 131 (v) -> Toast.makeText( 132 v.getContext(), "action 1", Toast.LENGTH_SHORT).show(), 133 "action 2", true, 134 (v) -> Toast.makeText( 135 v.getContext(), "action 2", Toast.LENGTH_SHORT).show()) 136 .build()); 137 138 mItems.add(new ListItem.Builder(mContext) 139 .withPrimaryActionNoIcon() 140 .withTitle("single line with divider between actions. " 141 + mContext.getString(R.string.long_text)) 142 .withActions("action 1", true, 143 (v) -> Toast.makeText( 144 v.getContext(), "action 1", Toast.LENGTH_SHORT).show(), 145 "action 2", false, 146 (v) -> Toast.makeText( 147 v.getContext(), "action 2", Toast.LENGTH_SHORT).show()) 148 .build()); 149 150 mItems.add(new ListItem.Builder(mContext) 151 .withPrimaryActionIcon(android.R.drawable.sym_def_app_icon, true) 152 .withTitle("double line with full icon and no end icon divider") 153 .withBody("one line text") 154 .withSupplementalIcon(android.R.drawable.sym_def_app_icon, false, 155 mGetParentHeight) 156 .build()); 157 158 mItems.add(new ListItem.Builder(mContext) 159 .withPrimaryActionIcon(android.R.drawable.sym_def_app_icon, false) 160 .withTitle("double line with small icon and one action") 161 .withBody("one line text") 162 .withAction("card height", true, mGetParentHeight) 163 .build()); 164 165 String tenChars = "Ten Chars."; 166 mItems.add(new ListItem.Builder(mContext) 167 .withPrimaryActionIcon(android.R.drawable.sym_def_app_icon, false) 168 .withTitle("Card with small icon and text longer than limit") 169 .withBody("some chars") 170 .withBody(TextUtils.join("", Collections.nCopies(20, tenChars))) 171 .withSupplementalIcon(android.R.drawable.sym_def_app_icon, true, 172 mGetParentHeight) 173 .build()); 174 175 mItems.add(new ListItem.Builder(mContext) 176 .withPrimaryActionEmptyIcon() 177 .withTitle("double line with empty primary icon." 178 + mContext.getString(R.string.long_text)) 179 .withBody("one line text as primary", true) 180 .withActions("screen size", false, (v) -> { 181 Context c = v.getContext(); 182 Point size = new Point(); 183 c.getSystemService(WindowManager.class).getDefaultDisplay().getSize(size); 184 185 Toast.makeText(v.getContext(), 186 String.format("%s x %s dp", pixelToDip(c, size.x), 187 pixelToDip(c, size.y)), Toast.LENGTH_SHORT).show(); 188 }, "card height", true, mGetParentHeight) 189 .build()); 190 191 mItems.add(new ListItem.Builder(mContext) 192 .withTitle("double line with no primary action and one divider") 193 .withBody("one line text as primary", true) 194 .withActions("screen size", false, (v) -> { 195 Context c = v.getContext(); 196 Point size = new Point(); 197 c.getSystemService(WindowManager.class).getDefaultDisplay().getSize(size); 198 199 Toast.makeText(v.getContext(), 200 String.format("%s x %s dp", pixelToDip(c, size.x), 201 pixelToDip(c, size.y)), Toast.LENGTH_SHORT).show(); 202 }, "card height", true, mGetParentHeight) 203 .build()); 204 205 mItems.add(new ListItem.Builder(mContext) 206 .withPrimaryActionIcon(android.R.drawable.sym_def_app_icon, true) 207 .withBody("Only body - no title is set") 208 .withAction("card height", true, mGetParentHeight) 209 .build()); 210 211 mItems.add(new ListItem.Builder(mContext) 212 .withPrimaryActionIcon(android.R.drawable.sym_def_app_icon, false) 213 .withBody("Only body - no title. " + mContext.getString(R.string.long_text)) 214 .build()); 215 216 mListProvider = new ListItemProvider.ListProvider(mItems); 217 } 218 219 @Override 220 public ListItem get(int position) { 221 return mListProvider.get(position); 222 } 223 224 @Override 225 public int size() { 226 return mListProvider.size(); 227 } 228 } 229} 230