1/* 2 * Copyright 2018 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.os.Bundle; 21import android.util.Log; 22import android.view.LayoutInflater; 23import android.view.View; 24import android.view.ViewGroup; 25import android.widget.TextView; 26 27import androidx.car.widget.AlphaJumpBucketer; 28import androidx.car.widget.IAlphaJumpAdapter; 29import androidx.car.widget.PagedListView; 30import androidx.recyclerview.widget.RecyclerView; 31 32import java.util.Arrays; 33import java.util.Collection; 34 35/** 36 * An activity with a long list of cheeses, initially in a random order but you can use alpha jump 37 * to quickly jump to your favourite cheese. 38 */ 39public class AlphaJumpActivity extends Activity { 40 private static final String TAG = "AlphaJumpActivity"; 41 42 private static final String[] CHEESES = { 43 "Pourly", "Macconais", "Bonchester", "Olivet Cendre", "Fruit Cream Cheese", 44 "Metton (Cancoillotte)", "Lyonnais", "Crema Agria", "Nantais", 45 "Brusselae Kaas (Fromage de Bruxelles)", "Rouleau De Beaulieu", "Flor de Guia", 46 "Poivre d'Ane", "Tomme des Chouans", "Whitestone Farmhouse", "Queso de Murcia", 47 "Saint-Marcellin", "Pave d'Affinois", "Quatre-Vents", "Galette du Paludier", "Pyramide", 48 "Capricorn Goat", "Feta", "Queso del Montsec", "Telemea", "Cooleney", 49 "Buchette d'Anjou", "Banon", "Bosworth", "Bergader", "Mothais a la Feuille", 50 "Mascarpone Torta", "Richelieu", "Guerbigny", "Taupiniere", "Anneau du Vic-Bilh", 51 "Tupi", "Queso Fresco", "Timboon Brie", "Neufchatel", "Blue Castello", 52 "Brebis du Puyfaucon", "Gratte-Paille", "Palet de Babligny", "Caciotta", "Rigotte", 53 "Caciocavallo", "Bleu Des Causses", "Civray", "Bath Cheese", "Farmer", "Cachaille", 54 "Ricotta", "Caravane", "Selles sur Cher", "Chaource", "Cottage Cheese (Australian)", 55 "Pelardon des Corbieres", "Cold Pack", "Queso Fresco (Adobera)", "Bleu de Gex", 56 "Provel", "Torta del Casar", "Golden Cross", "Mascarpone", "Fougerus", 57 "Dessertnyj Belyj", "Fresh Ricotta", "Gris de Lille", "Breakfast Cheese", "Venaco", 58 "Pant ys Gawn", "Mascarpone (Australian)", "Sharpam", "Humboldt Fog", 59 "Evansdale Farmhouse Brie", "Kernhem", "Mozzarella Rolls", "Dolcelatte", 60 "Briquette de Brebis", "Niolo", "Selva", "Dunbarra", "King Island Cape Wickham Brie", 61 "Carre de l'Est", "Broccio", "Castelo Branco", "Finn", "Panela", "Basket Cheese", 62 "Woodside Cabecou", "Truffe", "Flower Marie", "Mozzarella Fresh, in water", 63 "Jubilee Blue", "Coeur de Camembert au Calvados", "Caprice des Dieux", "Caboc", 64 "Crottin du Chavignol", "Cabecou", "Cottage Cheese", "Cashel Blue", "Patefine Fort", 65 "Mahoe Aged Gouda", "Cornish Pepper", "Greuilh", "Ricotta (Australian)", "Grand Vatel", 66 "Prince-Jean", "Coulommiers", "Scamorza", "Romans Part Dieu", "Quark", "Frinault", 67 "Chabichou du Poitou", "Le Lacandou", "Maredsous", "Fin-de-Siecle", "Button (Innes)", 68 "Washed Rind Cheese (Australian)", "Daralagjazsky", "Margotin", "Pithtviers au Foin", 69 "Cathelain", "Yarra Valley Pyramid", "Sirene", "Emlett", "Explorateur", "Bandal", 70 "Lingot Saint Bousquet d'Orb", "Gorgonzola", "Bresse Bleu", "Beer Cheese", 71 "Brinza (Burduf Brinza)", "Bakers", "Little Rydings", "Bryndza" 72 }; 73 74 @Override 75 protected void onCreate(Bundle savedInstanceState) { 76 super.onCreate(savedInstanceState); 77 setContentView(R.layout.activity_paged_list_view); 78 79 PagedListView pagedListView = findViewById(R.id.paged_list_view); 80 pagedListView.setAdapter(new CheeseAdapter()); 81 } 82 83 /** 84 * Adapter that populates a number of items for demo purposes. 85 */ 86 public static class CheeseAdapter extends RecyclerView.Adapter<CheeseAdapter.ViewHolder> 87 implements IAlphaJumpAdapter { 88 private String[] mCheeses; 89 private boolean mIsSorted; 90 91 CheeseAdapter() { 92 // Start out not being sorted. 93 mCheeses = CHEESES; 94 mIsSorted = false; 95 } 96 97 @Override 98 public CheeseAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 99 LayoutInflater inflater = LayoutInflater.from(parent.getContext()); 100 View view = inflater.inflate(R.layout.alpha_jump_list_item, parent, false); 101 return new CheeseAdapter.ViewHolder(view); 102 } 103 104 @Override 105 public void onBindViewHolder(CheeseAdapter.ViewHolder holder, int position) { 106 holder.mTextView.setText(mCheeses[position]); 107 } 108 109 @Override 110 public int getItemCount() { 111 return mCheeses.length; 112 } 113 114 @Override 115 public Collection<Bucket> getAlphaJumpBuckets() { 116 if (!mIsSorted) { 117 Log.i(TAG, "Sorting..."); 118 // We'll sort the first time we need to populate the buckets. 119 mCheeses = mCheeses.clone(); 120 Arrays.sort(mCheeses); 121 mIsSorted = true; 122 notifyDataSetChanged(); 123 } 124 125 AlphaJumpBucketer bucketer = new AlphaJumpBucketer(); 126 return bucketer.createBuckets(mCheeses); 127 } 128 129 @Override 130 public void onAlphaJumpEnter() { 131 Log.i(TAG, "onAlphaJumpEnter"); 132 } 133 134 @Override 135 public void onAlphaJumpLeave(Bucket bucket) { 136 Log.i(TAG, "onAlphaJumpLeave: " + bucket.getLabel()); 137 } 138 139 /** 140 * ViewHolder for CheeseAdapter. 141 */ 142 public static class ViewHolder extends RecyclerView.ViewHolder { 143 private final TextView mTextView; 144 145 public ViewHolder(View itemView) { 146 super(itemView); 147 mTextView = itemView.findViewById(R.id.text); 148 } 149 } 150 } 151} 152