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 */ 16package com.example.android.supportv7.widget.selection.simple; 17 18import android.graphics.Rect; 19import android.view.MotionEvent; 20import android.widget.LinearLayout; 21import android.widget.TextView; 22 23import androidx.annotation.Nullable; 24import androidx.recyclerview.selection.ItemDetailsLookup.ItemDetails; 25import androidx.recyclerview.widget.RecyclerView; 26 27import com.example.android.supportv7.R; 28 29final class DemoHolder extends RecyclerView.ViewHolder { 30 31 private final LinearLayout mContainer; 32 private final TextView mSelector; 33 private final TextView mLabel; 34 private final ItemDetails<Long> mDetails; 35 private @Nullable Long mKey; 36 37 DemoHolder(LinearLayout layout) { 38 super(layout); 39 mContainer = layout.findViewById(R.id.container); 40 mSelector = layout.findViewById(R.id.selector); 41 mLabel = layout.findViewById(R.id.label); 42 mDetails = new ItemDetails<Long>() { 43 @Override 44 public int getPosition() { 45 return DemoHolder.this.getAdapterPosition(); 46 } 47 48 @Override 49 public Long getSelectionKey() { 50 return DemoHolder.this.getItemId(); 51 } 52 53 @Override 54 public boolean inDragRegion(MotionEvent e) { 55 return DemoHolder.this.inDragRegion(e); 56 } 57 58 @Override 59 public boolean inSelectionHotspot(MotionEvent e) { 60 return DemoHolder.this.inSelectRegion(e); 61 } 62 }; 63 } 64 65 void update(String label, boolean selected) { 66 mLabel.setText(label); 67 setSelected(selected); 68 } 69 70 private void setSelected(boolean selected) { 71 mContainer.setActivated(selected); 72 mSelector.setActivated(selected); 73 } 74 75 boolean inDragRegion(MotionEvent event) { 76 // If itemView is activated = selected, then whole region is interactive 77 if (itemView.isActivated()) { 78 return true; 79 } 80 81 // Do everything in global coordinates - it makes things simpler. 82 int[] coords = new int[2]; 83 mSelector.getLocationOnScreen(coords); 84 85 Rect textBounds = new Rect(); 86 mLabel.getPaint().getTextBounds( 87 mLabel.getText().toString(), 0, mLabel.getText().length(), textBounds); 88 89 Rect rect = new Rect( 90 coords[0], 91 coords[1], 92 coords[0] + mSelector.getWidth() + textBounds.width(), 93 coords[1] + Math.max(mSelector.getHeight(), textBounds.height())); 94 95 // If the tap occurred inside icon or the text, these are interactive spots. 96 return rect.contains((int) event.getRawX(), (int) event.getRawY()); 97 } 98 99 boolean inSelectRegion(MotionEvent e) { 100 Rect iconRect = new Rect(); 101 mSelector.getGlobalVisibleRect(iconRect); 102 return iconRect.contains((int) e.getRawX(), (int) e.getRawY()); 103 } 104 105 ItemDetails<Long> getItemDetails() { 106 return mDetails; 107 } 108} 109