gpt4 book ai didi

Android 支持 BottomSheetBehavior 附加锚定状态

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:02:32 25 4
gpt4 key购买 nike

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

例如,Google 的 map 应用程序就有这种行为。

隐藏:

折叠:

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

锚定:

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

展开:

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

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

事实证明,在锚定状态下,您可以通过滑动面板上方的可见 map 区域继续向上滚动面板。但是,在从折叠状态向上滑动时, View 的这个不可见部分必须扩展到它的区域(正如可选图像可见的那样),因为在折叠状态下无法将面板从 map 向上滑动。我想我可以走这条路,但想看看是否有更好的方法。

最佳答案

重大更新因为有大约 4 或 5 个关于同一主题但要求不同的问题,我试图回答所有这些问题。一个不礼貌的管理员删除/关闭了它们,让我为每个人创建一张票并更改它们以避免“复制粘贴”。我会给你一个链接到 full answer在那里你可以找到关于如何获得像谷歌地图这样的完整行为的所有解释。


回答你的问题

How could I use BottomSheetBehavior and get this additional anchored state?

你可以通过修改默认的BottomSheetBehavior来实现通过以下步骤再添加一项统计数据:

  1. 创建一个 Java 类并从 CoordinatorLayout.Behavior<V> 扩展它
  2. 从默认BottomSheetBehavior 复制粘贴代码提交给你的新文件。
  3. 修改方法clampViewPositionVertical使用以下代码:
    @Override
public int clampViewPositionVertical(View child, int top, int dy) {
return constrain(top, mMinOffset, mHideable ? mParentHeight : mMaxOffset);
}

int constrain(int amount, int low, int high) {
return amount < low ? low : (amount > high ? high : amount);
}
  1. 添加一个新状态

    public static final int STATE_ANCHOR_POINT = X;

  2. 修改下一个方法:onLayoutChild , onStopNestedScroll , BottomSheetBehavior<V> from(V view)setState (可选)



我将添加那些修改过的方法和一个 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 ]

关于Android 支持 BottomSheetBehavior 附加锚定状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36963798/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com