gpt4 book ai didi

java - 如何通过在Android持久 Bottom Sheet 中向上滑动从隐藏状态进入折叠状态?

转载 作者:行者123 更新时间:2023-12-01 21:11:53 24 4
gpt4 key购买 nike

将 Bottom Sheet 从折叠状态拖动到隐藏状态后,从屏幕底部向上滑动不会将 Bottom Sheet 打开到折叠状态,这在谷歌地图的 Bottom Sheet 中是可行的。像这样:

如果这不是库功能,如何实现这一点?

activity_main.xml 是:

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<!-- include main content -->
<include layout="@layout/content_main" />

<!-- Adding bottom sheet after main content -->
<include layout="@layout/bottom_sheet" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

bottom_sheet.xml 是:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/bottom_sheet"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:orientation="vertical"
android:padding="12dp"
app:behavior_hideable="true"
app:behavior_peekHeight="100dp"
app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="96dp"
android:orientation="horizontal">

<ImageView
android:layout_width="84dp"
android:layout_height="match_parent"
android:padding="8dp"
android:src="@drawable/ic_launcher_background" />

<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="5"
android:orientation="vertical">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Dandelion Chocolate"
android:textColor="#000000"
android:textSize="24sp"
android:textStyle="bold" />

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="6dp"
android:orientation="horizontal">

<!-- <android.support.v7.widget.AppCompatRatingBar
style="@style/Base.Widget.AppCompat.RatingBar.Small"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="5"
android:rating="4" />-->

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="4.7"
android:textColor="@color/colorAccent" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:text="(51)"
android:textColor="@color/colorAccent" />

</LinearLayout>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="6dp"
android:text="12 min away"
/>

</LinearLayout>

</LinearLayout>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="6dp"
android:drawablePadding="16dp"
android:text="740 Valencia St, San Fracisco, CA"
/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="6dp"

android:drawablePadding="16dp"
android:text="(415) 349-0942"
/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="6dp"
android:drawablePadding="16dp"
android:text="Wed, 10 AM - 9 PM"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:background="@color/colorAccent"
android:text="PROCEED PAYMENT"
android:textColor="#fff" />

</LinearLayout>

content_main.xml 是:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#DEDEDE"

app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/activity_main">

</LinearLayout>

Activity 代码:

private BottomSheetBehavior sheetBehavior;
private LinearLayout bottom_sheet;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

bottom_sheet = findViewById(R.id.bottom_sheet);
sheetBehavior = BottomSheetBehavior.from(bottom_sheet);



sheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
@Override
public void onStateChanged(@NonNull View view, int newState) {
switch (newState) {
case BottomSheetBehavior.STATE_HIDDEN:
break;
case BottomSheetBehavior.STATE_EXPANDED: {
Log.d("Tag", "Close Sheet");
}
break;
case BottomSheetBehavior.STATE_COLLAPSED: {
Log.d("Tag", "Expand Sheet");
}
break;
case BottomSheetBehavior.STATE_DRAGGING:
break;
case BottomSheetBehavior.STATE_SETTLING:
break;
}
}

@Override
public void onSlide(@NonNull View view, float v) {

}
});

最佳答案

使用 GestureDetector 检测向上滑动手势 ( documentation )。例如,您可以像这样实现 onFling 回调:

    @Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
boolean result = false;
try {
float diffY = e2.getY() - e1.getY();
float diffX = e2.getX() - e1.getX();
if (Math.abs(diffX) > Math.abs(diffY)) {
// Horizontal swipe occurred
}
else if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
// Vertical swipe occurred
if (diffY > 0) {
// Swipe to bottom
} else {
onSwipeTop();
}
result = true;
}
} catch (Exception exception) {
exception.printStackTrace();
}
return result;
}

private void onSwipeTop() {
sheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED)
}

关于java - 如何通过在Android持久 Bottom Sheet 中向上滑动从隐藏状态进入折叠状态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58899864/

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