Advertisement
Guest User

Untitled

a guest
Nov 25th, 2015
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.04 KB | None | 0 0
  1.  
  2. public class QuickReturnFooterBehavior extends CoordinatorLayout.Behavior<View> {
  3.  
  4.  
  5. private static final Interpolator INTERPOLATOR = new FastOutSlowInInterpolator();
  6.  
  7. private int mDySinceDirectionChange;
  8. private boolean mIsShowing;
  9. private boolean mIsHiding;
  10.  
  11. public QuickReturnFooterBehavior(Context context, AttributeSet attrs) {
  12. super(context, attrs);
  13. }
  14.  
  15.  
  16. @Override
  17. public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout, View child, View directTargetChild, View target, int nestedScrollAxes) {
  18. /*
  19. return nestedScrollAxes == ViewCompat.SCROLL_AXIS_VERTICAL
  20. || super.onStartNestedScroll(coordinatorLayout,child, directTargetChild, target, nestedScrollAxes);
  21. */
  22.  
  23. return nestedScrollAxes == ViewCompat.SCROLL_AXIS_VERTICAL;
  24.  
  25. }
  26.  
  27.  
  28. @Override
  29. public void onNestedPreScroll(CoordinatorLayout coordinatorLayout, View child, View target, int dx, int dy, int[] consumed) {
  30.  
  31.  
  32. // 스크롤방향이 바뀌는경우 모든동작을 취소하고 Y값을 다시 처음부터 시작한다
  33. if (dy > 0 && mDySinceDirectionChange < 0
  34. || dy < 0 && mDySinceDirectionChange > 0) {
  35.  
  36. child.animate().cancel();
  37. mDySinceDirectionChange = 0;
  38. }
  39.  
  40. mDySinceDirectionChange += dy;
  41.  
  42. if (mDySinceDirectionChange > child.getHeight()
  43. && child.getVisibility() == View.VISIBLE
  44. && !mIsHiding) {
  45. hideView(child);
  46. } else if (mDySinceDirectionChange < 0
  47. && child.getVisibility() == View.GONE
  48. && !mIsShowing) {
  49. showView(child);
  50. }
  51. }
  52.  
  53.  
  54. /**
  55. * View를 숨긴다
  56. * <p/>
  57. * 아래로 슬라이딩하는 애니메이션.
  58. * 애니메이션 종료후 View를 없앤다.
  59. *
  60. * @param view The quick return view
  61. */
  62. private void hideView(final View view) {
  63. mIsHiding = true;
  64. ViewPropertyAnimator animator = view.animate()
  65. .translationY(view.getHeight())
  66. .setInterpolator(INTERPOLATOR)
  67. .setDuration(200);
  68.  
  69. animator.setListener(new Animator.AnimatorListener() {
  70. @Override
  71. public void onAnimationStart(Animator animator) {
  72. }
  73.  
  74. @Override
  75. public void onAnimationEnd(Animator animator) {
  76. mIsHiding = false;
  77. view.setVisibility(View.GONE);
  78. }
  79.  
  80. @Override
  81. public void onAnimationCancel(Animator animator) {
  82. // 취소되면 다시 보여줌
  83. mIsHiding = false;
  84. if (!mIsShowing) {
  85. showView(view);
  86. }
  87. }
  88.  
  89. @Override
  90. public void onAnimationRepeat(Animator animator) {
  91. }
  92. });
  93.  
  94. animator.start();
  95. }
  96.  
  97. /**
  98. * View를 보여준다.
  99. * <p/>
  100. * 아래서 위로 슬라이딩 애니메이션.
  101. * 애니메이션을 시작하기전 View를 보여준다.
  102. *
  103. * @param view The quick return view
  104. */
  105. private void showView(final View view) {
  106. mIsShowing = true;
  107. ViewPropertyAnimator animator = view.animate()
  108. .translationY(0)
  109. .setInterpolator(INTERPOLATOR)
  110. .setDuration(200);
  111.  
  112. animator.setListener(new Animator.AnimatorListener() {
  113. @Override
  114. public void onAnimationStart(Animator animator) {
  115. view.setVisibility(View.VISIBLE);
  116. }
  117.  
  118. @Override
  119. public void onAnimationEnd(Animator animator) {
  120. mIsShowing = false;
  121. }
  122.  
  123. @Override
  124. public void onAnimationCancel(Animator animator) {
  125. // 취소되면 다시 숨김
  126. mIsShowing = false;
  127. if (!mIsHiding) {
  128. hideView(view);
  129. }
  130. }
  131.  
  132. @Override
  133. public void onAnimationRepeat(Animator animator) {
  134. }
  135. });
  136.  
  137. animator.start();
  138. }
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement