Home | History | Annotate | Download | only in view
      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.os.Build;
     22 import android.util.AttributeSet;
     23 import android.view.View;
     24 import android.view.WindowInsets;
     25 
     26 /**
     27  * This class provides sticky header functionality in a scroll view, to use with
     28  * SetupWizardIllustration. To use this, add a subview tagged with "sticky", or a subview tagged
     29  * with "stickyContainer" and one of its child tagged as "sticky". The sticky container will be
     30  * drawn when the sticky element hits the top of the view.
     31  *
     32  * There are a few things to note:
     33  * 1. The two supported scenarios are StickyHeaderScrollView -> subview (stickyContainer) -> sticky,
     34  *    and StickyHeaderScrollView -> container -> subview (sticky).
     35  *    The arrow (->) represents parent/child relationship and must be immediate child.
     36  * 2. If fitsSystemWindows is true, then this will offset the sticking position by the height of
     37  *    the system decorations at the top of the screen.
     38  * 3. For versions before Honeycomb, this will behave like a regular ScrollView.
     39  *
     40  * @see StickyHeaderListView
     41  */
     42 public class StickyHeaderScrollView extends BottomScrollView {
     43 
     44     private View mSticky;
     45     private View mStickyContainer;
     46     private int mStatusBarInset = 0;
     47 
     48     public StickyHeaderScrollView(Context context) {
     49         super(context);
     50     }
     51 
     52     public StickyHeaderScrollView(Context context, AttributeSet attrs) {
     53         super(context, attrs);
     54     }
     55 
     56     public StickyHeaderScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
     57         super(context, attrs, defStyleAttr);
     58     }
     59 
     60     @Override
     61     protected void onLayout(boolean changed, int l, int t, int r, int b) {
     62         super.onLayout(changed, l, t, r, b);
     63         if (mSticky == null) {
     64             updateStickyView();
     65         }
     66         updateStickyHeaderPosition();
     67     }
     68 
     69     public void updateStickyView() {
     70         mSticky = findViewWithTag("sticky");
     71         mStickyContainer = findViewWithTag("stickyContainer");
     72     }
     73 
     74     private void updateStickyHeaderPosition() {
     75         // Note: for pre-Honeycomb the header will not be moved, so this ScrollView essentially
     76         // behaves like a normal BottomScrollView.
     77         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
     78             if (mSticky != null) {
     79                 // The view to draw when sticking to the top
     80                 final View drawTarget = mStickyContainer != null ? mStickyContainer : mSticky;
     81                 // The offset to draw the view at when sticky
     82                 final int drawOffset = mStickyContainer != null ? mSticky.getTop() : 0;
     83                 // Position of the draw target, relative to the outside of the scrollView
     84                 final int drawTop = drawTarget.getTop() - getScrollY();
     85                 if (drawTop + drawOffset < mStatusBarInset || !drawTarget.isShown()) {
     86                     // ScrollView translates the whole canvas so we have to compensate for that
     87                     drawTarget.setTranslationY(getScrollY() - drawOffset);
     88                 } else {
     89                     drawTarget.setTranslationY(0);
     90                 }
     91             }
     92         }
     93     }
     94 
     95     @Override
     96     protected void onScrollChanged(int l, int t, int oldl, int oldt) {
     97         super.onScrollChanged(l, t, oldl, oldt);
     98         updateStickyHeaderPosition();
     99     }
    100 
    101     @Override
    102     @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    103     public WindowInsets onApplyWindowInsets(WindowInsets insets) {
    104         if (getFitsSystemWindows()) {
    105             mStatusBarInset = insets.getSystemWindowInsetTop();
    106             insets = insets.replaceSystemWindowInsets(
    107                     insets.getSystemWindowInsetLeft(),
    108                     0, /* top */
    109                     insets.getSystemWindowInsetRight(),
    110                     insets.getSystemWindowInsetBottom()
    111             );
    112         }
    113         return insets;
    114     }
    115 }
    116