Skip to content

Commit

Permalink
port changes from support lib 27.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
laenger committed Mar 22, 2018
1 parent 9521e09 commit 0230dd8
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package biz.laenger.android.vpbs;

import static android.support.annotation.RestrictTo.Scope.LIBRARY_GROUP;

import android.content.Context;
import android.content.res.TypedArray;
import android.os.Parcel;
Expand All @@ -25,12 +27,8 @@
import android.support.annotation.RestrictTo;
import android.support.annotation.VisibleForTesting;
import android.support.design.widget.CoordinatorLayout;
import android.support.v4.os.ParcelableCompat;
import android.support.v4.os.ParcelableCompatCreatorCallbacks;
import android.support.v4.math.MathUtils;
import android.support.v4.view.AbsSavedState;
import android.support.v4.view.MotionEventCompat;
import android.support.v4.view.NestedScrollingChild;
import android.support.v4.view.VelocityTrackerCompat;
import android.support.v4.view.ViewCompat;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPagerUtils;
Expand All @@ -48,8 +46,6 @@
import java.lang.annotation.RetentionPolicy;
import java.lang.ref.WeakReference;

import static android.support.annotation.RestrictTo.Scope.LIBRARY_GROUP;


/**
* An interaction behavior plugin for a child view of {@link CoordinatorLayout} to make it work as
Expand Down Expand Up @@ -263,7 +259,7 @@ public boolean onInterceptTouchEvent(CoordinatorLayout parent, V child, MotionEv
mIgnoreEvents = true;
return false;
}
int action = MotionEventCompat.getActionMasked(event);
int action = event.getActionMasked();
// Record the velocity
if (action == MotionEvent.ACTION_DOWN) {
reset();
Expand All @@ -286,7 +282,8 @@ public boolean onInterceptTouchEvent(CoordinatorLayout parent, V child, MotionEv
case MotionEvent.ACTION_DOWN:
int initialX = (int) event.getX();
mInitialY = (int) event.getY();
View scroll = mNestedScrollingChildRef.get();
View scroll = mNestedScrollingChildRef != null
? mNestedScrollingChildRef.get() : null;
if (scroll != null && parent.isPointInChildBounds(scroll, initialX, mInitialY)) {
mActivePointerId = event.getPointerId(event.getActionIndex());
mTouchingScrollingChild = true;
Expand All @@ -313,11 +310,13 @@ public boolean onTouchEvent(CoordinatorLayout parent, V child, MotionEvent event
if (!child.isShown()) {
return false;
}
int action = MotionEventCompat.getActionMasked(event);
int action = event.getActionMasked();
if (mState == STATE_DRAGGING && action == MotionEvent.ACTION_DOWN) {
return true;
}
mViewDragHelper.processTouchEvent(event);
if (mViewDragHelper != null) {
mViewDragHelper.processTouchEvent(event);
}
// Record the velocity
if (action == MotionEvent.ACTION_DOWN) {
reset();
Expand Down Expand Up @@ -364,7 +363,7 @@ public void onNestedPreScroll(CoordinatorLayout coordinatorLayout, V child, View
setStateInternal(STATE_DRAGGING);
}
} else if (dy < 0) { // Downward
if (!ViewCompat.canScrollVertically(target, -1)) {
if (!target.canScrollVertically(-1)) {
if (newTop <= mMaxOffset || mHideable) {
consumed[1] = dy;
ViewCompat.offsetTopAndBottom(child, -dy);
Expand All @@ -387,7 +386,8 @@ public void onStopNestedScroll(CoordinatorLayout coordinatorLayout, V child, Vie
setStateInternal(STATE_EXPANDED);
return;
}
if (target != mNestedScrollingChildRef.get() || !mNestedScrolled) {
if (mNestedScrollingChildRef == null || target != mNestedScrollingChildRef.get()
|| !mNestedScrolled) {
return;
}
int top;
Expand Down Expand Up @@ -566,7 +566,7 @@ public void run() {
* Gets the current state of the bottom sheet.
*
* @return One of {@link #STATE_EXPANDED}, {@link #STATE_COLLAPSED}, {@link #STATE_DRAGGING},
* and {@link #STATE_SETTLING}.
* {@link #STATE_SETTLING}, and {@link #STATE_HIDDEN}.
*/
@State
public final int getState() {
Expand Down Expand Up @@ -604,8 +604,9 @@ boolean shouldHide(View child, float yvel) {
return Math.abs(newTop - mMaxOffset) / (float) mPeekHeight > HIDE_THRESHOLD;
}

private View findScrollingChild(View view) {
if (view instanceof NestedScrollingChild) {
@VisibleForTesting
View findScrollingChild(View view) {
if (ViewCompat.isNestedScrollingEnabled(view)) {
return view;
}
if (view instanceof ViewPager) {
Expand All @@ -629,7 +630,7 @@ private View findScrollingChild(View view) {

private float getYVelocity() {
mVelocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
return VelocityTrackerCompat.getYVelocity(mVelocityTracker, mActivePointerId);
return mVelocityTracker.getYVelocity(mActivePointerId);
}

void startSettlingAnimation(View child, int state) {
Expand All @@ -643,9 +644,11 @@ void startSettlingAnimation(View child, int state) {
} else {
throw new IllegalArgumentException("Illegal state argument: " + state);
}
setStateInternal(STATE_SETTLING);
if (mViewDragHelper.smoothSlideViewTo(child, child.getLeft(), top)) {
setStateInternal(STATE_SETTLING);
ViewCompat.postOnAnimation(child, new SettleRunnable(child, state));
} else {
setStateInternal(state);
}
}

Expand All @@ -661,7 +664,7 @@ public boolean tryCaptureView(View child, int pointerId) {
}
if (mState == STATE_EXPANDED && mActivePointerId == pointerId) {
View scroll = mNestedScrollingChildRef.get();
if (scroll != null && ViewCompat.canScrollVertically(scroll, -1)) {
if (scroll != null && scroll.canScrollVertically(-1)) {
// Let the content scroll up
return false;
}
Expand Down Expand Up @@ -715,11 +718,7 @@ public void onViewReleased(View releasedChild, float xvel, float yvel) {

@Override
public int clampViewPositionVertical(View child, int top, int dy) {
return constrain(top, mMinOffset, mHideable ? mParentHeight : mMaxOffset);
}

private int constrain(int amount, int low, int high) {
return amount < low ? low : (amount > high ? high : amount);
return MathUtils.clamp(top, mMinOffset, mHideable ? mParentHeight : mMaxOffset);
}

@Override
Expand Down Expand Up @@ -802,18 +801,22 @@ public void writeToParcel(Parcel out, int flags) {
out.writeInt(state);
}

public static final Creator<SavedState> CREATOR = ParcelableCompat.newCreator(
new ParcelableCompatCreatorCallbacks<SavedState>() {
@Override
public SavedState createFromParcel(Parcel in, ClassLoader loader) {
return new SavedState(in, loader);
}
public static final Creator<SavedState> CREATOR = new ClassLoaderCreator<SavedState>() {
@Override
public SavedState createFromParcel(Parcel in, ClassLoader loader) {
return new SavedState(in, loader);
}

@Override
public SavedState[] newArray(int size) {
return new SavedState[size];
}
});
@Override
public SavedState createFromParcel(Parcel in) {
return new SavedState(in, null);
}

@Override
public SavedState[] newArray(int size) {
return new SavedState[size];
}
};
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@
import android.support.v4.view.accessibility.AccessibilityNodeInfoCompat;
import android.support.v7.app.AppCompatDialog;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;

public final class ViewPagerBottomSheetDialog extends AppCompatDialog {
public class ViewPagerBottomSheetDialog extends AppCompatDialog {

private ViewPagerBottomSheetBehavior<FrameLayout> mBehavior;

Expand Down Expand Up @@ -52,8 +54,15 @@ public void setContentView(@LayoutRes int layoutResId) {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setLayout(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
Window window = getWindow();
if (window != null) {
if (Build.VERSION.SDK_INT >= 21) {
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
}
window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
}
}

@Override
Expand All @@ -77,6 +86,14 @@ public void setCancelable(boolean cancelable) {
}
}

@Override
protected void onStart() {
super.onStart();
if (mBehavior != null) {
mBehavior.setState(ViewPagerBottomSheetBehavior.STATE_COLLAPSED);
}
}

@Override
public void setCanceledOnTouchOutside(boolean cancel) {
super.setCanceledOnTouchOutside(cancel);
Expand All @@ -88,8 +105,10 @@ public void setCanceledOnTouchOutside(boolean cancel) {
}

private View wrapInBottomSheet(int layoutResId, View view, ViewGroup.LayoutParams params) {
final CoordinatorLayout coordinator = (CoordinatorLayout) View.inflate(getContext(),
final FrameLayout container = (FrameLayout) View.inflate(getContext(),
R.layout.design_view_pager_bottom_sheet_dialog, null);
final CoordinatorLayout coordinator =
(CoordinatorLayout) container.findViewById(R.id.coordinator);
if (layoutResId != 0 && view == null) {
view = getLayoutInflater().inflate(layoutResId, coordinator, false);
}
Expand Down Expand Up @@ -134,7 +153,14 @@ public boolean performAccessibilityAction(View host, int action, Bundle args) {
return super.performAccessibilityAction(host, action, args);
}
});
return coordinator;
bottomSheet.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent event) {
// Consume the event and prevent it from falling through
return true;
}
});
return container;
}

boolean shouldWindowCloseOnTouchOutside() {
Expand Down
33 changes: 22 additions & 11 deletions vpbs/src/main/res/layout/design_view_pager_bottom_sheet_dialog.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,37 @@
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
<FrameLayout
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"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">

<android.support.design.widget.CoordinatorLayout
android:id="@+id/coordinator"
android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_height="match_parent"
android:fitsSystemWindows="true">

<View
<View
android:id="@+id/touch_outside"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:importantForAccessibility="no"
android:soundEffectsEnabled="false"/>
android:soundEffectsEnabled="false"
tools:ignore="UnusedAttribute"/>

<FrameLayout
<FrameLayout
android:id="@+id/design_bottom_sheet"
style="?attr/bottomSheetStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|top"
android:clickable="true"
app:layout_behavior="@string/view_pager_bottom_sheet_behavior"
style="?attr/bottomSheetStyle"/>
app:layout_behavior="@string/view_pager_bottom_sheet_behavior"/>

</android.support.design.widget.CoordinatorLayout>

</android.support.design.widget.CoordinatorLayout>
</FrameLayout>

0 comments on commit 0230dd8

Please sign in to comment.