Skip to content

Commit

Permalink
refactor the impl to save and restore scroll info
Browse files Browse the repository at this point in the history
  • Loading branch information
cgspine committed Jun 6, 2019
1 parent 5bfd8f5 commit 8010439
Show file tree
Hide file tree
Showing 10 changed files with 125 additions and 147 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package com.qmuiteam.qmui.nestedScroll;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.View;
Expand All @@ -26,10 +28,9 @@ public interface IQMUIContinuousNestedScrollCommon {
int SCROLL_STATE_DRAGGING = RecyclerView.SCROLL_STATE_DRAGGING;
int SCROLL_STATE_SETTLING = RecyclerView.SCROLL_STATE_SETTLING;

@Nullable
Object saveScrollInfo();
void saveScrollInfo(@NonNull Bundle bundle);

void restoreScrollInfo(@Nullable Object scrollInfo);
void restoreScrollInfo(@NonNull Bundle bundle);

void injectScrollNotifier(OnScrollNotifier notifier);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import android.content.Context;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.view.NestedScrollingChild2;
import android.support.v4.view.NestedScrollingChildHelper;
Expand All @@ -44,6 +45,7 @@

public abstract class QMUIContinuousNestedBottomDelegateLayout extends QMUIFrameLayout implements
NestedScrollingChild2, NestedScrollingParent2, IQMUIContinuousNestedBottomView {
public static final String KEY_SCROLL_INFO_OFFSET = "@qmui_scroll_info_bottom_dl_offset";

private final NestedScrollingParentHelper mParentHelper;
private final NestedScrollingChildHelper mChildHelper;
Expand Down Expand Up @@ -281,29 +283,21 @@ public void onScrollStateChange(View view, int newScrollState) {
}

@Override
public void restoreScrollInfo(Object scrollInfo) {
if (scrollInfo instanceof ScrollInfo) {
ScrollInfo si = (ScrollInfo) scrollInfo;
int offset = QMUILangHelper.constrain(si.topBottomOffset, getMiniOffset(), 0);
mHeaderViewOffsetHelper.setTopAndBottomOffset(offset);
mContentViewOffsetHelper.setTopAndBottomOffset(offset);
((IQMUIContinuousNestedBottomView) mContentView).restoreScrollInfo(si.delegateScrollInfo);
public void saveScrollInfo(@NonNull Bundle bundle) {
bundle.putInt(KEY_SCROLL_INFO_OFFSET, mHeaderViewOffsetHelper.getTopAndBottomOffset());
if (mContentView != null) {
((IQMUIContinuousNestedBottomView) mContentView).saveScrollInfo(bundle);
}
}

@Override
public Object saveScrollInfo() {
return new ScrollInfo(mHeaderViewOffsetHelper.getTopAndBottomOffset(),
((IQMUIContinuousNestedBottomView) mContentView).saveScrollInfo());
}

public static class ScrollInfo {
public int topBottomOffset;
public Object delegateScrollInfo;

public ScrollInfo(int topBottomOffset, Object delegateScrollInfo) {
this.topBottomOffset = topBottomOffset;
this.delegateScrollInfo = delegateScrollInfo;
public void restoreScrollInfo(@NonNull Bundle bundle) {
int offset = bundle.getInt(KEY_SCROLL_INFO_OFFSET, 0);
offset = QMUILangHelper.constrain(offset, getMiniOffset(), 0);
mHeaderViewOffsetHelper.setTopAndBottomOffset(offset);
mContentViewOffsetHelper.setTopAndBottomOffset(offset);
if (mContentView != null) {
((IQMUIContinuousNestedBottomView) mContentView).restoreScrollInfo(bundle);
}
}

Expand Down Expand Up @@ -420,7 +414,7 @@ public void onNestedPreScroll(@NonNull View target, int dx, int dy, @NonNull int
int type) {
dispatchNestedPreScroll(dx, dy, consumed, null, type);
int unconsumed = dy - consumed[1];
if (unconsumed != 0 && unconsumed > 0) {
if (unconsumed > 0) {
consumed[1] += unconsumed - offsetBy(unconsumed);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.qmuiteam.qmui.nestedScroll;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewCompat;
Expand All @@ -29,6 +30,9 @@
public class QMUIContinuousNestedBottomRecyclerView extends RecyclerView
implements IQMUIContinuousNestedBottomView {

public static final String KEY_SCROLL_INFO_POSITION = "@qmui_scroll_info_bottom_rv_pos";
public static final String KEY_SCROLL_INFO_OFFSET = "@qmui_scroll_info_bottom_rv_offset";

private IQMUIContinuousNestedBottomView.OnScrollNotifier mOnScrollNotifier;
private final int[] mScrollConsumed = new int[2];

Expand Down Expand Up @@ -145,40 +149,28 @@ public void smoothScrollYBy(int dy, int duration) {
}

@Override
public Object saveScrollInfo() {
public void saveScrollInfo(@NonNull Bundle bundle) {
LayoutManager layoutManager = getLayoutManager();
if (layoutManager instanceof LinearLayoutManager) {
LinearLayoutManager lm = (LinearLayoutManager) layoutManager;
if (lm.getOrientation() == LinearLayoutManager.HORIZONTAL) {
return null;
}
int pos = lm.findFirstVisibleItemPosition();
View firstView = lm.findViewByPosition(pos);
int offset = firstView == null ? 0 : firstView.getTop();
return new ScrollInfo(pos, offset);
bundle.putInt(KEY_SCROLL_INFO_POSITION, pos);
bundle.putInt(KEY_SCROLL_INFO_OFFSET, offset);
}
return null;
}

@Override
public void restoreScrollInfo(Object scrollInfo) {
if (!(scrollInfo instanceof ScrollInfo)) {
return;
}
ScrollInfo sc = (ScrollInfo) scrollInfo;
public void restoreScrollInfo(@NonNull Bundle bundle) {
LayoutManager layoutManager = getLayoutManager();
if (layoutManager instanceof LinearLayoutManager) {
((LinearLayoutManager) layoutManager).scrollToPositionWithOffset(sc.scrollPosition, sc.scrollOffset);
}
}

public static class ScrollInfo {
public int scrollPosition;
public int scrollOffset;

public ScrollInfo(int scrollPosition, int scrollOffset) {
this.scrollPosition = scrollPosition;
this.scrollOffset = scrollOffset;
int pos = bundle.getInt(KEY_SCROLL_INFO_POSITION, 0);
int offset = bundle.getInt(KEY_SCROLL_INFO_OFFSET, 0);
((LinearLayoutManager) layoutManager).scrollToPositionWithOffset(pos, offset);
if(mOnScrollNotifier != null){
mOnScrollNotifier.notify(getCurrentScroll(), getScrollOffsetRange());
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.qmuiteam.qmui.nestedScroll;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.CoordinatorLayout;
Expand All @@ -33,6 +34,7 @@

public class QMUIContinuousNestedScrollLayout extends CoordinatorLayout implements
QMUIContinuousNestedTopAreaBehavior.Callback, QMUIDraggableScrollBar.Callback {
public static final String KEY_SCROLL_INFO_OFFSET = "@qmui_nested_scroll_layout_offset";

private IQMUIContinuousNestedTopView mTopView;
private IQMUIContinuousNestedBottomView mBottomView;
Expand Down Expand Up @@ -62,8 +64,8 @@ public QMUIContinuousNestedScrollLayout(@NonNull Context context, @Nullable Attr
super(context, attrs, defStyleAttr);
}

private void ensureScrollBar(){
if(mDraggableScrollBar == null){
private void ensureScrollBar() {
if (mDraggableScrollBar == null) {
mDraggableScrollBar = createScrollBar(getContext());
mDraggableScrollBar.setCallback(this);
CoordinatorLayout.LayoutParams lp = new CoordinatorLayout.LayoutParams(
Expand All @@ -77,7 +79,7 @@ public void setDraggableScrollBarEnabled(boolean draggableScrollBarEnabled) {
mIsDraggableScrollBarEnabled = draggableScrollBarEnabled;
}

protected QMUIDraggableScrollBar createScrollBar(Context context){
protected QMUIDraggableScrollBar createScrollBar(Context context) {
return new QMUIDraggableScrollBar(context);
}

Expand Down Expand Up @@ -253,7 +255,7 @@ public void checkLayout() {
int offsetCurrent = -mTopAreaBehavior.getTopAndBottomOffset();
int offsetRange = getOffsetRange();

if(offsetRange <= 0){
if (offsetRange <= 0) {
return;
}

Expand Down Expand Up @@ -300,7 +302,7 @@ public void scrollBottomViewToTop() {
private void dispatchScroll(int topCurrent, int topRange,
int offsetCurrent, int offsetRange,
int bottomCurrent, int bottomRange) {
if(mIsDraggableScrollBarEnabled){
if (mIsDraggableScrollBarEnabled) {
ensureScrollBar();
mDraggableScrollBar.setPercent(getCurrentScrollPercent());
mDraggableScrollBar.awakenScrollBar();
Expand Down Expand Up @@ -439,25 +441,41 @@ public boolean dispatchTouchEvent(MotionEvent ev) {
return super.dispatchTouchEvent(ev);
}

public ScrollInfo saveScrollInfo() {
Object topInfo = mTopView != null ? mTopView.saveScrollInfo() : null;
Object bottomInfo = mBottomView != null ? mBottomView.saveScrollInfo() : null;
return new ScrollInfo(topInfo, bottomInfo, getOffsetCurrent());
/**
* save current scroll info to bundle
*
* @param bundle
*/
public void saveScrollInfo(@NonNull Bundle bundle) {
if (mTopView != null) {
mTopView.saveScrollInfo(bundle);
}
if (mBottomView != null) {
mBottomView.saveScrollInfo(bundle);
}
bundle.putInt(KEY_SCROLL_INFO_OFFSET, getOffsetCurrent());
}

public void restoreScrollInfo(@Nullable ScrollInfo scrollInfo) {
if (scrollInfo == null) {

/**
* restore current scroll info from bundle
*
* @param bundle
*/
public void restoreScrollInfo(@Nullable Bundle bundle) {
if (bundle == null) {
return;
}
if (mTopAreaBehavior != null) {
mTopAreaBehavior.setTopAndBottomOffset(QMUILangHelper.constrain(-scrollInfo.topBottomOffset, -getOffsetRange(), 0));
int offset = bundle.getInt(KEY_SCROLL_INFO_OFFSET, 0);
mTopAreaBehavior.setTopAndBottomOffset(QMUILangHelper.constrain(-offset, -getOffsetRange(), 0));
}
if (mTopView != null) {
mTopView.restoreScrollInfo(scrollInfo.topInfo);
mTopView.restoreScrollInfo(bundle);
}

if (mBottomView != null) {
mBottomView.restoreScrollInfo(scrollInfo.bottomInfo);
mBottomView.restoreScrollInfo(bundle);
}
}

Expand All @@ -469,16 +487,4 @@ void onScroll(int topCurrent, int topRange,

void onScrollStateChange(int newScrollState, boolean fromTopBehavior);
}

public static class ScrollInfo {
public Object topInfo;
public Object bottomInfo;
public int topBottomOffset;

public ScrollInfo(Object topInfo, Object bottomInfo, int topBottomOffset) {
this.topInfo = topInfo;
this.bottomInfo = bottomInfo;
this.topBottomOffset = topBottomOffset;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.qmuiteam.qmui.nestedScroll;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.view.NestedScrollingChild2;
Expand All @@ -32,11 +33,11 @@
import com.qmuiteam.qmui.util.QMUILangHelper;
import com.qmuiteam.qmui.util.QMUIViewOffsetHelper;

import java.io.Serializable;

public class QMUIContinuousNestedTopDelegateLayout extends FrameLayout implements
NestedScrollingChild2, NestedScrollingParent2, IQMUIContinuousNestedTopView {

public static final String KEY_SCROLL_INFO_OFFSET = "@qmui_scroll_info_top_dl_offset";

private OnScrollNotifier mScrollNotifier;
private View mHeaderView;
private IQMUIContinuousNestedTopView mDelegateView;
Expand Down Expand Up @@ -387,28 +388,19 @@ public void onScrollStateChange(View view, int newScrollState) {
}

@Override
public Object saveScrollInfo() {
return new ScrollInfo(-mOffsetCurrent, mDelegateView == null ? null : mDelegateView.saveScrollInfo());
}

@Override
public void restoreScrollInfo(Object scrollInfo) {
if (scrollInfo instanceof ScrollInfo) {
ScrollInfo si = (ScrollInfo) scrollInfo;
offsetTo(QMUILangHelper.constrain(-si.topBottomOffset, 0, getContainerOffsetRange()));
if (mDelegateView != null) {
mDelegateView.restoreScrollInfo(((ScrollInfo) scrollInfo).delegateScrollInfo);
}
public void saveScrollInfo(@NonNull Bundle bundle) {
bundle.putInt(KEY_SCROLL_INFO_OFFSET, -mOffsetCurrent);
if (mDelegateView != null) {
mDelegateView.saveScrollInfo(bundle);
}
}

public static class ScrollInfo implements Serializable {
public int topBottomOffset;
public Object delegateScrollInfo;

public ScrollInfo(int topBottomOffset, Object delegateScrollInfo) {
this.topBottomOffset = topBottomOffset;
this.delegateScrollInfo = delegateScrollInfo;
@Override
public void restoreScrollInfo(@NonNull Bundle bundle) {
int offset = bundle.getInt(KEY_SCROLL_INFO_OFFSET, 0);
offsetTo(QMUILangHelper.constrain(-offset, 0, getContainerOffsetRange()));
if (mDelegateView != null) {
mDelegateView.restoreScrollInfo(bundle);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package com.qmuiteam.qmui.nestedScroll;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.util.AttributeSet;

import com.qmuiteam.qmui.layout.QMUILinearLayout;
Expand Down Expand Up @@ -58,12 +60,12 @@ public void injectScrollNotifier(OnScrollNotifier notifier) {
}

@Override
public void restoreScrollInfo(Object scrollInfo) {
public void restoreScrollInfo(@NonNull Bundle bundle) {

}

@Override
public Object saveScrollInfo() {
return null;
public void saveScrollInfo(@NonNull Bundle bundle) {

}
}
Loading

0 comments on commit 8010439

Please sign in to comment.