1 /* 2 * Copyright (C) 2015 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 package com.android.launcher3.allapps; 17 18 import static android.view.HapticFeedbackConstants.CLOCK_TICK; 19 20 import androidx.recyclerview.widget.LinearSmoothScroller; 21 import androidx.recyclerview.widget.RecyclerView.ViewHolder; 22 23 import com.android.launcher3.allapps.AlphabeticalAppsList.FastScrollSectionInfo; 24 25 public class AllAppsFastScrollHelper { 26 27 private static final int NO_POSITION = -1; 28 29 private int mTargetFastScrollPosition = NO_POSITION; 30 31 private AllAppsRecyclerView mRv; 32 private ViewHolder mLastSelectedViewHolder; 33 AllAppsFastScrollHelper(AllAppsRecyclerView rv)34 public AllAppsFastScrollHelper(AllAppsRecyclerView rv) { 35 mRv = rv; 36 } 37 38 /** 39 * Smooth scrolls the recycler view to the given section. 40 */ smoothScrollToSection(FastScrollSectionInfo info)41 public void smoothScrollToSection(FastScrollSectionInfo info) { 42 if (mTargetFastScrollPosition == info.position) { 43 return; 44 } 45 mTargetFastScrollPosition = info.position; 46 mRv.getLayoutManager().startSmoothScroll(new MyScroller(mTargetFastScrollPosition)); 47 } 48 onFastScrollCompleted()49 public void onFastScrollCompleted() { 50 mTargetFastScrollPosition = NO_POSITION; 51 setLastHolderSelected(false); 52 mLastSelectedViewHolder = null; 53 } 54 55 setLastHolderSelected(boolean isSelected)56 private void setLastHolderSelected(boolean isSelected) { 57 if (mLastSelectedViewHolder != null) { 58 mLastSelectedViewHolder.itemView.setActivated(isSelected); 59 mLastSelectedViewHolder.setIsRecyclable(!isSelected); 60 } 61 } 62 63 private class MyScroller extends LinearSmoothScroller { 64 65 private final int mTargetPosition; 66 MyScroller(int targetPosition)67 public MyScroller(int targetPosition) { 68 super(mRv.getContext()); 69 70 mTargetPosition = targetPosition; 71 setTargetPosition(targetPosition); 72 } 73 74 @Override getVerticalSnapPreference()75 protected int getVerticalSnapPreference() { 76 mRv.performHapticFeedback(CLOCK_TICK); 77 return SNAP_TO_ANY; 78 } 79 80 @Override onStop()81 protected void onStop() { 82 super.onStop(); 83 if (mTargetPosition != mTargetFastScrollPosition) { 84 // Target changed, before the last scroll can finish 85 return; 86 } 87 88 ViewHolder currentHolder = mRv.findViewHolderForAdapterPosition(mTargetPosition); 89 if (currentHolder == mLastSelectedViewHolder) { 90 return; 91 } 92 93 setLastHolderSelected(false); 94 mLastSelectedViewHolder = currentHolder; 95 setLastHolderSelected(true); 96 } 97 98 @Override onStart()99 protected void onStart() { 100 super.onStart(); 101 if (mTargetPosition != mTargetFastScrollPosition) { 102 setLastHolderSelected(false); 103 mLastSelectedViewHolder = null; 104 } 105 } 106 } 107 } 108