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.android.setupwizardlib.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.android.setupwizardlib.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 final RectF stickyRect = new RectF(); 59 StickyHeaderListView(Context context)60 public StickyHeaderListView(Context context) { 61 super(context); 62 init(null, android.R.attr.listViewStyle); 63 } 64 StickyHeaderListView(Context context, AttributeSet attrs)65 public StickyHeaderListView(Context context, AttributeSet attrs) { 66 super(context, attrs); 67 init(attrs, android.R.attr.listViewStyle); 68 } 69 StickyHeaderListView(Context context, AttributeSet attrs, int defStyleAttr)70 public StickyHeaderListView(Context context, AttributeSet attrs, int defStyleAttr) { 71 super(context, attrs, defStyleAttr); 72 init(attrs, defStyleAttr); 73 } 74 init(AttributeSet attrs, int defStyleAttr)75 private void init(AttributeSet attrs, int defStyleAttr) { 76 final TypedArray a = 77 getContext() 78 .obtainStyledAttributes(attrs, R.styleable.SuwStickyHeaderListView, defStyleAttr, 0); 79 int headerResId = a.getResourceId(R.styleable.SuwStickyHeaderListView_suwHeader, 0); 80 if (headerResId != 0) { 81 LayoutInflater inflater = LayoutInflater.from(getContext()); 82 View header = inflater.inflate(headerResId, this, false); 83 addHeaderView(header, null, false); 84 } 85 a.recycle(); 86 } 87 88 @Override onLayout(boolean changed, int l, int t, int r, int b)89 protected void onLayout(boolean changed, int l, int t, int r, int b) { 90 super.onLayout(changed, l, t, r, b); 91 if (sticky == null) { 92 updateStickyView(); 93 } 94 } 95 updateStickyView()96 public void updateStickyView() { 97 sticky = findViewWithTag("sticky"); 98 stickyContainer = findViewWithTag("stickyContainer"); 99 } 100 101 @Override dispatchTouchEvent(MotionEvent ev)102 public boolean dispatchTouchEvent(MotionEvent ev) { 103 if (stickyRect.contains(ev.getX(), ev.getY())) { 104 ev.offsetLocation(-stickyRect.left, -stickyRect.top); 105 return stickyContainer.dispatchTouchEvent(ev); 106 } else { 107 return super.dispatchTouchEvent(ev); 108 } 109 } 110 111 @Override draw(Canvas canvas)112 public void draw(Canvas canvas) { 113 super.draw(canvas); 114 if (sticky != null) { 115 final int saveCount = canvas.save(); 116 // The view to draw when sticking to the top 117 final View drawTarget = stickyContainer != null ? stickyContainer : sticky; 118 // The offset to draw the view at when sticky 119 final int drawOffset = stickyContainer != null ? sticky.getTop() : 0; 120 // Position of the draw target, relative to the outside of the scrollView 121 final int drawTop = drawTarget.getTop(); 122 if (drawTop + drawOffset < statusBarInset || !drawTarget.isShown()) { 123 // ListView does not translate the canvas, so we can simply draw at the top 124 stickyRect.set( 125 0, 126 -drawOffset + statusBarInset, 127 drawTarget.getWidth(), 128 drawTarget.getHeight() - drawOffset + statusBarInset); 129 canvas.translate(0, stickyRect.top); 130 canvas.clipRect(0, 0, drawTarget.getWidth(), drawTarget.getHeight()); 131 drawTarget.draw(canvas); 132 } else { 133 stickyRect.setEmpty(); 134 } 135 canvas.restoreToCount(saveCount); 136 } 137 } 138 139 @Override 140 @TargetApi(Build.VERSION_CODES.LOLLIPOP) onApplyWindowInsets(WindowInsets insets)141 public WindowInsets onApplyWindowInsets(WindowInsets insets) { 142 if (getFitsSystemWindows()) { 143 statusBarInset = insets.getSystemWindowInsetTop(); 144 insets.replaceSystemWindowInsets( 145 insets.getSystemWindowInsetLeft(), 146 0, /* top */ 147 insets.getSystemWindowInsetRight(), 148 insets.getSystemWindowInsetBottom()); 149 } 150 return insets; 151 } 152 153 @Override onInitializeAccessibilityEvent(AccessibilityEvent event)154 public void onInitializeAccessibilityEvent(AccessibilityEvent event) { 155 super.onInitializeAccessibilityEvent(event); 156 157 // Decoration-only headers should not count as an item for accessibility, adjust the 158 // accessibility event to account for that. 159 final int numberOfHeaders = sticky != null ? 1 : 0; 160 event.setItemCount(event.getItemCount() - numberOfHeaders); 161 event.setFromIndex(Math.max(event.getFromIndex() - numberOfHeaders, 0)); 162 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) { 163 event.setToIndex(Math.max(event.getToIndex() - numberOfHeaders, 0)); 164 } 165 } 166 } 167