我正在为 Wear OS 开发一个物联网应用程序,我想用搜索栏设置灯光的亮度。但是,从左向右移动搜索栏会导致 Fragment 消失,这对用户体验不利。我尝试使用 SwipeDismissFrameLayout 来避免这种情况,但它不起作用。有什么建议吗?我想重现 Google Home 应用程序的行为。我必须使用 Android View,我不能使用 compose。
这是包含 RecyclerView 和设备列表的片段
<?xml version="1.0" encoding="utf-8"?>
<androidx.wear.widget.SwipeDismissFrameLayout xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/dismiss_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/constraint_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.wear.widget.WearableRecyclerView
android:id="@+id/light_list"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:itemCount="10"
tools:listitem="@layout/item_device" />
</androidx.wear.widget.SwipeDismissFrameLayout>
这是我用来显示项目的布局:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/light_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginVertical="5dp"
android:orientation="vertical">
<SeekBar
android:elevation="2dp"
android:id="@+id/brightness_seekbar"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:maxHeight="@dimen/device_item_height"
android:minHeight="@dimen/device_item_height"
android:progressDrawable="@drawable/seekbar_drawable"
android:splitTrack="false"
android:thumbTint="@android:color/transparent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/light_checkbox" />
<CheckBox
android:id="@+id/light_checkbox"
android:layout_width="0dp"
android:layout_height="0dp"
android:button="@drawable/ic_lights"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
我尝试使用
setDismissMinDragWidthRatio(0)
降低滑动关闭手势的灵敏度,但没有任何改变。我也尝试过设置setSwipable(false)
,但同样,没有任何改变。最后,我尝试在 ConstraintLayout 内插入一个随机的 HorizontalView,从左向右滚动水平视图内容并没有触发滑动关闭手势。
在values/themes.xml中创建一个主题,像这样
<style name="DisableSwipeBack" parent="@android:style/Theme.DeviceDefault">
<item name="android:windowSwipeToDismiss">false</item>
</style>
或者您可以将 windowSwipeToDismiss 添加到当前主题...
然后将其设置为您的活动标签,就像这样
android:theme="@style/DisableSwipeBack"
完成,该活动将不再向后滑动...如果您禁用向后滑动,我建议添加一个关闭/后退按钮,因为用户可以将手表后退按钮设置为其他功能