public static void navigate(int direction, AppBarLayout appBarLayout, Navigable navigable) { switch (direction) { case Navigable.DIRECTION_DOWN: case Navigable.DIRECTION_RIGHT: if (appBarLayout.getBottom() == 0) { navigable.onNavigate(direction); } else { appBarLayout.setExpanded(false, true); } break; default: navigable.onNavigate(direction); break; } }
@Test public void testNavigate() { AppBarLayout appBar = mock(AppBarLayout.class); when(appBar.getBottom()).thenReturn(1); Navigable navigable = mock(Navigable.class); AppUtils.navigate(Navigable.DIRECTION_DOWN, appBar, navigable); verify(appBar).setExpanded(eq(false), anyBoolean()); verify(navigable, never()).onNavigate(anyInt()); when(appBar.getBottom()).thenReturn(0); AppUtils.navigate(Navigable.DIRECTION_DOWN, appBar, navigable); verify(navigable).onNavigate(eq(Navigable.DIRECTION_DOWN)); AppUtils.navigate(Navigable.DIRECTION_RIGHT, appBar, navigable); verify(navigable).onNavigate(eq(Navigable.DIRECTION_RIGHT)); AppUtils.navigate(Navigable.DIRECTION_UP, appBar, navigable); verify(navigable).onNavigate(eq(Navigable.DIRECTION_UP)); AppUtils.navigate(Navigable.DIRECTION_LEFT, appBar, navigable); verify(navigable).onNavigate(eq(Navigable.DIRECTION_LEFT)); } }
private void shortPress(@Direction int direction) { if (mScrollable == null) { return; } switch (direction) { case DIRECTION_UP: if (!mScrollable.scrollToPrevious() && mAppBarEnabled && mAppBarLayout != null) { mAppBarLayout.setExpanded(true, true); } break; case DIRECTION_DOWN: if (mAppBarEnabled && mAppBarLayout != null && mAppBarLayout.getHeight() == mAppBarLayout.getBottom()) { mAppBarLayout.setExpanded(false, true); } else { mScrollable.scrollToNext(); } break; case DIRECTION_NONE: default: break; } }
@Test public void testOnKeyUp() { KeyEvent keyEvent = mock(KeyEvent.class); when(keyEvent.getFlags()).thenReturn(KeyEvent.FLAG_CANCELED_LONG_PRESS); assertFalse(delegate.onKeyUp(KeyEvent.KEYCODE_BACK, keyEvent)); assertFalse(delegate.onKeyUp(KeyEvent.KEYCODE_VOLUME_UP, keyEvent)); assertFalse(delegate.onKeyUp(KeyEvent.KEYCODE_VOLUME_DOWN, keyEvent)); KeyEvent keyEventVolUp = new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_VOLUME_UP), keyEventVolDown = new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_VOLUME_DOWN); assertTrue(delegate.onKeyUp(KeyEvent.KEYCODE_VOLUME_UP, keyEventVolUp)); verify(scrollable).scrollToPrevious(); verify(appBar).setExpanded(eq(true), anyBoolean()); reset(scrollable, appBar); when(scrollable.scrollToPrevious()).thenReturn(true); assertTrue(delegate.onKeyUp(KeyEvent.KEYCODE_VOLUME_UP, keyEventVolUp)); verify(scrollable).scrollToPrevious(); verify(appBar, never()).setExpanded(eq(true), anyBoolean()); reset(scrollable, appBar); when(appBar.getHeight()).thenReturn(10); when(appBar.getBottom()).thenReturn(10); assertTrue(delegate.onKeyUp(KeyEvent.KEYCODE_VOLUME_DOWN, keyEventVolDown)); verify(scrollable, never()).scrollToNext(); verify(appBar).setExpanded(eq(false), anyBoolean()); reset(scrollable, appBar); when(appBar.getHeight()).thenReturn(10); when(appBar.getBottom()).thenReturn(0); assertTrue(delegate.onKeyUp(KeyEvent.KEYCODE_VOLUME_DOWN, keyEventVolDown)); verify(scrollable).scrollToNext(); verify(appBar, never()).setExpanded(eq(false), anyBoolean()); }
@Override public void onNestedPreScroll(CoordinatorLayout coordinatorLayout, AppBarLayout child, View target, int dx, int dy, int[] consumed) { if (!isRecovering) { if (mTargetView != null && ((dy < 0 && child.getBottom() >= mParentHeight) || (dy > 0 && child.getBottom() > mParentHeight)) && !isPining) { scale(child, dy, target);//执行下拉展开动画 return; } } }
@Override public void onNestedPreScroll(CoordinatorLayout coordinatorLayout, AppBarLayout child, View target, int dx, int dy, int[] consumed, int type) { Log.e(TAG, "onNestedPreScroll: dy==" + dy); Log.e(TAG, "onNestedPreScroll: child.getBottom()==" + child.getBottom()); if (!isRecovering) { if (mTargetView != null && ((dy < 0 && child.getBottom() >= mParentHeight) || (dy > 0 && child.getBottom() > mParentHeight))) { scale(child, target, dy); return; } } super.onNestedPreScroll(coordinatorLayout, child, target, dx, dy, consumed, type); }
@Override public void onNestedPreScroll(CoordinatorLayout coordinatorLayout, AppBarLayout child, View target, int dx, int dy, int[] consumed) { if (!isRecovering) { if (mTargetView != null && ((dy < 0 && child.getBottom() >= mParentHeight) || (dy > 0 && child.getBottom() > mParentHeight))) { scale(child, target, dy); return; } } super.onNestedPreScroll(coordinatorLayout, child, target, dx, dy, consumed); }
@Override public void check(View view, NoMatchingViewException noViewFoundException) { AppBarLayout appBarLayout = (AppBarLayout) view; boolean isFullExpanded = appBarLayout.getHeight() - appBarLayout.getBottom() == 0; ViewMatchers.assertThat("is full expanded", isFullExpanded, is(true)); } };
@Override public boolean onInterceptTouchEvent(CoordinatorLayout parent, AppBarLayout child, MotionEvent ev) { judgeDrag(); if (!canDrag) { if (ev.getRawX() < dhv.getLeft() || ev.getRawX() > dhv.getRight() || ev.getRawY() < dhv.getTop() || ev.getRawY() > dhv.getBottom()) { ((Activity) mContext).finish(); } else { mGestureDetector.onTouchEvent(ev); } } else { lastY = ev.getRawY(); } switch (ev.getAction()) { case MotionEvent.ACTION_DOWN: mGestureDetector.onTouchEvent(ev); break; case MotionEvent.ACTION_UP: onActionUp(ev); mGestureDetector.onTouchEvent(ev); break; } return canDrag && super.onInterceptTouchEvent(parent, child, ev); }
@Override public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) { if (appbarMovieTopChild.getBottom() > toolbarDoubanDetail.getBottom() ) { toolbarDoubanDetail.setBackgroundColor(getResources().getColor(R.color.translucent)); } else { toolbarDoubanDetail.setBackgroundResource(R.color.colorTheme); } } });
@Override public void check(View view, NoMatchingViewException noViewFoundException) { CollapsingToolbarLayout collapsingToolbarLayout = null; Toolbar toolbar = null; AppBarLayout appBarLayout = (AppBarLayout) view; for (int i = 0; i < appBarLayout.getChildCount(); i++) { if(appBarLayout.getChildAt(i) instanceof CollapsingToolbarLayout) { collapsingToolbarLayout = (CollapsingToolbarLayout) appBarLayout.getChildAt(i); } } if(collapsingToolbarLayout == null) { throw new IllegalStateException("Current only AppBarLayouts with CollapsingToolbarLayout are supported."); } for (int i = 0; i < collapsingToolbarLayout.getChildCount(); i++) { if(collapsingToolbarLayout.getChildAt(i) instanceof Toolbar) { toolbar = (Toolbar) collapsingToolbarLayout.getChildAt(i); } } if(toolbar == null) { throw new IllegalStateException("Current only CollapsingToolbarLayout with support Toolbar are supported."); } boolean isFullCollapsed = appBarLayout.getBottom() - toolbar.getHeight() == 0; isFullCollapsed |= appBarLayout.getBottom() - toolbar.getHeight() - EspResourceTool.getStatusBarHeight(view.getContext()) == 0; ViewMatchers.assertThat("is full collapsed", isFullCollapsed, is(true)); } };