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 17 package com.google.android.setupdesign.view; 18 19 import android.annotation.TargetApi; 20 import android.content.Context; 21 import android.content.res.TypedArray; 22 import android.graphics.Canvas; 23 import android.graphics.RectF; 24 import android.os.Build; 25 import android.util.AttributeSet; 26 import android.view.LayoutInflater; 27 import android.view.MotionEvent; 28 import android.view.View; 29 import android.view.WindowInsets; 30 import android.view.accessibility.AccessibilityEvent; 31 import android.widget.ListView; 32 import com.google.android.setupdesign.R; 33 34 /** 35 * This class provides sticky header functionality in a list view, to use with 36 * SetupWizardIllustration. To use this, add a header tagged with "sticky", or a header tagged with 37 * "stickyContainer" and one of its child tagged as "sticky". The sticky container will be drawn 38 * when the sticky element hits the top of the view. 39 * 40 * <p>There are a few things to note: 41 * 42 * <ol> 43 * <li>The two supported scenarios are StickyHeaderListView -> Header (stickyContainer) -> sticky, 44 * and StickyHeaderListView -> Header (sticky). The arrow (->) represents parent/child 45 * relationship and must be immediate child. 46 * <li>The view does not work well with padding. b/16190933 47 * <li>If fitsSystemWindows is true, then this will offset the sticking position by the height of 48 * the system decorations at the top of the screen. 49 * </ol> 50 * 51 * @see StickyHeaderScrollView 52 */ 53 public class StickyHeaderListView extends ListView { 54 55 private View sticky; 56 private View stickyContainer; 57 private int statusBarInset = 0; 58 private boolean shouldApplyAdditionalMargin; 59 private final RectF stickyRect = new RectF(); 60 StickyHeaderListView(Context context)61 public StickyHeaderListView(Context context) { 62 super(context); 63 init(null, android.R.attr.listViewStyle); 64 } 65 StickyHeaderListView(Context context, AttributeSet attrs)66 public StickyHeaderListView(Context context, AttributeSet attrs) { 67 super(context, attrs); 68 init(attrs, android.R.attr.listViewStyle); 69 } 70 StickyHeaderListView(Context context, AttributeSet attrs, int defStyleAttr)71 public StickyHeaderListView(Context context, AttributeSet attrs, int defStyleAttr) { 72 super(context, attrs, defStyleAttr); 73 init(attrs, defStyleAttr); 74 } 75 init(AttributeSet attrs, int defStyleAttr)76 private void init(AttributeSet attrs, int defStyleAttr) { 77 if (isInEditMode()) { 78 return; 79 } 80 81 final TypedArray a = 82 getContext() 83 .obtainStyledAttributes(attrs, R.styleable.SudStickyHeaderListView, defStyleAttr, 0); 84 int headerResId = a.getResourceId(R.styleable.SudStickyHeaderListView_sudHeader, 0); 85 86 shouldApplyAdditionalMargin = 87 a.getBoolean(R.styleable.SudStickyHeaderListView_sudShouldApplyAdditionalMargin, false); 88 if (headerResId != 0) { 89 LayoutInflater inflater = LayoutInflater.from(getContext()); 90 View header = inflater.inflate(headerResId, this, false); 91 addHeaderView(header, null, false); 92 } 93 a.recycle(); 94 } 95 96 @Override onLayout(boolean changed, int l, int t, int r, int b)97 protected void onLayout(boolean changed, int l, int t, int r, int b) { 98 super.onLayout(changed, l, t, r, b); 99 if (sticky == null) { 100 updateStickyView(); 101 } 102 } 103 shouldApplyAdditionalMargin()104 public boolean shouldApplyAdditionalMargin() { 105 return shouldApplyAdditionalMargin; 106 } 107 updateStickyView()108 public void updateStickyView() { 109 sticky = findViewWithTag("sticky"); 110 stickyContainer = findViewWithTag("stickyContainer"); 111 } 112 113 @Override dispatchTouchEvent(MotionEvent ev)114 public boolean dispatchTouchEvent(MotionEvent ev) { 115 if (stickyRect.contains(ev.getX(), ev.getY())) { 116 ev.offsetLocation(-stickyRect.left, -stickyRect.top); 117 return stickyContainer.dispatchTouchEvent(ev); 118 } else { 119 return super.dispatchTouchEvent(ev); 120 } 121 } 122 123 @Override draw(Canvas canvas)124 public void draw(Canvas canvas) { 125 super.draw(canvas); 126 if (sticky != null) { 127 final int saveCount = canvas.save(); 128 // The view to draw when sticking to the top 129 final View drawTarget = stickyContainer != null ? stickyContainer : sticky; 130 // The offset to draw the view at when sticky 131 final int drawOffset = stickyContainer != null ? sticky.getTop() : 0; 132 // Position of the draw target, relative to the outside of the scrollView 133 final int drawTop = drawTarget.getTop(); 134 if (drawTop + drawOffset < statusBarInset || !drawTarget.isShown()) { 135 // ListView does not translate the canvas, so we can simply draw at the top 136 stickyRect.set( 137 0, 138 -drawOffset + statusBarInset, 139 drawTarget.getWidth(), 140 drawTarget.getHeight() - drawOffset + statusBarInset); 141 canvas.translate(0, stickyRect.top); 142 canvas.clipRect(0, 0, drawTarget.getWidth(), drawTarget.getHeight()); 143 drawTarget.draw(canvas); 144 } else { 145 stickyRect.setEmpty(); 146 } 147 canvas.restoreToCount(saveCount); 148 } 149 } 150 151 @Override 152 @TargetApi(Build.VERSION_CODES.LOLLIPOP) onApplyWindowInsets(WindowInsets insets)153 public WindowInsets onApplyWindowInsets(WindowInsets insets) { 154 if (getFitsSystemWindows()) { 155 statusBarInset = insets.getSystemWindowInsetTop(); 156 insets.replaceSystemWindowInsets( 157 insets.getSystemWindowInsetLeft(), 158 0, /* top */ 159 insets.getSystemWindowInsetRight(), 160 insets.getSystemWindowInsetBottom()); 161 } 162 return insets; 163 } 164 165 @Override onInitializeAccessibilityEvent(AccessibilityEvent event)166 public void onInitializeAccessibilityEvent(AccessibilityEvent event) { 167 super.onInitializeAccessibilityEvent(event); 168 169 // Decoration-only headers should not count as an item for accessibility, adjust the 170 // accessibility event to account for that. 171 final int numberOfHeaders = sticky != null ? 1 : 0; 172 event.setItemCount(event.getItemCount() - numberOfHeaders); 173 event.setFromIndex(Math.max(event.getFromIndex() - numberOfHeaders, 0)); 174 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) { 175 event.setToIndex(Math.max(event.getToIndex() - numberOfHeaders, 0)); 176 } 177 } 178 } 179