8
8
import android .support .design .widget .FloatingActionButton ;
9
9
import android .support .v4 .view .ViewCompat ;
10
10
import android .support .v4 .view .animation .FastOutSlowInInterpolator ;
11
+ import android .support .v7 .widget .LinearLayoutManager ;
11
12
import android .util .AttributeSet ;
12
13
import android .util .TypedValue ;
13
14
import android .view .View ;
14
15
import android .view .ViewPropertyAnimator ;
15
16
import android .view .animation .Interpolator ;
16
17
17
18
import com .zulip .android .R ;
19
+ import com .zulip .android .activities .RecyclerMessageAdapter ;
18
20
19
21
/**
20
22
* This hides the {@link AppBarLayout} and {@link android.support.design.widget.FloatingActionButton} when the
@@ -27,6 +29,8 @@ public class RemoveViewsOnScroll extends CoordinatorLayout.Behavior<View> {
27
29
private boolean mIsShowing ;
28
30
private boolean isViewHidden ;
29
31
private View chatBox ;
32
+ private LinearLayoutManager linearLayoutManager ;
33
+ private RecyclerMessageAdapter adapter ;
30
34
31
35
public RemoveViewsOnScroll (Context context , AttributeSet attrs ) {
32
36
super (context , attrs );
@@ -35,33 +39,42 @@ public RemoveViewsOnScroll(Context context, AttributeSet attrs) {
35
39
toolbarHeight = TypedValue .complexToDimensionPixelSize (tv .data , context .getResources ().getDisplayMetrics ());
36
40
}
37
41
42
+ public RemoveViewsOnScroll (LinearLayoutManager linearLayoutManager , RecyclerMessageAdapter adapter ) {
43
+ this .linearLayoutManager = linearLayoutManager ;
44
+ this .adapter = adapter ;
45
+ }
46
+
38
47
@ Override
39
48
public boolean onStartNestedScroll (CoordinatorLayout coordinatorLayout , View child , View directTargetChild , View target , int nestedScrollAxes ) {
40
49
return (nestedScrollAxes & ViewCompat .SCROLL_AXIS_VERTICAL ) != 0 ;
41
50
}
42
51
43
52
@ SuppressLint ("NewApi" )
44
53
@ Override
45
- public void onNestedPreScroll (CoordinatorLayout coordinatorLayout , View child , View target , int dx , int dy , int [] consumed ) {
46
- if (dy > 0 && changeInYDir < 0 || dy < 0 && changeInYDir > 0 ) {
47
- child .animate ().cancel ();
48
- changeInYDir = 0 ;
49
- }
54
+ public void onNestedPreScroll (CoordinatorLayout coordinatorLayout , View child , View target , int dx , int dy , int [] consumed ) throws NullPointerException {
55
+ //count index starts from 1 where as position starts from 0, thus difference 1
56
+ //we have 2 loading layouts one at top and another at bottom of the messages which should be ignored
57
+ //resulting in a overall difference of 3
58
+ if (linearLayoutManager .findLastCompletelyVisibleItemPosition () < adapter .getItemCount () - 3 ) {
59
+ if (dy > 0 && changeInYDir < 0 || dy < 0 && changeInYDir > 0 ) {
60
+ child .animate ().cancel ();
61
+ changeInYDir = 0 ;
62
+ }
50
63
51
- changeInYDir += dy ;
52
- if (changeInYDir > toolbarHeight && child .getVisibility () == View .VISIBLE && !isViewHidden )
53
- hideView (child );
54
- else if (changeInYDir < 0 && child .getVisibility () == View .GONE && !mIsShowing ) {
55
- if (child instanceof FloatingActionButton ) {
56
- if (chatBox == null )
57
- chatBox = coordinatorLayout .findViewById (R .id .messageBoxContainer );
58
- if (chatBox .getVisibility () == View .VISIBLE ) {
59
- return ;
64
+ changeInYDir += dy ;
65
+ if (changeInYDir > toolbarHeight && child .getVisibility () == View .VISIBLE && !isViewHidden )
66
+ hideView (child );
67
+ else if (changeInYDir < 0 && child .getVisibility () == View .GONE && !mIsShowing ) {
68
+ if (child instanceof FloatingActionButton ) {
69
+ if (chatBox == null )
70
+ chatBox = coordinatorLayout .findViewById (R .id .messageBoxContainer );
71
+ if (chatBox .getVisibility () == View .VISIBLE ) {
72
+ return ;
73
+ }
60
74
}
75
+ showView (child );
61
76
}
62
- showView (child );
63
77
}
64
-
65
78
}
66
79
67
80
@ SuppressLint ("NewApi" )
@@ -98,7 +111,7 @@ public void onAnimationRepeat(Animator animator) {
98
111
}
99
112
100
113
@ SuppressLint ("NewApi" )
101
- private void showView (final View view ) {
114
+ public void showView (final View view ) {
102
115
mIsShowing = true ;
103
116
ViewPropertyAnimator animator = view .animate ()
104
117
.translationY (0 )
0 commit comments