2017-10-18 10:31:36 -07:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2017 The Android Open Source Project
|
|
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
package com.android.launcher3.allapps;
|
|
|
|
|
|
2017-11-14 11:32:00 -08:00
|
|
|
import android.animation.ValueAnimator;
|
2017-12-06 11:45:49 -08:00
|
|
|
import android.content.Context;
|
2017-12-06 13:03:54 -08:00
|
|
|
import android.graphics.Point;
|
2017-10-18 10:31:36 -07:00
|
|
|
import android.graphics.Rect;
|
|
|
|
|
import android.support.annotation.NonNull;
|
|
|
|
|
import android.support.annotation.Nullable;
|
|
|
|
|
import android.support.v7.widget.RecyclerView;
|
2017-12-06 11:45:49 -08:00
|
|
|
import android.util.AttributeSet;
|
2017-12-06 13:03:54 -08:00
|
|
|
import android.view.MotionEvent;
|
2017-10-18 10:31:36 -07:00
|
|
|
import android.view.View;
|
2017-11-14 11:32:00 -08:00
|
|
|
import android.view.ViewGroup;
|
2018-01-23 15:40:50 -08:00
|
|
|
import android.widget.LinearLayout;
|
2017-11-14 11:32:00 -08:00
|
|
|
import android.widget.RelativeLayout;
|
2017-10-18 10:31:36 -07:00
|
|
|
|
|
|
|
|
import com.android.launcher3.R;
|
|
|
|
|
|
2018-01-23 15:40:50 -08:00
|
|
|
public class FloatingHeaderView extends LinearLayout implements
|
2017-12-06 11:45:49 -08:00
|
|
|
ValueAnimator.AnimatorUpdateListener {
|
2017-10-18 10:31:36 -07:00
|
|
|
|
|
|
|
|
private final Rect mClip = new Rect(0, 0, Integer.MAX_VALUE, Integer.MAX_VALUE);
|
2017-11-14 11:32:00 -08:00
|
|
|
private final ValueAnimator mAnimator = ValueAnimator.ofInt(0, 0);
|
2017-12-06 13:03:54 -08:00
|
|
|
private final Point mTempOffset = new Point();
|
2017-12-06 11:45:49 -08:00
|
|
|
private final RecyclerView.OnScrollListener mOnScrollListener = new RecyclerView.OnScrollListener() {
|
|
|
|
|
@Override
|
|
|
|
|
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onScrolled(RecyclerView rv, int dx, int dy) {
|
2017-12-07 10:51:41 -08:00
|
|
|
if (rv != mCurrentRV) {
|
2017-12-06 11:45:49 -08:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (mAnimator.isStarted()) {
|
|
|
|
|
mAnimator.cancel();
|
|
|
|
|
}
|
2017-10-18 10:31:36 -07:00
|
|
|
|
2017-12-07 10:51:41 -08:00
|
|
|
int current = -mCurrentRV.getCurrentScrollY();
|
2017-12-06 11:45:49 -08:00
|
|
|
moved(current);
|
|
|
|
|
apply();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
private ViewGroup mTabLayout;
|
2017-11-27 13:10:44 -08:00
|
|
|
private AllAppsRecyclerView mMainRV;
|
|
|
|
|
private AllAppsRecyclerView mWorkRV;
|
2017-12-07 10:51:41 -08:00
|
|
|
private AllAppsRecyclerView mCurrentRV;
|
2017-12-06 13:03:54 -08:00
|
|
|
private ViewGroup mParent;
|
2017-12-07 10:51:41 -08:00
|
|
|
private boolean mHeaderCollapsed;
|
2017-10-18 10:31:36 -07:00
|
|
|
private int mSnappedScrolledY;
|
|
|
|
|
private int mTranslationY;
|
2017-12-06 13:03:54 -08:00
|
|
|
private boolean mForwardToRecyclerView;
|
2017-10-18 10:31:36 -07:00
|
|
|
|
2018-01-23 15:40:50 -08:00
|
|
|
protected int mMaxTranslation;
|
|
|
|
|
|
2017-12-06 11:45:49 -08:00
|
|
|
public FloatingHeaderView(@NonNull Context context) {
|
|
|
|
|
this(context, null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public FloatingHeaderView(@NonNull Context context, @Nullable AttributeSet attrs) {
|
|
|
|
|
super(context, attrs);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected void onFinishInflate() {
|
|
|
|
|
super.onFinishInflate();
|
|
|
|
|
mTabLayout = findViewById(R.id.tabs);
|
2017-11-14 11:32:00 -08:00
|
|
|
}
|
|
|
|
|
|
2018-01-23 15:40:50 -08:00
|
|
|
public void setup(AllAppsContainerView.AdapterHolder[] mAH, boolean tabsHidden) {
|
|
|
|
|
mTabLayout.setVisibility(tabsHidden ? View.GONE : View.VISIBLE);
|
2017-12-07 10:51:41 -08:00
|
|
|
mMainRV = setupRV(mMainRV, mAH[AllAppsContainerView.AdapterHolder.MAIN].recyclerView);
|
|
|
|
|
mWorkRV = setupRV(mWorkRV, mAH[AllAppsContainerView.AdapterHolder.WORK].recyclerView);
|
|
|
|
|
mParent = (ViewGroup) mMainRV.getParent();
|
2017-10-18 10:31:36 -07:00
|
|
|
setMainActive(true);
|
2017-12-12 16:00:22 -08:00
|
|
|
reset();
|
2017-11-14 11:32:00 -08:00
|
|
|
}
|
|
|
|
|
|
2017-11-27 13:10:44 -08:00
|
|
|
private AllAppsRecyclerView setupRV(AllAppsRecyclerView old, AllAppsRecyclerView updated) {
|
|
|
|
|
if (old != updated && updated != null ) {
|
2017-12-06 11:45:49 -08:00
|
|
|
updated.addOnScrollListener(mOnScrollListener);
|
2017-11-27 13:10:44 -08:00
|
|
|
}
|
|
|
|
|
return updated;
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-18 10:31:36 -07:00
|
|
|
public void setMainActive(boolean active) {
|
2017-12-07 10:51:41 -08:00
|
|
|
mCurrentRV = active ? mMainRV : mWorkRV;
|
2017-10-18 10:31:36 -07:00
|
|
|
}
|
|
|
|
|
|
2018-01-23 15:40:50 -08:00
|
|
|
public int getMaxTranslation() {
|
|
|
|
|
return mMaxTranslation;
|
2017-11-14 11:32:00 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private boolean canSnapAt(int currentScrollY) {
|
2018-01-23 15:40:50 -08:00
|
|
|
return Math.abs(currentScrollY) <= mMaxTranslation;
|
2017-10-18 10:31:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void moved(final int currentScrollY) {
|
2017-12-07 10:51:41 -08:00
|
|
|
if (mHeaderCollapsed) {
|
2017-10-18 10:31:36 -07:00
|
|
|
if (currentScrollY <= mSnappedScrolledY) {
|
2017-11-14 11:32:00 -08:00
|
|
|
if (canSnapAt(currentScrollY)) {
|
|
|
|
|
mSnappedScrolledY = currentScrollY;
|
|
|
|
|
}
|
2017-10-18 10:31:36 -07:00
|
|
|
} else {
|
2017-12-07 10:51:41 -08:00
|
|
|
mHeaderCollapsed = false;
|
2017-10-18 10:31:36 -07:00
|
|
|
}
|
|
|
|
|
mTranslationY = currentScrollY;
|
2017-12-07 10:51:41 -08:00
|
|
|
} else if (!mHeaderCollapsed) {
|
2017-10-18 10:31:36 -07:00
|
|
|
mTranslationY = currentScrollY - mSnappedScrolledY - mMaxTranslation;
|
|
|
|
|
|
|
|
|
|
// update state vars
|
|
|
|
|
if (mTranslationY >= 0) { // expanded: must not move down further
|
|
|
|
|
mTranslationY = 0;
|
|
|
|
|
mSnappedScrolledY = currentScrollY - mMaxTranslation;
|
|
|
|
|
} else if (mTranslationY <= -mMaxTranslation) { // hide or stay hidden
|
2017-12-07 10:51:41 -08:00
|
|
|
mHeaderCollapsed = true;
|
2018-02-28 14:16:38 -08:00
|
|
|
mSnappedScrolledY = -mMaxTranslation;
|
2017-10-18 10:31:36 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-23 15:40:50 -08:00
|
|
|
protected void applyScroll(int uncappedY, int currentY) { }
|
|
|
|
|
|
|
|
|
|
protected void apply() {
|
2017-11-14 11:32:00 -08:00
|
|
|
int uncappedTranslationY = mTranslationY;
|
2017-10-18 10:31:36 -07:00
|
|
|
mTranslationY = Math.max(mTranslationY, -mMaxTranslation);
|
2018-01-23 15:40:50 -08:00
|
|
|
applyScroll(uncappedTranslationY, mTranslationY);
|
2017-11-14 11:32:00 -08:00
|
|
|
mTabLayout.setTranslationY(mTranslationY);
|
2017-10-18 10:31:36 -07:00
|
|
|
mClip.top = mMaxTranslation + mTranslationY;
|
2017-11-14 11:32:00 -08:00
|
|
|
// clipping on a draw might cause additional redraw
|
2017-10-18 10:31:36 -07:00
|
|
|
mMainRV.setClipBounds(mClip);
|
|
|
|
|
if (mWorkRV != null) {
|
|
|
|
|
mWorkRV.setClipBounds(mClip);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-12 16:00:22 -08:00
|
|
|
public void reset() {
|
|
|
|
|
int translateTo = 0;
|
2017-11-14 11:32:00 -08:00
|
|
|
mAnimator.setIntValues(mTranslationY, translateTo);
|
|
|
|
|
mAnimator.addUpdateListener(this);
|
|
|
|
|
mAnimator.setDuration(150);
|
|
|
|
|
mAnimator.start();
|
2017-12-12 16:00:22 -08:00
|
|
|
mHeaderCollapsed = false;
|
|
|
|
|
mSnappedScrolledY = -mMaxTranslation;
|
2018-01-12 16:07:08 -08:00
|
|
|
mCurrentRV.scrollToTop();
|
2017-10-18 10:31:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public boolean isExpanded() {
|
2017-12-07 10:51:41 -08:00
|
|
|
return !mHeaderCollapsed;
|
2017-10-18 10:31:36 -07:00
|
|
|
}
|
|
|
|
|
|
2017-11-14 11:32:00 -08:00
|
|
|
@Override
|
|
|
|
|
public void onAnimationUpdate(ValueAnimator animation) {
|
|
|
|
|
mTranslationY = (Integer) animation.getAnimatedValue();
|
|
|
|
|
apply();
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-06 13:03:54 -08:00
|
|
|
@Override
|
|
|
|
|
public boolean onInterceptTouchEvent(MotionEvent ev) {
|
|
|
|
|
calcOffset(mTempOffset);
|
|
|
|
|
ev.offsetLocation(mTempOffset.x, mTempOffset.y);
|
2017-12-07 10:51:41 -08:00
|
|
|
mForwardToRecyclerView = mCurrentRV.onInterceptTouchEvent(ev);
|
2017-12-06 13:03:54 -08:00
|
|
|
ev.offsetLocation(-mTempOffset.x, -mTempOffset.y);
|
|
|
|
|
return mForwardToRecyclerView || super.onInterceptTouchEvent(ev);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean onTouchEvent(MotionEvent event) {
|
|
|
|
|
if (mForwardToRecyclerView) {
|
|
|
|
|
// take this view's and parent view's (view pager) location into account
|
|
|
|
|
calcOffset(mTempOffset);
|
|
|
|
|
event.offsetLocation(mTempOffset.x, mTempOffset.y);
|
|
|
|
|
try {
|
2017-12-07 10:51:41 -08:00
|
|
|
return mCurrentRV.onTouchEvent(event);
|
2017-12-06 13:03:54 -08:00
|
|
|
} finally {
|
|
|
|
|
event.offsetLocation(-mTempOffset.x, -mTempOffset.y);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
return super.onTouchEvent(event);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void calcOffset(Point p) {
|
2017-12-07 10:51:41 -08:00
|
|
|
p.x = getLeft() - mCurrentRV.getLeft() - mParent.getLeft();
|
|
|
|
|
p.y = getTop() - mCurrentRV.getTop() - mParent.getTop();
|
2017-12-06 13:03:54 -08:00
|
|
|
}
|
2017-10-18 10:31:36 -07:00
|
|
|
}
|
2017-12-06 11:45:49 -08:00
|
|
|
|
|
|
|
|
|