Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added toggle, which enables to hide panel with gesture. #606

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion demo/res/layout/activity_demo.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
sothree:umanoParallaxOffset="100dp"
sothree:umanoDragView="@+id/dragView"
sothree:umanoOverlay="true"
sothree:umanoScrollableView="@+id/list">
sothree:umanoScrollableView="@+id/list"
sothree:umanoHideWithFling="true">

<!-- MAIN CONTENT -->
<FrameLayout
Expand Down
14 changes: 12 additions & 2 deletions demo/src/com/sothree/slidinguppanel/demo/DemoActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public class DemoActivity extends ActionBarActivity {
private static final String TAG = "DemoActivity";

private SlidingUpPanelLayout mLayout;
private TextView mText;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand Down Expand Up @@ -93,13 +94,13 @@ public void onPanelSlide(View panel, float slideOffset) {
@Override
public void onPanelExpanded(View panel) {
Log.i(TAG, "onPanelExpanded");

mText.setText("Panel is expanded");
}

@Override
public void onPanelCollapsed(View panel) {
Log.i(TAG, "onPanelCollapsed");

mText.setText("Panel is collapsed");
}

@Override
Expand All @@ -110,9 +111,18 @@ public void onPanelAnchored(View panel) {
@Override
public void onPanelHidden(View panel) {
Log.i(TAG, "onPanelHidden");
mText.setText("Panel is hidden. Click to show panel");
}
});

mText = (TextView) findViewById(R.id.main);
mText.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mLayout.setPanelState(PanelState.COLLAPSED);
}
});

TextView t = (TextView) findViewById(R.id.name);
t.setText(Html.fromHtml(getString(R.string.hello)));
Button f = (Button) findViewById(R.id.follow);
Expand Down
1 change: 1 addition & 0 deletions library/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<attr name="umanoOverlay" format="boolean"/>
<attr name="umanoClipPanel" format="boolean"/>
<attr name="umanoAnchorPoint" format="float" />
<attr name="umanoHideWithFling" format="boolean" />
<attr name="umanoInitialState" format="enum">
<enum name="expanded" value="0" />
<enum name="collapsed" value="1" />
Expand Down
40 changes: 32 additions & 8 deletions library/src/com/sothree/slidinguppanel/SlidingUpPanelLayout.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ public class SlidingUpPanelLayout extends ViewGroup {
* Default is set to true for clip panel for performance reasons
*/
private static final boolean DEFAULT_CLIP_PANEL_FLAG = true;
/**
* Default is set to false for hiding panel with fling down event
*/
private static final boolean DEFAULT_HIDE_WITH_FLING_FLAG = false;
/**
* Default attributes for layout
*/
Expand Down Expand Up @@ -132,6 +136,11 @@ public class SlidingUpPanelLayout extends ViewGroup {
*/
private boolean mClipPanel = DEFAULT_CLIP_PANEL_FLAG;

/**
* User can hide panel with fling down gesture
*/
private boolean mHideWithFlingEnabled = DEFAULT_HIDE_WITH_FLING_FLAG;

/**
* If provided, the panel can be dragged by only this view. Otherwise, the entire panel can be
* used for dragging.
Expand Down Expand Up @@ -314,9 +323,10 @@ public SlidingUpPanelLayout(Context context, AttributeSet attrs, int defStyle) {
if (defAttrs != null) {
int gravity = defAttrs.getInt(0, Gravity.NO_GRAVITY);
setGravity(gravity);

defAttrs.recycle();
}

defAttrs.recycle();

TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.SlidingUpPanelLayout);

Expand All @@ -333,6 +343,7 @@ public SlidingUpPanelLayout(Context context, AttributeSet attrs, int defStyle) {

mOverlayContent = ta.getBoolean(R.styleable.SlidingUpPanelLayout_umanoOverlay, DEFAULT_OVERLAY_FLAG);
mClipPanel = ta.getBoolean(R.styleable.SlidingUpPanelLayout_umanoClipPanel, DEFAULT_CLIP_PANEL_FLAG);
mHideWithFlingEnabled = ta.getBoolean(R.styleable.SlidingUpPanelLayout_umanoHideWithFling, DEFAULT_HIDE_WITH_FLING_FLAG);

mAnchorPoint = ta.getFloat(R.styleable.SlidingUpPanelLayout_umanoAnchorPoint, DEFAULT_ANCHOR_POINT);

Expand All @@ -342,9 +353,9 @@ public SlidingUpPanelLayout(Context context, AttributeSet attrs, int defStyle) {
if (interpolatorResId != -1) {
scrollerInterpolator = AnimationUtils.loadInterpolator(context, interpolatorResId);
}
}

ta.recycle();
ta.recycle();
}
}

final float density = context.getResources().getDisplayMetrics().density;
Expand Down Expand Up @@ -831,7 +842,7 @@ protected void onLayout(boolean changed, int l, int t, int r, int b) {
mSlideOffset = mAnchorPoint;
break;
case HIDDEN:
int newTop = computePanelTopPosition(0.0f) + (mIsSlidingUp ? +mPanelHeight : -mPanelHeight);
int newTop = computeHiddenPanelTopPosition();
mSlideOffset = computeSlideOffset(newTop);
break;
default:
Expand Down Expand Up @@ -1089,6 +1100,13 @@ private int computePanelTopPosition(float slideOffset) {
: getPaddingTop() - slidingViewHeight + mPanelHeight + slidePixelOffset;
}

/*
* Computes the top position of the panel based on the slide offset, considering that panel might be hidden.
*/
private int computeHiddenPanelTopPosition() {
return computePanelTopPosition(0.0f) + (mIsSlidingUp ? +mPanelHeight : -mPanelHeight);
}

/*
* Computes the slide offset based on the top position of the panel
*/
Expand Down Expand Up @@ -1144,7 +1162,7 @@ public void setPanelState(PanelState state) {
smoothSlideTo(1.0f, 0);
break;
case HIDDEN:
int newTop = computePanelTopPosition(0.0f) + (mIsSlidingUp ? +mPanelHeight : -mPanelHeight);
int newTop = computeHiddenPanelTopPosition();
smoothSlideTo(computeSlideOffset(newTop), 0);
break;
}
Expand Down Expand Up @@ -1412,12 +1430,15 @@ public void onViewPositionChanged(View changedView, int left, int top, int dx, i

@Override
public void onViewReleased(View releasedChild, float xvel, float yvel) {
int target = 0;
int target;

// direction is always positive if we are sliding in the expanded direction
float direction = mIsSlidingUp ? -yvel : yvel;

if (direction > 0 && mSlideOffset <= mAnchorPoint) {
if (mHideWithFlingEnabled && mSlideOffset < 0 && direction < 0) {
// swipe down -> hide
target = computeHiddenPanelTopPosition();
} else if (direction > 0 && mSlideOffset <= mAnchorPoint) {
// swipe up -> expand and stop at anchor point
target = computePanelTopPosition(mAnchorPoint);
} else if (direction > 0 && mSlideOffset > mAnchorPoint) {
Expand Down Expand Up @@ -1451,7 +1472,10 @@ public int getViewVerticalDragRange(View child) {

@Override
public int clampViewPositionVertical(View child, int top, int dy) {
final int collapsedTop = computePanelTopPosition(0.f);
int collapsedTop = computePanelTopPosition(0.f);
if (mHideWithFlingEnabled) {
collapsedTop = computeHiddenPanelTopPosition();
}
final int expandedTop = computePanelTopPosition(1.0f);
if (mIsSlidingUp) {
return Math.min(Math.max(top, expandedTop), collapsedTop);
Expand Down