@@ -50,7 +50,7 @@ public enum LayoutOrientation {
50
50
private AdapterDataObserver dataObserver ;
51
51
private StickyViewScrollListener scrollListener ;
52
52
53
- private int stickyHeaderStart = 0 ;
53
+ private int stickyHeaderLeft , stickyHeaderTop ;
54
54
private long currentStickyId = Long .MIN_VALUE ;
55
55
private LayoutOrientation orientation = LayoutOrientation .VERTICAL ;
56
56
@@ -84,9 +84,7 @@ public StickyHeaderDecoration(RecyclerView parent) {
84
84
@ Override
85
85
public void onDrawOver (Canvas c , RecyclerView parent , RecyclerView .State state ) {
86
86
if (stickyHeader != null ) {
87
- int x = orientation == LayoutOrientation .HORIZONTAL ? stickyHeaderStart : 0 ;
88
- int y = orientation == LayoutOrientation .HORIZONTAL ? 0 : stickyHeaderStart ;
89
- c .drawBitmap (stickyHeader , x , y , null );
87
+ c .drawBitmap (stickyHeader , stickyHeaderLeft , stickyHeaderTop , null );
90
88
}
91
89
}
92
90
@@ -114,7 +112,6 @@ public void clearStickyHeader() {
114
112
}
115
113
116
114
stickyHeader = null ;
117
- stickyHeaderStart = 0 ;
118
115
currentStickyId = RecyclerView .NO_ID ;
119
116
}
120
117
@@ -144,6 +141,10 @@ public LayoutOrientation getOrientation() {
144
141
* @return The bitmap representing the drag view
145
142
*/
146
143
private Bitmap createStickyViewBitmap (View view ) {
144
+ //Makes sure the location opposite the scroll orientation is persisted
145
+ stickyHeaderLeft = orientation == LayoutOrientation .HORIZONTAL ? 0 : view .getLeft ();
146
+ stickyHeaderTop = orientation == LayoutOrientation .VERTICAL ? 0 : view .getTop ();
147
+
147
148
Rect stickyViewBounds = new Rect (0 , 0 , view .getRight () - view .getLeft (), view .getBottom () - view .getTop ());
148
149
149
150
Bitmap bitmap = Bitmap .createBitmap (stickyViewBounds .width (), stickyViewBounds .height (), Bitmap .Config .ARGB_8888 );
@@ -235,8 +236,19 @@ private void updateHeader(long headerId, int headerPosition) {
235
236
return ;
236
237
}
237
238
238
- stickyHeaderStart = 0 ;
239
239
currentStickyId = headerId ;
240
+
241
+ //Creates the actual sticky header (or view)
242
+ int stickyViewId = getCustomStickyViewId ();
243
+ if (stickyViewId != 0 ) {
244
+ View stickyView = holder .itemView .findViewById (stickyViewId );
245
+ if (stickyView != null ) {
246
+ stickyHeader = createStickyViewBitmap (stickyView );
247
+ return ;
248
+ }
249
+ }
250
+
251
+ //If the user hasn't specified a sticky view id or it was null, use the entire view holder
240
252
stickyHeader = createStickyViewBitmap (holder .itemView );
241
253
}
242
254
@@ -319,6 +331,14 @@ private int getHeaderViewType(int headerPosition) {
319
331
return rawType | HeaderApi .HEADER_VIEW_TYPE_MASK ;
320
332
}
321
333
334
+ private int getCustomStickyViewId () {
335
+ if (adapter instanceof RecyclerHeaderAdapter ) {
336
+ return ((RecyclerHeaderAdapter )adapter ).getCustomStickyHeaderViewId ();
337
+ }
338
+
339
+ return ((RecyclerHeaderCursorAdapter ) adapter ).getCustomStickyHeaderViewId ();
340
+ }
341
+
322
342
/**
323
343
* Retrieves the ViewHolder associated with the header at <code>headerPosition</code>.
324
344
* If the ViewHolder returned from <code>parent</code> is null then a temporary ViewHolder
0 commit comments