1 /* 2 * Copyright (c) 2016, 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 17 package com.android.car.radio; 18 19 import android.animation.ValueAnimator; 20 import android.content.Context; 21 import android.support.v7.widget.LinearLayoutManager; 22 import android.support.v7.widget.RecyclerView; 23 import android.view.View; 24 25 import androidx.car.widget.PagedListView; 26 27 /** 28 * Listener on the preset list that will add elevation on the container holding the current 29 * playing radio station. This elevation will give the illusion of the preset list scrolling 30 * under that container. 31 */ 32 public class PresetListScrollListener extends RecyclerView.OnScrollListener { 33 private static final int ANIMATION_DURATION_MS = 100; 34 35 private final float mContainerElevation; 36 private final View mCurrentRadioCardContainer; 37 private final View mCurrentRadioCard; 38 private final PagedListView mPresetList; 39 private final ValueAnimator mRemoveElevationAnimator; 40 41 public PresetListScrollListener(Context context, View container, View currentRadioCard, 42 PagedListView presetList) { 43 mPresetList = presetList; 44 mCurrentRadioCardContainer = container.findViewById(R.id.preset_current_card_container); 45 mCurrentRadioCard = currentRadioCard; 46 mContainerElevation = context.getResources() 47 .getDimension(R.dimen.car_preset_container_elevation); 48 49 mRemoveElevationAnimator = ValueAnimator.ofFloat(mContainerElevation, 0.f); 50 mRemoveElevationAnimator 51 .setDuration(ANIMATION_DURATION_MS) 52 .addUpdateListener(animation -> mCurrentRadioCardContainer.setElevation( 53 (float) animation.getAnimatedValue())); 54 } 55 56 @Override 57 public void onScrolled(RecyclerView recyclerView, int dx, int dy) { 58 if (mPresetList.getTranslationY() != 0.f) { 59 return; 60 } 61 62 // The default LayoutManager for PagedListView is a LinearLayoutManager. Radio does 63 // not change this. 64 LinearLayoutManager layoutManager = 65 (LinearLayoutManager) recyclerView.getLayoutManager(); 66 67 if (layoutManager.findFirstCompletelyVisibleItemPosition() == 0) { 68 // Animate the removal of the elevation so that it's not jarring. 69 mRemoveElevationAnimator.start(); 70 } else { 71 // No animation needed when adding the elevation because the scroll masks the adding 72 // of the elevation. 73 mCurrentRadioCardContainer.setElevation(mContainerElevation); 74 mCurrentRadioCard.setTranslationZ(mContainerElevation); 75 } 76 } 77 } 78