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.android.supportv7.widget.selection.simple; 18 19import static androidx.core.util.Preconditions.checkArgument; 20 21import android.content.Context; 22import android.util.Log; 23import android.view.LayoutInflater; 24import android.view.View; 25import android.view.ViewGroup; 26import android.widget.LinearLayout; 27 28import androidx.recyclerview.selection.ItemKeyProvider; 29import androidx.recyclerview.selection.SelectionTracker; 30import androidx.recyclerview.widget.RecyclerView; 31 32import com.example.android.supportv7.Cheeses; 33import com.example.android.supportv7.R; 34 35final class SimpleSelectionDemoAdapter extends RecyclerView.Adapter<DemoHolder> { 36 37 private static final String TAG = "SelectionDemos"; 38 private final Context mContext; 39 private final ItemKeyProvider<Long> mKeyProvider; 40 41 // This should be replaced at "bind" time with a real test that 42 // asks SelectionTracker. 43 private SelectionTest mSelTest; 44 45 SimpleSelectionDemoAdapter(Context context, ItemKeyProvider<Long> keyProvider) { 46 mContext = context; 47 mKeyProvider = keyProvider; 48 mSelTest = new SelectionTest() { 49 @Override 50 public boolean isSelected(Long id) { 51 throw new IllegalStateException( 52 "Adapter must be initialized with SelectionTracker."); 53 } 54 }; 55 } 56 57 // Glue together SelectionTracker and the adapter. 58 public void bindSelectionHelper(final SelectionTracker<Long> selectionTracker) { 59 checkArgument(selectionTracker != null); 60 mSelTest = new SelectionTest() { 61 @Override 62 public boolean isSelected(Long id) { 63 return selectionTracker.isSelected(id); 64 } 65 }; 66 } 67 68 void loadData() { 69 onDataReady(); 70 } 71 72 private void onDataReady() { 73 notifyDataSetChanged(); 74 } 75 76 @Override 77 public int getItemCount() { 78 return Cheeses.sCheeseStrings.length; 79 } 80 81 @Override 82 public long getItemId(int position) { 83 return position; 84 } 85 86 @Override 87 public void onBindViewHolder(DemoHolder holder, int position) { 88 Long key = getItemId(position); 89 Log.v(TAG, "Just before rendering item position=" + position + ", key=" + key); 90 holder.update(Cheeses.sCheeseStrings[position], mSelTest.isSelected(key)); 91 } 92 93 @Override 94 public DemoHolder onCreateViewHolder(ViewGroup parent, int viewType) { 95 LinearLayout layout = inflateLayout(mContext, parent, R.layout.selection_demo_list_item); 96 return new DemoHolder(layout); 97 } 98 99 @SuppressWarnings("TypeParameterUnusedInFormals") // Convenience to avoid clumsy cast. 100 private static <V extends View> V inflateLayout( 101 Context context, ViewGroup parent, int layout) { 102 103 return (V) LayoutInflater.from(context).inflate(layout, parent, false); 104 } 105 106 private interface SelectionTest { 107 boolean isSelected(Long id); 108 } 109} 110