Skip to content

Commit

Permalink
Reimplement scroll with NestedScrolling APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
race604 committed Aug 20, 2015
1 parent 7faad47 commit 4efa6a0
Show file tree
Hide file tree
Showing 12 changed files with 505 additions and 372 deletions.
2 changes: 1 addition & 1 deletion .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 8 additions & 8 deletions app/app.iml
Expand Up @@ -71,9 +71,9 @@
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/dependency-cache" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/dex" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/dex-cache" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/com.android.support/appcompat-v7/22.2.0/jars" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/com.android.support/recyclerview-v7/22.1.1/jars" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/com.android.support/support-v4/22.2.0/jars" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/com.android.support/appcompat-v7/23.0.0/jars" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/com.android.support/recyclerview-v7/23.0.0/jars" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/com.android.support/support-v4/23.0.0/jars" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/jp.wasabeef/recyclerview-animators/1.2.0/jars" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/incremental" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/jacoco" />
Expand All @@ -90,13 +90,13 @@
<excludeFolder url="file://$MODULE_DIR$/build/outputs" />
<excludeFolder url="file://$MODULE_DIR$/build/tmp" />
</content>
<orderEntry type="jdk" jdkName="Android API 22 Platform" jdkType="Android SDK" />
<orderEntry type="jdk" jdkName="Android API 23 Platform" jdkType="Android SDK" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" exported="" name="recyclerview-v7-22.1.1" level="project" />
<orderEntry type="library" exported="" name="appcompat-v7-23.0.0" level="project" />
<orderEntry type="library" exported="" name="recyclerview-v7-23.0.0" level="project" />
<orderEntry type="library" exported="" name="recyclerview-animators-1.2.0" level="project" />
<orderEntry type="library" exported="" name="support-annotations-22.2.0" level="project" />
<orderEntry type="library" exported="" name="support-v4-22.2.0" level="project" />
<orderEntry type="library" exported="" name="appcompat-v7-22.2.0" level="project" />
<orderEntry type="library" exported="" name="support-v4-23.0.0" level="project" />
<orderEntry type="library" exported="" name="support-annotations-23.0.0" level="project" />
<orderEntry type="module" module-name="library" exported="" />
</component>
</module>
8 changes: 4 additions & 4 deletions app/build.gradle
@@ -1,13 +1,13 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 22
compileSdkVersion 23
buildToolsVersion "22.0.1"

