Add scrollToPositionProperly
This commit is contained in:
1 parent
dae22d0284
commit
903373a625
2 files changed
+37
-3
No files matched your search
@@ -221,6 +221,14 @@ public class ContentLayout extends FrameLayout {
|
||||
}
|
||||
};
|
||||
|
||||
private LayoutManagerUtils.OnScrollToPositionListener mOnScrollToPositionListener =
|
||||
new LayoutManagerUtils.OnScrollToPositionListener() {
|
||||
@Override
|
||||
public void onScrollToPosition() {
|
||||
ContentHelper.this.onScrollToPosition();
|
||||
}
|
||||
};
|
||||
|
||||
public ContentHelper(Context context) {
|
||||
mContext = context;
|
||||
mData = new ArrayList<>();
|
||||
@@ -352,7 +360,7 @@ public class ContentLayout extends FrameLayout {
|
||||
mLastIndex += pageVolume;
|
||||
|
||||
mRecyclerView.stopScroll();
|
||||
LayoutManagerUtils.smoothScrollToPosition(mLayoutManager, mContext, mFirstIndex - 1, 100);
|
||||
LayoutManagerUtils.scrollToPositionProperly(mLayoutManager, mContext, mFirstIndex - 1, mOnScrollToPositionListener);
|
||||
} else {
|
||||
mCurrentPage = mFirstPage;
|
||||
mFirstIndex = 0;
|
||||
@@ -466,8 +474,8 @@ public class ContentLayout extends FrameLayout {
|
||||
if (mViewTransition.getShownViewIndex() == 0) {
|
||||
// Go to top
|
||||
mRecyclerView.stopScroll();
|
||||
LayoutManagerUtils.scrollToPositionWithOffset(mLayoutManager, 0, 0);
|
||||
onScrollToPosition();
|
||||
LayoutManagerUtils.scrollToPositionProperly(mLayoutManager,
|
||||
mContext, 0, mOnScrollToPositionListener);
|
||||
// Show header refresh
|
||||
mRefreshLayout.setFooterRefreshing(false);
|
||||
mRefreshLayout.setHeaderRefreshing(true);
|
||||
|
||||
@@ -78,6 +78,28 @@ public final class LayoutManagerUtils {
|
||||
layoutManager.startSmoothScroll(smoothScroller);
|
||||
}
|
||||
|
||||
public static void scrollToPositionProperly(final RecyclerView.LayoutManager layoutManager,
|
||||
final Context context, final int position, final OnScrollToPositionListener listener) {
|
||||
AppHandler.getInstance().post(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
int first = getFirstVisibleItemPostion(layoutManager);
|
||||
int last = getLastVisibleItemPostion(layoutManager);
|
||||
int offset = Math.abs(position - first);
|
||||
int max = last - first;
|
||||
if (offset < max && max > 0) {
|
||||
smoothScrollToPosition(layoutManager, context, position,
|
||||
MathUtils.lerp(100, 25, (offset / max)));
|
||||
} else {
|
||||
scrollToPositionWithOffset(layoutManager, position, 0);
|
||||
if (listener != null) {
|
||||
listener.onScrollToPosition();
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public static int getFirstVisibleItemPostion(RecyclerView.LayoutManager layoutManager) {
|
||||
if (layoutManager instanceof LinearLayoutManager) {
|
||||
return ((LinearLayoutManager) layoutManager).findFirstVisibleItemPosition();
|
||||
@@ -101,4 +123,8 @@ public final class LayoutManagerUtils {
|
||||
layoutManager.getClass().getName());
|
||||
}
|
||||
}
|
||||
|
||||
public interface OnScrollToPositionListener {
|
||||
void onScrollToPosition();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user