@@ -54,6 +54,8 @@ public class StickyHeaderLayout extends FrameLayout {
5454 //是否已经注册了adapter刷新监听
5555 private boolean isRegisterDataObserver = false ;
5656
57+ private OnStickyChangedListener mListener ;
58+
5759 public StickyHeaderLayout (@ NonNull Context context ) {
5860 super (context );
5961 mContext = context ;
@@ -123,6 +125,9 @@ private void updateStickyView(boolean imperative) {
123125 RecyclerView .Adapter adapter = mRecyclerView .getAdapter ();
124126 //只有RecyclerView的adapter是GroupedRecyclerViewAdapter的时候,才会添加吸顶布局。
125127 if (adapter instanceof GroupedRecyclerViewAdapter ) {
128+
129+ int oldGroupIndex = mCurrentStickyGroup ;
130+
126131 GroupedRecyclerViewAdapter gAdapter = (GroupedRecyclerViewAdapter ) adapter ;
127132 registerAdapterDataObserver (gAdapter );
128133 //获取列表显示的第一个项。
@@ -173,18 +178,23 @@ private void updateStickyView(boolean imperative) {
173178 }
174179 }
175180
176- if (mRecyclerView .computeVerticalScrollOffset () == 0 ){
181+ if (mRecyclerView .computeVerticalScrollOffset () == 0 ) {
177182 // 滑动到顶部
178183 recycle ();
179184 }
180185
181- //这是是处理第一次打开时 ,吸顶布局已经添加到StickyLayout,但StickyLayout的高依然为0的情况。
186+ //这里是处理第一次打开时 ,吸顶布局已经添加到StickyLayout,但StickyLayout的高依然为0的情况。
182187 if (mStickyLayout .getChildCount () > 0 && mStickyLayout .getHeight () == 0 ) {
183188 mStickyLayout .requestLayout ();
184189 }
185190
186191 //设置mStickyLayout的Y偏移量。
187192 mStickyLayout .setTranslationY (calculateOffset (gAdapter , firstVisibleItem , groupPosition + 1 ));
193+
194+ if (mListener != null && oldGroupIndex != mCurrentStickyGroup ) {
195+ // 吸顶的项改变了
196+ mListener .onStickyChanged (oldGroupIndex , mCurrentStickyGroup );
197+ }
188198 }
189199 }
190200
@@ -358,13 +368,7 @@ public void setSticky(boolean sticky) {
358368 @ Override
359369 protected int computeVerticalScrollOffset () {
360370 if (mRecyclerView != null ) {
361- try {
362- Method method = View .class .getDeclaredMethod ("computeVerticalScrollOffset" );
363- method .setAccessible (true );
364- return (int ) method .invoke (mRecyclerView );
365- } catch (Exception e ) {
366- e .printStackTrace ();
367- }
371+ mRecyclerView .computeVerticalScrollOffset ();
368372 }
369373 return super .computeVerticalScrollOffset ();
370374 }
@@ -373,27 +377,15 @@ protected int computeVerticalScrollOffset() {
373377 @ Override
374378 protected int computeVerticalScrollRange () {
375379 if (mRecyclerView != null ) {
376- try {
377- Method method = View .class .getDeclaredMethod ("computeVerticalScrollRange" );
378- method .setAccessible (true );
379- return (int ) method .invoke (mRecyclerView );
380- } catch (Exception e ) {
381- e .printStackTrace ();
382- }
380+ mRecyclerView .computeVerticalScrollRange ();
383381 }
384382 return super .computeVerticalScrollRange ();
385383 }
386384
387385 @ Override
388386 protected int computeVerticalScrollExtent () {
389387 if (mRecyclerView != null ) {
390- try {
391- Method method = View .class .getDeclaredMethod ("computeVerticalScrollExtent" );
392- method .setAccessible (true );
393- return (int ) method .invoke (mRecyclerView );
394- } catch (Exception e ) {
395- e .printStackTrace ();
396- }
388+ mRecyclerView .computeVerticalScrollExtent ();
397389 }
398390 return super .computeVerticalScrollExtent ();
399391 }
@@ -415,4 +407,21 @@ public void scrollTo(int x, int y) {
415407 super .scrollTo (x , y );
416408 }
417409 }
410+
411+ /**
412+ * 监听吸顶项改变
413+ *
414+ * @param l
415+ */
416+ public void setOnStickyChangedListener (OnStickyChangedListener l ) {
417+ mListener = l ;
418+ }
419+
420+ public interface OnStickyChangedListener {
421+ /**
422+ * @param oldGroupIndex 旧的吸顶组下标,-1表示没有吸顶
423+ * @param newGroupIndex 新的吸顶组下标,-1表示没有吸顶
424+ */
425+ void onStickyChanged (int oldGroupIndex , int newGroupIndex );
426+ }
418427}
0 commit comments