Android支持BottomSheetBehavior附加锚定状态

时间:2016-05-01 06:33:00

标签: android android-support-library android-design-library bottom-sheet

我一直在我的应用中使用AndroidSlidingUpPanel库。对于自23.1.1以来的Android设计支持库版本,这会破坏我的布局中的一些内容。由于最新版本引入了BottomSheetBehavior,我正在寻找替换AndroidSlidingUpPanel库并使用BottomSheetBehavior。但是,BottomSheetBehavior只有3个状态,隐藏,折叠和展开(以及2个中间状态拖动和稳定)。 AndroidSlidingUpPanel还具有锚定状态,这是面板在折叠和展开之间捕捉到的状态。我怎么能使用BottomSheetBehavior并获得这个额外的锚定状态?

Google的Google地图应用就是这种行为。

隐藏:

已折叠:

拖动(在折叠和锚定之间):

拖动(在锚定和展开之间):

扩展

当有位置的可选图像在锚定状态下向上滑动时,会出现一些视差效果。当完全展开时,位置名称将成为操作栏标题。我最终也有兴趣实现类似的东西。

我的第一直觉是锚定状态实际上是展开状态,面板上方有空白区域,地图仍然可见,是视图的透明部分。然后,在锚定状态和展开状态之间拖动只是滚动面板视图本身的内容。

这可以通过以下事实得到验证:在锚定状态下,您可以通过滑动面板上方的可见地图区域继续向上滚动面板。视图的这个不可见部分必须在从折叠状态向上滑动的同时扩展到其区域(如可选图像可见),因为在折叠状态下无法从地图向上滑动面板。我想我可以走这条路,但想知道那里是否有更好的方法。

2 个答案:

答案 0 :(得分:8)

BIG UPDATE 因为有关于同一主题的4或5个问题,但有不同的requeriments,我试图回答所有这些非礼貌的管理员删除/关闭他们让我为每个人创建一张票并更改它们以避免"复制粘贴"我将为您提供full answer的链接,您可以在其中找到有关如何获得Google地图等完整行为的所有说明。

回答你的问题

  

我如何使用BottomSheetBehavior并获得这个额外的锚定状态?