defaultConfig {
applicationId "com.race604.flyrefresh"
minSdkVersion 11
targetSdkVersion 22
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
Expand All @@ -21,8 +21,8 @@ android {

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:appcompat-v7:22.2.0'
compile 'com.android.support:appcompat-v7:23.0.0'
compile project(':library')
compile 'com.android.support:recyclerview-v7:22.1.1'
compile 'com.android.support:recyclerview-v7:23.0.0'
compile 'jp.wasabeef:recyclerview-animators:1.2.0@aar'
}
12 changes: 12 additions & 0 deletions app/src/main/java/com/race604/flyrefresh/sample/MainActivity.java
Expand Up @@ -70,6 +70,18 @@ private void initDataSet() {
mDataSet.add(new ItemData(Color.parseColor("#76A9FC"), R.mipmap.ic_assessment_white_24dp, "Meeting Minutes", new Date(2014 - 1900, 2, 9)));
mDataSet.add(new ItemData(Color.GRAY, R.mipmap.ic_folder_white_24dp, "Favorites Photos", new Date(2014 - 1900, 1, 3)));
mDataSet.add(new ItemData(Color.GRAY, R.mipmap.ic_folder_white_24dp, "Photos", new Date(2014 - 1900, 0, 9)));

mDataSet.add(new ItemData(Color.parseColor("#76A9FC"), R.mipmap.ic_assessment_white_24dp, "Meeting Minutes", new Date(2014 - 1900, 2, 9)));
mDataSet.add(new ItemData(Color.GRAY, R.mipmap.ic_folder_white_24dp, "Favorites Photos", new Date(2014 - 1900, 1, 3)));
mDataSet.add(new ItemData(Color.GRAY, R.mipmap.ic_folder_white_24dp, "Photos", new Date(2014 - 1900, 0, 9)));

mDataSet.add(new ItemData(Color.parseColor("#76A9FC"), R.mipmap.ic_assessment_white_24dp, "Meeting Minutes", new Date(2014 - 1900, 2, 9)));
mDataSet.add(new ItemData(Color.GRAY, R.mipmap.ic_folder_white_24dp, "Favorites Photos", new Date(2014 - 1900, 1, 3)));
mDataSet.add(new ItemData(Color.GRAY, R.mipmap.ic_folder_white_24dp, "Photos", new Date(2014 - 1900, 0, 9)));

mDataSet.add(new ItemData(Color.parseColor("#76A9FC"), R.mipmap.ic_assessment_white_24dp, "Meeting Minutes", new Date(2014 - 1900, 2, 9)));
mDataSet.add(new ItemData(Color.GRAY, R.mipmap.ic_folder_white_24dp, "Favorites Photos", new Date(2014 - 1900, 1, 3)));
mDataSet.add(new ItemData(Color.GRAY, R.mipmap.ic_folder_white_24dp, "Photos", new Date(2014 - 1900, 0, 9)));
}

private void addItemData() {
Expand Down
8 changes: 4 additions & 4 deletions library/build.gradle
Expand Up @@ -5,13 +5,13 @@ apply plugin: 'com.jfrog.bintray'
version = "1.0.2"

android {
compileSdkVersion 22
compileSdkVersion 23
buildToolsVersion "22.0.1"
resourcePrefix "flyrefresh__"

defaultConfig {
minSdkVersion 11
targetSdkVersion 22
targetSdkVersion 23
versionCode 3
versionName version
}
Expand All @@ -25,8 +25,8 @@ android {

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.2.0'
compile 'com.android.support:design:22.2.0'
compile 'com.android.support:appcompat-v7:23.0.0'
compile 'com.android.support:design:23.0.0'
}

def siteUrl = 'https://github.com/race604/FlyRefresh'
Expand Down
10 changes: 5 additions & 5 deletions library/library.iml
Expand Up @@ -87,11 +87,11 @@
<excludeFolder url="file://$MODULE_DIR$/build/outputs" />
<excludeFolder url="file://$MODULE_DIR$/build/tmp" />
</content>
<orderEntry type="jdk" jdkName="Android API 22 Platform" jdkType="Android SDK" />
<orderEntry type="jdk" jdkName="Android API 23 Platform" jdkType="Android SDK" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" exported="" name="support-annotations-22.2.0" level="project" />
<orderEntry type="library" exported="" name="support-v4-22.2.0" level="project" />
<orderEntry type="library" exported="" name="appcompat-v7-22.2.0" level="project" />
<orderEntry type="library" exported="" name="design-22.2.0" level="project" />
<orderEntry type="library" exported="" name="appcompat-v7-23.0.0" level="project" />
<orderEntry type="library" exported="" name="support-v4-23.0.0" level="project" />
<orderEntry type="library" exported="" name="design-23.0.0" level="project" />
<orderEntry type="library" exported="" name="support-annotations-23.0.0" level="project" />
</component>
</module>

This file was deleted.

Expand Up @@ -38,11 +38,6 @@ public FlyRefreshLayout(Context context, AttributeSet attrs, int defStyleAttr) {
init(context);
}

public FlyRefreshLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init(context);
}

private void init(Context context) {
MountanScenceView headerView = new MountanScenceView(getContext());
LayoutParams lp = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, mHeaderController.getMaxHeight());
Expand Down
121 changes: 55 additions & 66 deletions library/src/main/java/com/race604/flyrefresh/HeaderController.java
Expand Up @@ -11,13 +11,10 @@ public class HeaderController {
private float mOverDistance;

private float mResistance = 0.5f;
private float mStartX;
private float mStartY;
private int mOffsetX;
private int mOffsetY;
private boolean mIsInTouch = false;
private int mStartPos = -1;
private int mCurrentPos = -1;
private float mScroll = 0;
private int mMaxScroll = 0;
private int mMinScroll = 0;

public HeaderController(int height, int maxHeight, int minHeight) {

Expand All @@ -34,7 +31,9 @@ public void setSize(int height, int maxHeight, int minHeight) {
mMinHegiht = Math.max(0, minHeight);
mOverDistance = mMaxHegiht - mHeight;

mCurrentPos = mStartPos = mHeight;
mScroll = 0;
mMaxScroll = mHeight - mMinHegiht;
mMinScroll = mHeight - mMaxHegiht;
}

public int getMaxHeight() {
Expand All @@ -49,91 +48,81 @@ public int getHeight() {
return mHeight;
}

public int getCurrentPos() {
return mCurrentPos;
public int getScroll() {
return (int) mScroll;
}

public int getOffsetX() {
return mOffsetX;
public int getMaxScroll() {
return mMaxScroll;
}

public int getOffsetY() {
return mOffsetY;
public int getMinScroll() {
return mMinScroll;
}

public boolean isInTouch() {
return mIsInTouch;
}

public boolean hasMoved() {
return mStartPos != mCurrentPos;
}
public boolean canMoveUp() {
return mCurrentPos > mMinHegiht;
}

public boolean canMoveDown() {
return mCurrentPos < mMaxHegiht;
public int getCurPosition() {
return (int) (mHeight - mScroll);
}

public void onTouchRelease() {
mIsInTouch = false;
}

public void onTouchDown(float x, float y) {
mIsInTouch = true;
mStartX = x;
mStartY = y;
mOffsetX = mOffsetY = 0;
startMove();
public boolean isInTouch() {
return mIsInTouch;
}

public void onTouchMove(float x, float y) {
mOffsetX = (int) (x - mStartX);
mOffsetY = (int) (y - mStartY);
/**
* Check if can scroll down to show top
* @return
*/
public boolean canScrollDown() {
return mScroll > mMinScroll;
}

public void startMove() {
mStartPos = mCurrentPos;
/**
* Check if can scroll up to show bottom
* @return
*/
public boolean canScrollUp() {
return mScroll < mMaxScroll;
}

public int willMove(float deltaY) {
public int move(float deltaY) {
float willTo;
if (mStartPos > mHeight) {
willTo = mStartPos + deltaY * mResistance;
if (willTo < mHeight) {
willTo = mHeight + (willTo - mHeight) / mResistance;
float consumed = deltaY;
if (mScroll >= 0) {
willTo = mScroll + deltaY;
if (willTo < 0) {
willTo = willTo * mResistance;
if (willTo < mMinScroll) {
consumed -= (willTo - mMinScroll) / mResistance;
willTo = mMinScroll;
}
} else if (willTo > mMaxHegiht) {
consumed -= willTo - mMaxScroll;
willTo = mMaxScroll;
}
} else {
willTo = mStartPos + deltaY;
if (willTo > mHeight) {
willTo = mHeight + (willTo - mHeight) * mResistance;
willTo = mScroll + deltaY * mResistance;
if (willTo > 0) {
willTo = willTo / mResistance;
if (willTo > mMaxScroll) {
consumed -= willTo - mMaxScroll;
willTo = mMaxScroll;
}
} else if (willTo < mMinScroll) {
consumed -= willTo - mMinScroll;
willTo = mMinScroll;
}
}

int offsetY = Math.max(mMinHegiht, Math.min(mMaxHegiht, (int)willTo));
int move = offsetY - mCurrentPos;
mCurrentPos = offsetY;
return move;
}

public int moveTo(float pos) {
int offsetY = Math.max(mMinHegiht, Math.min(mMaxHegiht, (int)pos));
int delta = offsetY - mCurrentPos;
mCurrentPos = offsetY;
return delta;
mScroll = willTo;
return (int) consumed;
}

public boolean isOverHeight() {
return mCurrentPos > mHeight;
return mScroll < 0;
}

public float getMovePercentage() {
if (mCurrentPos >= mHeight) {
return (mCurrentPos - mHeight) / mOverDistance;
} else {
return (mCurrentPos - mHeight) / mOverDistance;
}
return -mScroll / mOverDistance;
}

public boolean needSendRefresh() {
Expand Down
11 changes: 0 additions & 11 deletions library/src/main/java/com/race604/flyrefresh/IScrollHandler.java

This file was deleted.

0 comments on commit 4efa6a0

Please sign in to comment.