From 0230dd863583bbc707ef3af9462d96d2d724ee91 Mon Sep 17 00:00:00 2001 From: Christian Langer Date: Thu, 22 Mar 2018 21:52:31 +0100 Subject: [PATCH] port changes from support lib 27.1.0 --- .../vpbs/ViewPagerBottomSheetBehavior.java | 73 ++++++++++--------- .../vpbs/ViewPagerBottomSheetDialog.java | 36 +++++++-- .../design_view_pager_bottom_sheet_dialog.xml | 33 ++++++--- 3 files changed, 91 insertions(+), 51 deletions(-) diff --git a/vpbs/src/main/java/biz/laenger/android/vpbs/ViewPagerBottomSheetBehavior.java b/vpbs/src/main/java/biz/laenger/android/vpbs/ViewPagerBottomSheetBehavior.java index ef8c7d5..bf8d7a8 100644 --- a/vpbs/src/main/java/biz/laenger/android/vpbs/ViewPagerBottomSheetBehavior.java +++ b/vpbs/src/main/java/biz/laenger/android/vpbs/ViewPagerBottomSheetBehavior.java @@ -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; @@ -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; @@ -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 @@ -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(); @@ -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; @@ -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(); @@ -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); @@ -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; @@ -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() { @@ -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) { @@ -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) { @@ -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); } } @@ -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; } @@ -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 @@ -802,18 +801,22 @@ public void writeToParcel(Parcel out, int flags) { out.writeInt(state); } - public static final Creator CREATOR = ParcelableCompat.newCreator( - new ParcelableCompatCreatorCallbacks() { - @Override - public SavedState createFromParcel(Parcel in, ClassLoader loader) { - return new SavedState(in, loader); - } + public static final Creator CREATOR = new ClassLoaderCreator() { + @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]; + } + }; } /** diff --git a/vpbs/src/main/java/biz/laenger/android/vpbs/ViewPagerBottomSheetDialog.java b/vpbs/src/main/java/biz/laenger/android/vpbs/ViewPagerBottomSheetDialog.java index 043af86..f28242e 100644 --- a/vpbs/src/main/java/biz/laenger/android/vpbs/ViewPagerBottomSheetDialog.java +++ b/vpbs/src/main/java/biz/laenger/android/vpbs/ViewPagerBottomSheetDialog.java @@ -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 mBehavior; @@ -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 @@ -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); @@ -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); } @@ -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() { diff --git a/vpbs/src/main/res/layout/design_view_pager_bottom_sheet_dialog.xml b/vpbs/src/main/res/layout/design_view_pager_bottom_sheet_dialog.xml index fada713..9ae0969 100644 --- a/vpbs/src/main/res/layout/design_view_pager_bottom_sheet_dialog.xml +++ b/vpbs/src/main/res/layout/design_view_pager_bottom_sheet_dialog.xml @@ -14,26 +14,37 @@ ~ See the License for the specific language governing permissions and ~ limitations under the License. --> - + + + android:layout_height="match_parent" + android:fitsSystemWindows="true"> - + android:soundEffectsEnabled="false" + tools:ignore="UnusedAttribute"/> - + app:layout_behavior="@string/view_pager_bottom_sheet_behavior"/> + + - +