diff --git a/qmui/src/main/java/com/qmuiteam/qmui/nestedScroll/IQMUIContinuousNestedScrollCommon.java b/qmui/src/main/java/com/qmuiteam/qmui/nestedScroll/IQMUIContinuousNestedScrollCommon.java index f5e77221b..446bba8e0 100644 --- a/qmui/src/main/java/com/qmuiteam/qmui/nestedScroll/IQMUIContinuousNestedScrollCommon.java +++ b/qmui/src/main/java/com/qmuiteam/qmui/nestedScroll/IQMUIContinuousNestedScrollCommon.java @@ -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; @@ -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); diff --git a/qmui/src/main/java/com/qmuiteam/qmui/nestedScroll/QMUIContinuousNestedBottomDelegateLayout.java b/qmui/src/main/java/com/qmuiteam/qmui/nestedScroll/QMUIContinuousNestedBottomDelegateLayout.java index 3d7717973..6d5d96b84 100644 --- a/qmui/src/main/java/com/qmuiteam/qmui/nestedScroll/QMUIContinuousNestedBottomDelegateLayout.java +++ b/qmui/src/main/java/com/qmuiteam/qmui/nestedScroll/QMUIContinuousNestedBottomDelegateLayout.java @@ -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; @@ -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; @@ -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); } } @@ -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); } } diff --git a/qmui/src/main/java/com/qmuiteam/qmui/nestedScroll/QMUIContinuousNestedBottomRecyclerView.java b/qmui/src/main/java/com/qmuiteam/qmui/nestedScroll/QMUIContinuousNestedBottomRecyclerView.java index 8bce19102..0b252f1f1 100644 --- a/qmui/src/main/java/com/qmuiteam/qmui/nestedScroll/QMUIContinuousNestedBottomRecyclerView.java +++ b/qmui/src/main/java/com/qmuiteam/qmui/nestedScroll/QMUIContinuousNestedBottomRecyclerView.java @@ -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; @@ -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]; @@ -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()); + } } } } diff --git a/qmui/src/main/java/com/qmuiteam/qmui/nestedScroll/QMUIContinuousNestedScrollLayout.java b/qmui/src/main/java/com/qmuiteam/qmui/nestedScroll/QMUIContinuousNestedScrollLayout.java index 567fd8bdd..40726b121 100644 --- a/qmui/src/main/java/com/qmuiteam/qmui/nestedScroll/QMUIContinuousNestedScrollLayout.java +++ b/qmui/src/main/java/com/qmuiteam/qmui/nestedScroll/QMUIContinuousNestedScrollLayout.java @@ -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; @@ -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; @@ -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( @@ -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); } @@ -253,7 +255,7 @@ public void checkLayout() { int offsetCurrent = -mTopAreaBehavior.getTopAndBottomOffset(); int offsetRange = getOffsetRange(); - if(offsetRange <= 0){ + if (offsetRange <= 0) { return; } @@ -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(); @@ -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); } } @@ -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; - } - } } diff --git a/qmui/src/main/java/com/qmuiteam/qmui/nestedScroll/QMUIContinuousNestedTopDelegateLayout.java b/qmui/src/main/java/com/qmuiteam/qmui/nestedScroll/QMUIContinuousNestedTopDelegateLayout.java index 40b0783fd..c4d64cb2c 100644 --- a/qmui/src/main/java/com/qmuiteam/qmui/nestedScroll/QMUIContinuousNestedTopDelegateLayout.java +++ b/qmui/src/main/java/com/qmuiteam/qmui/nestedScroll/QMUIContinuousNestedTopDelegateLayout.java @@ -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; @@ -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; @@ -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); } } diff --git a/qmui/src/main/java/com/qmuiteam/qmui/nestedScroll/QMUIContinuousNestedTopLinearLayout.java b/qmui/src/main/java/com/qmuiteam/qmui/nestedScroll/QMUIContinuousNestedTopLinearLayout.java index 3a72ca829..8893a7249 100644 --- a/qmui/src/main/java/com/qmuiteam/qmui/nestedScroll/QMUIContinuousNestedTopLinearLayout.java +++ b/qmui/src/main/java/com/qmuiteam/qmui/nestedScroll/QMUIContinuousNestedTopLinearLayout.java @@ -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; @@ -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) { + } } diff --git a/qmui/src/main/java/com/qmuiteam/qmui/nestedScroll/QMUIContinuousNestedTopRecyclerView.java b/qmui/src/main/java/com/qmuiteam/qmui/nestedScroll/QMUIContinuousNestedTopRecyclerView.java index 444fc0734..9b1f247e7 100644 --- a/qmui/src/main/java/com/qmuiteam/qmui/nestedScroll/QMUIContinuousNestedTopRecyclerView.java +++ b/qmui/src/main/java/com/qmuiteam/qmui/nestedScroll/QMUIContinuousNestedTopRecyclerView.java @@ -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; @@ -26,6 +27,8 @@ import android.view.View; public class QMUIContinuousNestedTopRecyclerView extends RecyclerView implements IQMUIContinuousNestedTopView { + public static final String KEY_SCROLL_INFO_POSITION = "@qmui_scroll_info_top_rv_pos"; + public static final String KEY_SCROLL_INFO_OFFSET = "@qmui_scroll_info_top_rv_offset"; private OnScrollNotifier mScrollNotifier; private final int[] mScrollConsumed = new int[2]; @@ -105,43 +108,28 @@ public void onScrolled(int dx, int dy) { } @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); - } - if(mScrollNotifier != null){ - mScrollNotifier.notify(getCurrentScroll(), getScrollOffsetRange()); - } - } - - 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(mScrollNotifier != null){ + mScrollNotifier.notify(getCurrentScroll(), getScrollOffsetRange()); + } } } } diff --git a/qmui/src/main/java/com/qmuiteam/qmui/nestedScroll/QMUIContinuousNestedTopWebView.java b/qmui/src/main/java/com/qmuiteam/qmui/nestedScroll/QMUIContinuousNestedTopWebView.java index 84d85ed6a..d7fd2662e 100644 --- a/qmui/src/main/java/com/qmuiteam/qmui/nestedScroll/QMUIContinuousNestedTopWebView.java +++ b/qmui/src/main/java/com/qmuiteam/qmui/nestedScroll/QMUIContinuousNestedTopWebView.java @@ -18,6 +18,8 @@ import android.content.Context; import android.os.Build; +import android.os.Bundle; +import android.support.annotation.NonNull; import android.util.AttributeSet; import com.qmuiteam.qmui.util.QMUIDisplayHelper; @@ -25,6 +27,8 @@ public class QMUIContinuousNestedTopWebView extends QMUIWebView implements IQMUIContinuousNestedTopView { + public static final String KEY_SCROLL_INFO = "@qmui_scroll_info_top_webview"; + private OnScrollNotifier mScrollNotifier; public QMUIContinuousNestedTopWebView(Context context) { @@ -89,17 +93,15 @@ protected void onScrollChanged(int l, int t, int oldl, int oldt) { } @Override - public Object saveScrollInfo() { - return getScrollY(); + public void saveScrollInfo(@NonNull Bundle bundle) { + bundle.putInt(KEY_SCROLL_INFO, getScrollY()); } @Override - public void restoreScrollInfo(Object scrollInfo) { - if (scrollInfo instanceof Integer) { - // scrollTo does not always work, even if after onPageLoaded - int value = QMUIDisplayHelper.px2dp(getContext(), (Integer) scrollInfo); - exec("javascript:scrollTo(0, " + value + ")"); - } + public void restoreScrollInfo(@NonNull Bundle bundle) { + int scrollY = QMUIDisplayHelper.px2dp(getContext(), + bundle.getInt(KEY_SCROLL_INFO, 0)); + exec("javascript:scrollTo(0, " + scrollY + ")"); } private void exec(final String jsCode) { diff --git a/qmuidemo/src/main/java/com/qmuiteam/qmuidemo/fragment/lab/QDContinuousBottomView.java b/qmuidemo/src/main/java/com/qmuiteam/qmuidemo/fragment/lab/QDContinuousBottomView.java index 6d5392544..4e6f4a8d9 100644 --- a/qmuidemo/src/main/java/com/qmuiteam/qmuidemo/fragment/lab/QDContinuousBottomView.java +++ b/qmuidemo/src/main/java/com/qmuiteam/qmuidemo/fragment/lab/QDContinuousBottomView.java @@ -18,6 +18,7 @@ import android.content.Context; import android.graphics.Color; +import android.os.Bundle; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.v7.widget.LinearLayoutManager; @@ -168,6 +169,7 @@ private void onDataLoaded(BaseRecyclerAdapter adapter) { } class MyViewPager extends QMUIViewPager implements IQMUIContinuousNestedBottomView { + static final String KEY_CURRENT_POSITION = "demo_bottom_current_position"; public MyViewPager(Context context) { super(context); @@ -228,31 +230,21 @@ public void injectScrollNotifier(OnScrollNotifier notifier) { } @Override - public Object saveScrollInfo() { - if (mCurrentItemView != null) { - return new ScrollInfo(mCurrentPosition, mCurrentItemView.saveScrollInfo()); + public void saveScrollInfo(@NonNull Bundle bundle) { + bundle.putInt(KEY_CURRENT_POSITION, mCurrentPosition); + if(mCurrentItemView != null){ + mCurrentItemView.saveScrollInfo(bundle); } - return null; } @Override - public void restoreScrollInfo(Object scrollInfo) { - if (mCurrentItemView != null && (scrollInfo instanceof ScrollInfo)) { - ScrollInfo si = (ScrollInfo) scrollInfo; - if (si.currentPosition == mCurrentPosition) { - mCurrentItemView.restoreScrollInfo(si.currentScrollInfo); + public void restoreScrollInfo(@NonNull Bundle bundle) { + if(mCurrentItemView != null){ + int currentPos = bundle.getInt(KEY_CURRENT_POSITION, -1); + if(currentPos == mCurrentPosition){ + mCurrentItemView.restoreScrollInfo(bundle); } } } - - class ScrollInfo { - int currentPosition; - Object currentScrollInfo; - - public ScrollInfo(int currentPosition, Object currentScrollInfo) { - this.currentPosition = currentPosition; - this.currentScrollInfo = currentScrollInfo; - } - } } } \ No newline at end of file diff --git a/qmuidemo/src/main/java/com/qmuiteam/qmuidemo/fragment/lab/QDContinuousNestedScrollBaseFragment.java b/qmuidemo/src/main/java/com/qmuiteam/qmuidemo/fragment/lab/QDContinuousNestedScrollBaseFragment.java index 9a8c85022..07d8bce9f 100644 --- a/qmuidemo/src/main/java/com/qmuiteam/qmuidemo/fragment/lab/QDContinuousNestedScrollBaseFragment.java +++ b/qmuidemo/src/main/java/com/qmuiteam/qmuidemo/fragment/lab/QDContinuousNestedScrollBaseFragment.java @@ -16,6 +16,7 @@ package com.qmuiteam.qmuidemo.fragment.lab; +import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; @@ -35,6 +36,7 @@ public abstract class QDContinuousNestedScrollBaseFragment extends BaseFragment @BindView(R.id.topbar) QMUITopBarLayout mTopBarLayout; @BindView(R.id.coordinator) QMUIContinuousNestedScrollLayout mCoordinatorLayout; + private Bundle mSavedScrollInfo = new Bundle(); @Override protected View onCreateView() { @@ -75,6 +77,8 @@ private void showBottomSheet() { .addItem("scrollBy -40dp") .addItem("smoothScrollBy 100dp/1s") .addItem("smoothScrollBy -100dp/1s") + .addItem("save current scroll info") + .addItem("restore scroll info") .setOnSheetItemClickListener(new QMUIBottomSheet.BottomListSheetBuilder.OnSheetItemClickListener() { @Override public void onClick(QMUIBottomSheet dialog, View itemView, int position, String tag) { @@ -100,6 +104,11 @@ public void onClick(QMUIBottomSheet dialog, View itemView, int position, String case 6: mCoordinatorLayout.smoothScrollBy(QMUIDisplayHelper.dp2px(getContext(), -100), 1000); break; + case 7: + mCoordinatorLayout.saveScrollInfo(mSavedScrollInfo); + break; + case 8: + mCoordinatorLayout.restoreScrollInfo(mSavedScrollInfo); } dialog.dismiss(); }