Skip to content

Commit

Permalink
1.add AdapterDataObserver with recyclerview
Browse files Browse the repository at this point in the history
2.support that recyclerviwe  need not to extends RecyclerWithHeaderAdapter
  • Loading branch information
w446108264 committed Dec 8, 2015
1 parent 4d74169 commit e56c1ae
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ public void updatePlaceHeight(final int placeHoderHeight, final StickHeaderViewP
this.mStickHeaderViewPagerManager = stickHeaderViewPagerManager;
this.mPosition = position;

if (mScrollItemView instanceof RecyclerView && ((RecyclerView) mScrollItemView).getAdapter() != null) {
placeHolderView = ((RecyclerWithHeaderAdapter) (((RecyclerView) mScrollItemView).getAdapter())).getPlaceHolderView();
if (mScrollItemView instanceof RecyclerView && ((RecyclerView) mScrollItemView).getChildCount() > 0) {
placeHolderView = ((RecyclerView) mScrollItemView).getChildAt(0);
}

if (placeHolderView != null && placeHoderHeight != 0) {
Expand Down Expand Up @@ -144,6 +144,22 @@ public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
stickHeaderViewPagerManager.onRecyclerViewScroll(recyclerView, mRecyclerViewScrollY, position, false);
}
});

if(((RecyclerView) mScrollItemView).getAdapter() != null){
((RecyclerView) mScrollItemView).getAdapter().registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
@Override
public void onItemRangeMoved(int fromPosition, int toPosition, int itemCount) {
super.onItemRangeMoved(fromPosition, toPosition, itemCount);
checkAdapterDataObserver();
}

@Override
public void onItemRangeRemoved(int positionStart, int itemCount) {
super.onItemRangeRemoved(positionStart, itemCount);
checkAdapterDataObserver();
}
});
}
} else if (mScrollItemView instanceof NestingWebViewScrollView) {
((NestingWebViewScrollView) mScrollItemView).setOnScrollChangedListener(new NotifyingListenerScrollView.OnScrollChangedListener() {
@Override
Expand All @@ -157,7 +173,34 @@ public void onScrollChanged(ScrollView who, int l, int t, int oldl, int oldt) {
}
}

private void checkAdapterDataObserver(){
if (((RecyclerView) mScrollItemView).getLayoutManager() != null) {
if (((RecyclerView) mScrollItemView).getLayoutManager() instanceof LinearLayoutManager) {
float recyclerViewBottom = mScrollItemView.getBottom();
int countCount = (((RecyclerView) mScrollItemView).getLayoutManager()).getChildCount();
if(countCount > 0){
float lastChildViewBottom = (((RecyclerView) mScrollItemView).getLayoutManager()).getChildAt(countCount - 1).getBottom();
float contentViewHeight = lastChildViewBottom - mScrollHeight;
if(contentViewHeight + mHeaderHeight < recyclerViewBottom){
if(mStickHeaderViewPagerManager != null){
mRecyclerViewScrollY = 0;
(((RecyclerView) mScrollItemView).getLayoutManager()).scrollToPosition(0);
mStickHeaderViewPagerManager.onRecyclerViewScroll((RecyclerView) mScrollItemView, mRecyclerViewScrollY, mPosition, true);
}
}
}
} else if (((RecyclerView) mScrollItemView).getLayoutManager() instanceof GridLayoutManager) {
((GridLayoutManager) ((RecyclerView) mScrollItemView).getLayoutManager()).scrollToPositionWithOffset(0, -mRecyclerViewScrollY);
}
}
}

int mScrollHeight;
int mHeaderHeight;

public void adjustScroll(int scrollHeight, int headerHeight) {
this.mScrollHeight = scrollHeight;
this.mHeaderHeight = headerHeight;
if (mScrollItemView == null) return;

if (mScrollItemView instanceof ListView) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,8 @@ private void updatePlaceHeight() {
if (mStickHeaderHeight != 0 && mStickViewHeight != 0) {
mMinHeaderTranslation = -mStickHeaderHeight + mStickViewHeight;

if (mScrollItemView instanceof RecyclerView) {
if (((RecyclerView) mScrollItemView).getAdapter() != null) {
placeHolderView = ((RecyclerWithHeaderAdapter) (((RecyclerView) mScrollItemView).getAdapter())).getPlaceHolderView();
}
if (mScrollItemView instanceof RecyclerView && ((RecyclerView) mScrollItemView).getChildCount() > 0) {
placeHolderView = ((RecyclerView) mScrollItemView).getChildAt(0);
}

if (placeHolderView != null) {
Expand Down

0 comments on commit e56c1ae

Please sign in to comment.