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.setupwizardlib.view; 18 19 import android.annotation.TargetApi; 20 import android.content.Context; 21 import android.graphics.Canvas; 22 import android.graphics.RectF; 23 import android.os.Build; 24 import android.util.AttributeSet; 25 import android.view.MotionEvent; 26 import android.view.View; 27 import android.view.WindowInsets; 28 29 /** 30 * This class provides sticky header functionality in a recycler view, to use with 31 * SetupWizardIllustration. To use this, add a header tagged with "sticky". The header will continue 32 * to be drawn when the sticky element hits the top of the view. 33 * 34 * <p>There are a few things to note: 35 * <ol> 36 * <li>The view does not work well with padding. b/16190933 37 * <li>If fitsSystemWindows is true, then this will offset the sticking position by the height of 38 * the system decorations at the top of the screen. 39 * </ol> 40 */ 41 public class StickyHeaderRecyclerView extends HeaderRecyclerView { 42 43 private View mSticky; 44 private int mStatusBarInset = 0; 45 private RectF mStickyRect = new RectF(); 46 StickyHeaderRecyclerView(Context context)47 public StickyHeaderRecyclerView(Context context) { 48 super(context); 49 } 50 StickyHeaderRecyclerView(Context context, AttributeSet attrs)51 public StickyHeaderRecyclerView(Context context, AttributeSet attrs) { 52 super(context, attrs); 53 } 54 StickyHeaderRecyclerView(Context context, AttributeSet attrs, int defStyleAttr)55 public StickyHeaderRecyclerView(Context context, AttributeSet attrs, int defStyleAttr) { 56 super(context, attrs, defStyleAttr); 57 } 58 59 @Override onLayout(boolean changed, int l, int t, int r, int b)60 protected void onLayout(boolean changed, int l, int t, int r, int b) { 61 super.onLayout(changed, l, t, r, b); 62 if (mSticky == null) { 63 updateStickyView(); 64 } 65 if (mSticky != null) { 66 final View headerView = getHeader(); 67 if (headerView != null && headerView.getHeight() == 0) { 68 headerView.layout(0, -headerView.getMeasuredHeight(), 69 headerView.getMeasuredWidth(), 0); 70 } 71 } 72 } 73 74 @Override onMeasure(int widthSpec, int heightSpec)75 protected void onMeasure(int widthSpec, int heightSpec) { 76 super.onMeasure(widthSpec, heightSpec); 77 if (mSticky != null) { 78 measureChild(getHeader(), widthSpec, heightSpec); 79 } 80 } 81 82 /** 83 * Call this method when the "sticky" view has changed, so this view can update its internal 84 * states as well. 85 */ updateStickyView()86 public void updateStickyView() { 87 final View header = getHeader(); 88 if (header != null) { 89 mSticky = header.findViewWithTag("sticky"); 90 } 91 } 92 93 @Override draw(Canvas canvas)94 public void draw(Canvas canvas) { 95 super.draw(canvas); 96 if (mSticky != null) { 97 final View headerView = getHeader(); 98 final int saveCount = canvas.save(); 99 // The view to draw when sticking to the top 100 final View drawTarget = headerView != null ? headerView : mSticky; 101 // The offset to draw the view at when sticky 102 final int drawOffset = headerView != null ? mSticky.getTop() : 0; 103 // Position of the draw target, relative to the outside of the scrollView 104 final int drawTop = drawTarget.getTop(); 105 if (drawTop + drawOffset < mStatusBarInset || !drawTarget.isShown()) { 106 // RecyclerView does not translate the canvas, so we can simply draw at the top 107 mStickyRect.set(0, -drawOffset + mStatusBarInset, drawTarget.getWidth(), 108 drawTarget.getHeight() - drawOffset + mStatusBarInset); 109 canvas.translate(0, mStickyRect.top); 110 canvas.clipRect(0, 0, drawTarget.getWidth(), drawTarget.getHeight()); 111 drawTarget.draw(canvas); 112 } else { 113 mStickyRect.setEmpty(); 114 } 115 canvas.restoreToCount(saveCount); 116 } 117 } 118 119 @Override 120 @TargetApi(Build.VERSION_CODES.LOLLIPOP) onApplyWindowInsets(WindowInsets insets)121 public WindowInsets onApplyWindowInsets(WindowInsets insets) { 122 if (getFitsSystemWindows()) { 123 mStatusBarInset = insets.getSystemWindowInsetTop(); 124 insets.replaceSystemWindowInsets( 125 insets.getSystemWindowInsetLeft(), 126 0, /* top */ 127 insets.getSystemWindowInsetRight(), 128 insets.getSystemWindowInsetBottom() 129 ); 130 } 131 return insets; 132 } 133 134 @Override dispatchTouchEvent(MotionEvent ev)135 public boolean dispatchTouchEvent(MotionEvent ev) { 136 if (mStickyRect.contains(ev.getX(), ev.getY())) { 137 ev.offsetLocation(-mStickyRect.left, -mStickyRect.top); 138 return getHeader().dispatchTouchEvent(ev); 139 } else { 140 return super.dispatchTouchEvent(ev); 141 } 142 } 143 } 144