gpt4 book ai didi

android - 如何使 Bottom Sheet (BottomSheetBehavior)可扩展到任意位置?

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:24:31 53 4
gpt4 key购买 nike

我有 Bottom Sheet ,我想改变它的行为,让它像在谷歌地图应用程序的主屏幕上一样工作,在那里你可以把它展开到任何位置并把它留在那里,它不会自动粘在底部或顶部。这是我的 Bottom Sheet 布局:

<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<com.google.android.gms.maps.MapView
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

<View
android:id="@+id/shadow"
android:layout_width="match_parent"
android:layout_height="16dp"
android:background="@drawable/shape_gradient_top_shadow"
app:layout_anchor="@+id/map_bottom_sheet" />


<LinearLayout
android:id="@+id/map_bottom_sheet"
android:layout_width="match_parent"
android:layout_height="300dp"
android:fillViewport="false"
android:orientation="vertical"
app:behavior_peekHeight="50dp"
android:background="@color/lightGray"
app:layout_behavior="android.support.design.widget.BottomSheetBehavior">

<include layout="@layout/bottom_sheet_top_buttons"/>
<android.support.v4.widget.NestedScrollView
android:id="@+id/bottom_sheet_content_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/lightGray"/>

</LinearLayout>

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

本质上我需要的是在拖动结束时消除 STATE_EXPANDEDSTATE_COLLAPSED 状态的强制。

这是我试图实现的目标的直观解释:


如您所见, Bottom Sheet 不会自动锚定到顶部或底部,而是停留在它离开的任何位置。

最佳答案

android.support.design.widget.BottomSheetBehavior 复制代码来制作您自己的自定义行为。然后修改onViewReleased()方法,该方法负责拖动结束后sheet的移动。除了现有状态之外,您还必须引入一个新状态 - 该状态有助于恢复位置并让其他人知道您的工作表目前使用 getState() 处于哪种状态。

@Override
public void onViewReleased(View releasedChild, float xVel, float yVel) {
int top;
@State int targetState;

// Use the position where the drag ended as new top
top = releasedChild.getTop();

// You have to manage the states here, too (introduce a new one)
targetState = STATE_ANCHORED;

if (mViewDragHelper.settleCapturedViewAt(releasedChild.getLeft(), top)) {
setStateInternal(STATE_SETTLING);
ViewCompat.postOnAnimation(releasedChild, new SettleRunnable(releasedChild, targetState));

} else {
setStateInternal(targetState);
}
}

关于android - 如何使 Bottom Sheet (BottomSheetBehavior)可扩展到任意位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44274457/

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