- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我一直在使用 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
来实现通过以下步骤再添加一项统计数据:
CoordinatorLayout.Behavior<V>
扩展它BottomSheetBehavior
复制粘贴代码提交给你的新文件。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);
}
添加一个新状态
public static final int STATE_ANCHOR_POINT = X;
修改下一个方法: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() {....
的回调
这是它的样子
[ ]
关于Android 支持 BottomSheetBehavior 附加锚定状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36963798/
我正在实现谱聚类算法,我必须确保矩阵(拉普拉斯矩阵)是半正定矩阵。 检查矩阵是否为正定矩阵 (PD) 就足够了,因为可以在特征值中看到“半”部分。矩阵非常大(nxn,其中 n 是几千的数量级)所以特征
我是一名优秀的程序员,十分优秀!