summaryrefslogtreecommitdiff
path: root/library/recyclerview/src/com/android/setupwizardlib/view/StickyHeaderRecyclerView.java
diff options
context:
space:
mode:
Diffstat (limited to 'library/recyclerview/src/com/android/setupwizardlib/view/StickyHeaderRecyclerView.java')
-rw-r--r--library/recyclerview/src/com/android/setupwizardlib/view/StickyHeaderRecyclerView.java174
1 files changed, 88 insertions, 86 deletions
diff --git a/library/recyclerview/src/com/android/setupwizardlib/view/StickyHeaderRecyclerView.java b/library/recyclerview/src/com/android/setupwizardlib/view/StickyHeaderRecyclerView.java
index d51ea56..a5fa69c 100644
--- a/library/recyclerview/src/com/android/setupwizardlib/view/StickyHeaderRecyclerView.java
+++ b/library/recyclerview/src/com/android/setupwizardlib/view/StickyHeaderRecyclerView.java
@@ -32,112 +32,114 @@ import android.view.WindowInsets;
* to be drawn when the sticky element hits the top of the view.
*
* <p>There are a few things to note:
+ *
* <ol>
* <li>The view does not work well with padding. b/16190933
* <li>If fitsSystemWindows is true, then this will offset the sticking position by the height of
- * the system decorations at the top of the screen.
+ * the system decorations at the top of the screen.
* </ol>
*/
public class StickyHeaderRecyclerView extends HeaderRecyclerView {
- private View mSticky;
- private int mStatusBarInset = 0;
- private RectF mStickyRect = new RectF();
+ private View sticky;
+ private int statusBarInset = 0;
+ private final RectF stickyRect = new RectF();
- public StickyHeaderRecyclerView(Context context) {
- super(context);
- }
+ public StickyHeaderRecyclerView(Context context) {
+ super(context);
+ }
- public StickyHeaderRecyclerView(Context context, AttributeSet attrs) {
- super(context, attrs);
- }
+ public StickyHeaderRecyclerView(Context context, AttributeSet attrs) {
+ super(context, attrs);
+ }
- public StickyHeaderRecyclerView(Context context, AttributeSet attrs, int defStyleAttr) {
- super(context, attrs, defStyleAttr);
- }
+ public StickyHeaderRecyclerView(Context context, AttributeSet attrs, int defStyleAttr) {
+ super(context, attrs, defStyleAttr);
+ }
- @Override
- protected void onLayout(boolean changed, int l, int t, int r, int b) {
- super.onLayout(changed, l, t, r, b);
- if (mSticky == null) {
- updateStickyView();
- }
- if (mSticky != null) {
- final View headerView = getHeader();
- if (headerView != null && headerView.getHeight() == 0) {
- headerView.layout(0, -headerView.getMeasuredHeight(),
- headerView.getMeasuredWidth(), 0);
- }
- }
+ @Override
+ protected void onLayout(boolean changed, int l, int t, int r, int b) {
+ super.onLayout(changed, l, t, r, b);
+ if (sticky == null) {
+ updateStickyView();
+ }
+ if (sticky != null) {
+ final View headerView = getHeader();
+ if (headerView != null && headerView.getHeight() == 0) {
+ headerView.layout(0, -headerView.getMeasuredHeight(), headerView.getMeasuredWidth(), 0);
+ }
}
+ }
- @Override
- protected void onMeasure(int widthSpec, int heightSpec) {
- super.onMeasure(widthSpec, heightSpec);
- if (mSticky != null) {
- measureChild(getHeader(), widthSpec, heightSpec);
- }
+ @Override
+ protected void onMeasure(int widthSpec, int heightSpec) {
+ super.onMeasure(widthSpec, heightSpec);
+ if (sticky != null) {
+ measureChild(getHeader(), widthSpec, heightSpec);
}
+ }
- /**
- * Call this method when the "sticky" view has changed, so this view can update its internal
- * states as well.
- */
- public void updateStickyView() {
- final View header = getHeader();
- if (header != null) {
- mSticky = header.findViewWithTag("sticky");
- }
+ /**
+ * Call this method when the "sticky" view has changed, so this view can update its internal
+ * states as well.
+ */
+ public void updateStickyView() {
+ final View header = getHeader();
+ if (header != null) {
+ sticky = header.findViewWithTag("sticky");
}
+ }
- @Override
- public void draw(Canvas canvas) {
- super.draw(canvas);
- if (mSticky != null) {
- final View headerView = getHeader();
- final int saveCount = canvas.save();
- // The view to draw when sticking to the top
- final View drawTarget = headerView != null ? headerView : mSticky;
- // The offset to draw the view at when sticky
- final int drawOffset = headerView != null ? mSticky.getTop() : 0;
- // Position of the draw target, relative to the outside of the scrollView
- final int drawTop = drawTarget.getTop();
- if (drawTop + drawOffset < mStatusBarInset || !drawTarget.isShown()) {
- // RecyclerView does not translate the canvas, so we can simply draw at the top
- mStickyRect.set(0, -drawOffset + mStatusBarInset, drawTarget.getWidth(),
- drawTarget.getHeight() - drawOffset + mStatusBarInset);
- canvas.translate(0, mStickyRect.top);
- canvas.clipRect(0, 0, drawTarget.getWidth(), drawTarget.getHeight());
- drawTarget.draw(canvas);
- } else {
- mStickyRect.setEmpty();
- }
- canvas.restoreToCount(saveCount);
- }
+ @Override
+ public void draw(Canvas canvas) {
+ super.draw(canvas);
+ if (sticky != null) {
+ final View headerView = getHeader();
+ final int saveCount = canvas.save();
+ // The view to draw when sticking to the top
+ final View drawTarget = headerView != null ? headerView : sticky;
+ // The offset to draw the view at when sticky
+ final int drawOffset = headerView != null ? sticky.getTop() : 0;
+ // Position of the draw target, relative to the outside of the scrollView
+ final int drawTop = drawTarget.getTop();
+ if (drawTop + drawOffset < statusBarInset || !drawTarget.isShown()) {
+ // RecyclerView does not translate the canvas, so we can simply draw at the top
+ stickyRect.set(
+ 0,
+ -drawOffset + statusBarInset,
+ drawTarget.getWidth(),
+ drawTarget.getHeight() - drawOffset + statusBarInset);
+ canvas.translate(0, stickyRect.top);
+ canvas.clipRect(0, 0, drawTarget.getWidth(), drawTarget.getHeight());
+ drawTarget.draw(canvas);
+ } else {
+ stickyRect.setEmpty();
+ }
+ canvas.restoreToCount(saveCount);
}
+ }
- @Override
- @TargetApi(Build.VERSION_CODES.LOLLIPOP)
- public WindowInsets onApplyWindowInsets(WindowInsets insets) {
- if (getFitsSystemWindows()) {
- mStatusBarInset = insets.getSystemWindowInsetTop();
- insets.replaceSystemWindowInsets(
- insets.getSystemWindowInsetLeft(),
- 0, /* top */
- insets.getSystemWindowInsetRight(),
- insets.getSystemWindowInsetBottom()
- );
- }
- return insets;
+ @Override
+ @TargetApi(Build.VERSION_CODES.LOLLIPOP)
+ public WindowInsets onApplyWindowInsets(WindowInsets insets) {
+ if (getFitsSystemWindows()) {
+ statusBarInset = insets.getSystemWindowInsetTop();
+ insets.replaceSystemWindowInsets(
+ insets.getSystemWindowInsetLeft(),
+ 0, /* top */
+ insets.getSystemWindowInsetRight(),
+ insets.getSystemWindowInsetBottom());
}
+ return insets;
+ }
- @Override
- public boolean dispatchTouchEvent(MotionEvent ev) {
- if (mStickyRect.contains(ev.getX(), ev.getY())) {
- ev.offsetLocation(-mStickyRect.left, -mStickyRect.top);
- return getHeader().dispatchTouchEvent(ev);
- } else {
- return super.dispatchTouchEvent(ev);
- }
+ @Override
+ public boolean dispatchTouchEvent(MotionEvent ev) {
+ if (stickyRect.contains(ev.getX(), ev.getY())) {
+ ev.offsetLocation(-stickyRect.left, -stickyRect.top);
+ return getHeader().dispatchTouchEvent(ev);
+ } else {
+ return super.dispatchTouchEvent(ev);
}
+ }
}