-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
361 additions
and
141 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<TextView xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:fontFamily="sans-serif-medium" | ||
android:text="Loading..." | ||
android:textColor="@android:color/white" | ||
android:textSize="20sp"/> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,10 @@ | ||
<menu xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:app="http://schemas.android.com/apk/res-auto" | ||
xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity"> | ||
<item android:id="@+id/action_settings" android:title="@string/action_settings" | ||
android:orderInCategory="100" app:showAsAction="never" /> | ||
xmlns:app="http://schemas.android.com/apk/res-auto" | ||
xmlns:tools="http://schemas.android.com/tools" | ||
tools:context=".MainActivity"> | ||
<item | ||
android:id="@+id/action_refresh" | ||
android:orderInCategory="100" | ||
android:title="@string/action_refresh" | ||
app:showAsAction="never"/> | ||
</menu> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
136 changes: 117 additions & 19 deletions
136
jellyrefresh/src/main/java/uk/co/imallan/jellyrefresh/JellyRefreshLayout.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,141 @@ | ||
package uk.co.imallan.jellyrefresh; | ||
|
||
import android.content.Context; | ||
import android.content.res.TypedArray; | ||
import android.graphics.Color; | ||
import android.os.Build; | ||
import android.support.annotation.Nullable; | ||
import android.util.AttributeSet; | ||
import android.view.Gravity; | ||
import android.view.View; | ||
import android.view.ViewGroup; | ||
import android.widget.FrameLayout; | ||
|
||
public class JellyRefreshLayout extends PullToRefreshLayout { | ||
public class JellyRefreshLayout extends PullToRefreshLayout implements PullToRefreshLayout.PullToRefreshPullingListener { | ||
|
||
private JellyLayout mJellyLayout; | ||
private View mLoadingView; | ||
|
||
public JellyRefreshLayout(Context context) { | ||
super(context); | ||
init(); | ||
this(context, null); | ||
} | ||
|
||
public JellyRefreshLayout(Context context, AttributeSet attrs) { | ||
super(context, attrs); | ||
init(); | ||
this(context, attrs, 0); | ||
} | ||
|
||
public JellyRefreshLayout(Context context, AttributeSet attrs, int defStyleAttr) { | ||
super(context, attrs, defStyleAttr); | ||
init(); | ||
resolveAttributes(attrs); | ||
} | ||
|
||
private void resolveAttributes(@Nullable AttributeSet attrs) { | ||
if (attrs == null) return; | ||
TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.JellyRefreshLayout); | ||
try { | ||
int color = a.getColor(R.styleable.JellyRefreshLayout_jellyColor, Color.WHITE); | ||
float height = a.getDimension(R.styleable.JellyRefreshLayout_headerHeight, mHeaderHeight); | ||
float pullHeight = a.getDimension(R.styleable.JellyRefreshLayout_pullHeight, mPullHeight); | ||
float triggerHeight = a.getDimension(R.styleable.JellyRefreshLayout_pullHeight, mTriggerHeight); | ||
mJellyLayout.setColor(color); | ||
mHeaderHeight = height; | ||
mPullHeight = pullHeight; | ||
mTriggerHeight = triggerHeight; | ||
} finally { | ||
a.recycle(); | ||
} | ||
} | ||
|
||
@Override | ||
public void setRefreshing(boolean refreshing) { | ||
if (refreshing) { | ||
post(() -> mJellyLayout.setPointX(mJellyLayout.getWidth() / 2)); | ||
} | ||
super.setRefreshing(refreshing); | ||
} | ||
|
||
private void init() { | ||
JellyLayout jellyLayout = new JellyLayout(getContext()); | ||
mJellyLayout = new JellyLayout(getContext()); | ||
|
||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { | ||
jellyLayout.setElevation(getElevation()); | ||
mJellyLayout.setElevation(getElevation()); | ||
} | ||
setHeaderView(jellyLayout); | ||
setPullingListener(new PullToRefreshPullingListener() { | ||
@Override | ||
public void onPulling(PullToRefreshLayout pullToRefreshLayout, float fraction, float pointXPosition) { | ||
jellyLayout.setPointX(pointXPosition); | ||
} | ||
|
||
@Override | ||
public void onReleasing(PullToRefreshLayout pullToRefreshLayout, float fraction) { | ||
|
||
} | ||
}); | ||
setHeaderView(mJellyLayout); | ||
setPullingListener(this); | ||
} | ||
|
||
@Override | ||
protected void onStateChanged(@State int newState) { | ||
switch (newState) { | ||
case STATE_REFRESHING: | ||
if (mLoadingView != null) { | ||
mLoadingView.setVisibility(VISIBLE); | ||
} | ||
break; | ||
case STATE_REFRESHING_SETTLING: | ||
if (mLoadingView != null) { | ||
mLoadingView.setVisibility(VISIBLE); | ||
mLoadingView.setTranslationY(mPullHeight); | ||
mLoadingView.animate().translationY(0).setDuration(150).start(); | ||
} | ||
break; | ||
case STATE_DRAGGING: | ||
case STATE_IDLE: | ||
case STATE_SETTLING: | ||
case STATE_RELEASING: | ||
if (mLoadingView != null) { | ||
mLoadingView.setVisibility(INVISIBLE); | ||
} | ||
break; | ||
} | ||
} | ||
|
||
@Override | ||
public void onPulling(float fraction, float pointXPosition) { | ||
mJellyLayout.setPointX(pointXPosition); | ||
} | ||
|
||
@Override | ||
public void onTranslationYChanged(float translationY) { | ||
switch (getState()) { | ||
case STATE_DRAGGING: | ||
case STATE_RELEASING: | ||
mJellyLayout.mHeaderHeight = Math.min(translationY / 2, mHeaderHeight); | ||
mJellyLayout.mPullHeight = translationY; | ||
break; | ||
case STATE_REFRESHING: | ||
mJellyLayout.mHeaderHeight = mHeaderHeight; | ||
mJellyLayout.mPullHeight = mHeaderHeight; | ||
break; | ||
case STATE_SETTLING: | ||
mJellyLayout.mHeaderHeight = translationY; | ||
mJellyLayout.mPullHeight = translationY; | ||
break; | ||
case STATE_REFRESHING_SETTLING: | ||
mJellyLayout.mHeaderHeight = mHeaderHeight; | ||
if (translationY > mHeaderHeight) { | ||
float dy = translationY - mHeaderHeight; | ||
float acceleratedHeight = translationY - 2 * dy; | ||
mJellyLayout.mPullHeight = Math.max(mHeaderHeight, acceleratedHeight); | ||
} else { | ||
mJellyLayout.mPullHeight = mHeaderHeight; | ||
} | ||
break; | ||
case STATE_IDLE: | ||
mJellyLayout.mHeaderHeight = 0; | ||
mJellyLayout.mPullHeight = 0; | ||
break; | ||
} | ||
mJellyLayout.postInvalidate(); | ||
} | ||
|
||
public void setLoadingView(View view) { | ||
FrameLayout.LayoutParams params = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, | ||
ViewGroup.LayoutParams.WRAP_CONTENT); | ||
params.gravity = Gravity.CENTER_HORIZONTAL | Gravity.TOP; | ||
mJellyLayout.addView(view, params); | ||
view.setVisibility(INVISIBLE); | ||
mLoadingView = view; | ||
} | ||
} |
Oops, something went wrong.