您可以通过以下步骤修改默认BottomSheetBehavior添加一个属性:

  1. 创建一个Java类并从CoordinatorLayout.Behavior<V>
  2. 扩展它
  3. 将粘贴代码从默认BottomSheetBehavior文件复制到新文件。
  4. 使用以下代码修改方法clampViewPositionVertical

    @覆盖 public int clampViewPositionVertical(View child,int top,int dy){     return constrain(top,mMinOffset,mHideable?mParentHeight:mMaxOffset); } int constrain(int amount,int low,int high){     返还金额&lt;低?低:(量>高?高:量); }

  5. 添加新状态

    public static final int STATE_ANCHOR_POINT = X;

  6. 修改下列方法:onLayoutChildonStopNestedScrollBottomSheetBehavior<V> from(V view)setState(可选)



  7. 我将添加修改后的方法和link to the example project

    public boolean onLayoutChild(CoordinatorLayout parent, V child, int layoutDirection) {
        // First let the parent lay it out
        if (mState != STATE_DRAGGING && mState != STATE_SETTLING) {
            if (ViewCompat.getFitsSystemWindows(parent) &&
                    !ViewCompat.getFitsSystemWindows(child)) {
                ViewCompat.setFitsSystemWindows(child, true);
            }
            parent.onLayoutChild(child, layoutDirection);
        }
        // Offset the bottom sheet
        mParentHeight = parent.getHeight();
        mMinOffset = Math.max(0, mParentHeight - child.getHeight());
        mMaxOffset = Math.max(mParentHeight - mPeekHeight, mMinOffset);
    
        //if (mState == STATE_EXPANDED) {
        //    ViewCompat.offsetTopAndBottom(child, mMinOffset);
        //} else if (mHideable && mState == STATE_HIDDEN...
        if (mState == STATE_ANCHOR_POINT) {
            ViewCompat.offsetTopAndBottom(child, mAnchorPoint);
        } else if (mState == STATE_EXPANDED) {
            ViewCompat.offsetTopAndBottom(child, mMinOffset);
        } else if (mHideable && mState == STATE_HIDDEN) {
            ViewCompat.offsetTopAndBottom(child, mParentHeight);
        } else if (mState == STATE_COLLAPSED) {
            ViewCompat.offsetTopAndBottom(child, mMaxOffset);
        }
        if (mViewDragHelper == null) {
            mViewDragHelper = ViewDragHelper.create(parent, mDragCallback);
        }
        mViewRef = new WeakReference<>(child);
        mNestedScrollingChildRef = new WeakReference<>(findScrollingChild(child));
        return true;
    }
    
    
    public void onStopNestedScroll(CoordinatorLayout coordinatorLayout, V child, View target) {
        if (child.getTop() == mMinOffset) {
            setStateInternal(STATE_EXPANDED);
            return;
        }
        if (target != mNestedScrollingChildRef.get() || !mNestedScrolled) {
            return;
        }
        int top;
        int targetState;
        if (mLastNestedScrollDy > 0) {
            //top = mMinOffset;
            //targetState = STATE_EXPANDED;
            int currentTop = child.getTop();
            if (currentTop > mAnchorPoint) {
                top = mAnchorPoint;
                targetState = STATE_ANCHOR_POINT;
            }
            else {
                top = mMinOffset;
                targetState = STATE_EXPANDED;
            }
        } else if (mHideable && shouldHide(child, getYVelocity())) {
            top = mParentHeight;
            targetState = STATE_HIDDEN;
        } else if (mLastNestedScrollDy == 0) {
            int currentTop = child.getTop();
            if (Math.abs(currentTop - mMinOffset) < Math.abs(currentTop - mMaxOffset)) {
                top = mMinOffset;
                targetState = STATE_EXPANDED;
            } else {
                top = mMaxOffset;
                targetState = STATE_COLLAPSED;
            }
        } else {
            //top = mMaxOffset;
            //targetState = STATE_COLLAPSED;
            int currentTop = child.getTop();
            if (currentTop > mAnchorPoint) {
                top = mMaxOffset;
                targetState = STATE_COLLAPSED;
            }
            else {
                top = mAnchorPoint;
                targetState = STATE_ANCHOR_POINT;
            }
        }
        if (mViewDragHelper.smoothSlideViewTo(child, child.getLeft(), top)) {
            setStateInternal(STATE_SETTLING);
            ViewCompat.postOnAnimation(child, new SettleRunnable(child, targetState));
        } else {
            setStateInternal(targetState);
        }
        mNestedScrolled = false;
    }
    
    public final void setState(@State int state) {
        if (state == mState) {
            return;
        }
        if (mViewRef == null) {
            // The view is not laid out yet; modify mState and let onLayoutChild handle it later
            /**
             * New behavior (added: state == STATE_ANCHOR_POINT ||)
             */
            if (state == STATE_COLLAPSED || state == STATE_EXPANDED ||
                    state == STATE_ANCHOR_POINT ||
                    (mHideable && state == STATE_HIDDEN)) {
                mState = state;
            }
            return;
        }
        V child = mViewRef.get();
        if (child == null) {
            return;
        }
        int top;
        if (state == STATE_COLLAPSED) {
            top = mMaxOffset;
        } else if (state == STATE_ANCHOR_POINT) {
            top = mAnchorPoint;
        } else if (state == STATE_EXPANDED) {
            top = mMinOffset;
        } else if (mHideable && state == STATE_HIDDEN) {
            top = mParentHeight;
        } else {
            throw new IllegalArgumentException("Illegal state argument: " + state);
        }
        setStateInternal(STATE_SETTLING);
        if (mViewDragHelper.smoothSlideViewTo(child, child.getLeft(), top)) {
            ViewCompat.postOnAnimation(child, new SettleRunnable(child, state));
        }
    }
    
    
    public static <V extends View> BottomSheetBehaviorGoogleMapsLike<V> from(V view) {
        ViewGroup.LayoutParams params = view.getLayoutParams();
        if (!(params instanceof CoordinatorLayout.LayoutParams)) {
            throw new IllegalArgumentException("The view is not a child of CoordinatorLayout");
        }
        CoordinatorLayout.Behavior behavior = ((CoordinatorLayout.LayoutParams) params)
                .getBehavior();
        if (!(behavior instanceof BottomSheetBehaviorGoogleMapsLike)) {
            throw new IllegalArgumentException(
                    "The view is not associated with BottomSheetBehaviorGoogleMapsLike");
        }
        return (BottomSheetBehaviorGoogleMapsLike<V>) behavior;
    }
    



    您甚至可以使用behavior.setBottomSheetCallback(new BottomSheetBehaviorGoogleMapsLike.BottomSheetCallback() {....

    进行回调

    以下是它的外观:
    [CustomBottomSheetBehavior]

答案 1 :(得分:-1)

使用锚 FloatingActionButton

更改 BottomSheetBehavior 更改状态有一些简单的方法
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

  <android.support.v7.widget.CardView
    android:id="@+id/bottom_sheet"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:behavior_hideable="false"
    app:behavior_peekHeight="44dp"
    app:cardCornerRadius="0dp"
    app:cardElevation="5dp"
    app:layout_behavior="@string/bottom_sheet_behavior">

</android.support.v7.widget.CardView>

<android.support.design.widget.FloatingActionButton
    android:id="@+id/request_show_fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    android:src="@drawable/fab_requests"
    app:layout_anchor="@id/bottom_sheet"
    app:layout_anchorGravity="top|end" />

现在您可以更改状态点击 FloatingActionButton

bottomSheetBehavior = BottomSheetBehavior.from(bottom_sheet);

    request_show_fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (bottomSheetBehavior.getState() == BottomSheetBehavior.STATE_COLLAPSED) {
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
            } else {
                bottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);

            }
        }
    });