Add current page change while scroll in ContentLayout
This commit is contained in:
1 parent
d617ef402d
commit
71217857f1
4 files changed
+66
-23
No files matched your search
@@ -144,13 +144,6 @@ public class ContentLayout extends FrameLayout {
|
||||
|
||||
private ViewTransition mViewTransition;
|
||||
|
||||
private RecyclerView.OnScrollListener mOnScrollListener = new RecyclerView.OnScrollListener() {
|
||||
@Override
|
||||
public void onScrolled(RecyclerView recyclerView, int dx, int dy){
|
||||
ContentHelper.this.onScrolled(recyclerView, dx, dy);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Store data
|
||||
*/
|
||||
@@ -185,6 +178,38 @@ public class ContentLayout extends FrameLayout {
|
||||
private int mCurrentTaskType;
|
||||
private int mCurrentTaskPage;
|
||||
|
||||
private RecyclerView.OnScrollListener mOnScrollListener = new RecyclerView.OnScrollListener() {
|
||||
|
||||
@Override
|
||||
public void onScrollStateChanged(RecyclerView recyclerView, int newState){
|
||||
if (newState == RecyclerView.SCROLL_STATE_IDLE) {
|
||||
if (mPageVolume <= 0) {
|
||||
return;
|
||||
}
|
||||
if (mLastIndex == 0) {
|
||||
mCurrentPage = 0;
|
||||
mFirstIndex = 0;
|
||||
mLastIndex = mPageVolume;
|
||||
}
|
||||
|
||||
int firstVisiblePosition = LayoutManagerUtils.getFirstVisibleItemPostion(mLayoutManager);
|
||||
int lastVisiblePosition = LayoutManagerUtils.getLastVisibleItemPostion(mLayoutManager);
|
||||
|
||||
int pageChanged = (firstVisiblePosition - mFirstIndex) / mPageVolume;
|
||||
if (pageChanged <= 0) {
|
||||
pageChanged = (lastVisiblePosition - mLastIndex + 1) / mPageVolume;
|
||||
}
|
||||
|
||||
if (pageChanged != 0) {
|
||||
mCurrentPage = mCurrentPage + pageChanged;
|
||||
int offset = pageChanged * mPageVolume;
|
||||
mFirstIndex += offset;
|
||||
mLastIndex += offset;
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
public ContentHelper() {
|
||||
mData = new ArrayList<>();
|
||||
mIdGenerator = IntIdGenerator.create();
|
||||
@@ -209,10 +234,6 @@ public class ContentLayout extends FrameLayout {
|
||||
mRefreshLayout.setOnFooterRefreshListener(this);
|
||||
}
|
||||
|
||||
private RecyclerView.OnScrollListener getOnScrollListener() {
|
||||
return mOnScrollListener;
|
||||
}
|
||||
|
||||
protected abstract RecyclerView.LayoutManager generateLayoutManager();
|
||||
|
||||
public void showContent() {
|
||||
@@ -429,10 +450,6 @@ public class ContentLayout extends FrameLayout {
|
||||
LayoutManagerUtils.scrollToPositionWithOffset(mLayoutManager, 0, 0);
|
||||
}
|
||||
|
||||
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getItemCount() {
|
||||
return mData.size();
|
||||
|
||||
@@ -23,6 +23,8 @@ public class SceneApplication extends Application {
|
||||
|
||||
@Override
|
||||
public void onCreate() {
|
||||
super.onCreate();
|
||||
|
||||
mSceneManager = new SceneManager();
|
||||
StageActivity.setSceneManager(mSceneManager);
|
||||
Scene.setSceneManager(mSceneManager);
|
||||
|
||||
@@ -32,4 +32,28 @@ public final class LayoutManagerUtils {
|
||||
layoutManager.getClass().getName());
|
||||
}
|
||||
}
|
||||
|
||||
public static int getFirstVisibleItemPostion(RecyclerView.LayoutManager layoutManager) {
|
||||
if (layoutManager instanceof LinearLayoutManager) {
|
||||
return ((LinearLayoutManager) layoutManager).findFirstVisibleItemPosition();
|
||||
} else if (layoutManager instanceof StaggeredGridLayoutManager) {
|
||||
int[] positions = ((StaggeredGridLayoutManager) layoutManager).findFirstVisibleItemPositions(null);
|
||||
return MathUtils.min(positions);
|
||||
} else {
|
||||
throw new IllegalStateException("Can't do getFirstVisibleItemPostion for " +
|
||||
layoutManager.getClass().getName());
|
||||
}
|
||||
}
|
||||
|
||||
public static int getLastVisibleItemPostion(RecyclerView.LayoutManager layoutManager) {
|
||||
if (layoutManager instanceof LinearLayoutManager) {
|
||||
return ((LinearLayoutManager) layoutManager).findLastVisibleItemPosition();
|
||||
} else if (layoutManager instanceof StaggeredGridLayoutManager) {
|
||||
int[] positions = ((StaggeredGridLayoutManager) layoutManager).findLastVisibleItemPositions(null);
|
||||
return MathUtils.max(positions);
|
||||
} else {
|
||||
throw new IllegalStateException("Can't do getFirstVisibleItemPostion for " +
|
||||
layoutManager.getClass().getName());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -103,15 +103,15 @@ public final class MathUtils {
|
||||
return a < b ? (a < c ? a : c) : (b < c ? b : c);
|
||||
}
|
||||
|
||||
public static float min(float... arg) {
|
||||
int length = arg.length;
|
||||
public static float min(float... args) {
|
||||
int length = args.length;
|
||||
if (length <= 0) {
|
||||
throw new IllegalArgumentException("Empty argument");
|
||||
} else {
|
||||
float n = arg[0];
|
||||
float n = args[0];
|
||||
float m;
|
||||
for (int i = 1; i < length; i++) {
|
||||
m = arg[i];
|
||||
m = args[i];
|
||||
if (m < n)
|
||||
n = m;
|
||||
}
|
||||
@@ -119,15 +119,15 @@ public final class MathUtils {
|
||||
}
|
||||
}
|
||||
|
||||
public static int min(int... arg) {
|
||||
int length = arg.length;
|
||||
public static int min(int... args) {
|
||||
int length = args.length;
|
||||
if (length <= 0) {
|
||||
throw new IllegalArgumentException("Empty argument");
|
||||
} else {
|
||||
int n = arg[0];
|
||||
int n = args[0];
|
||||
int m;
|
||||
for (int i = 1; i < length; i++) {
|
||||
m = arg[i];
|
||||
m = args[i];
|
||||
if (m < n)
|
||||
n = m;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user