首页 > 解决方案 > BottomSheet 正在点击按钮

问题描述

我有一个包含产品详细信息卡的BottomSheet 。问题是,当我在底部工作表处于展开状态时单击产品详细信息上的+-按钮时,它会向下跳

当它向下并且我单击它不会跳转的按钮时,它只会在它处于展开(完全向上)状态时发生

我附上了一个 GIF 来显示到底发生了什么

在此处输入图像描述

这是代码

scan_sheet.xml

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/bottom_sheet"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom"
    android:animateLayoutChanges="false"
    android:background="@drawable/bottom_sheet_dialog_fragment"
    android:orientation="vertical"
    app:behavior_hideable="true"
    app:behavior_peekHeight="100dp"
    app:layout_behavior="studio.monotype.storedemo.BottomSheetBehavior">

    <include
        layout="@layout/hero_item"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="32dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <View
        android:id="@+id/divider_view"
        android:layout_width="match_parent"
        android:layout_height="4dp"
        android:layout_marginStart="24dp"
        android:layout_marginTop="44dp"
        android:layout_marginEnd="24dp"
        android:background="@color/colorPrimary"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/hero_item" />

    <include
        layout="@layout/related_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="24dp"
        android:layout_marginBottom="16dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/divider_view"
        tools:layout_editor_absoluteX="0dp" />

</androidx.constraintlayout.widget.ConstraintLayout>

ScanActivity.kt (简化为仅显示必要的内容)

class ScanActivity : AppCompatActivity() {


    private lateinit var bottomSheet: BottomSheetBehavior<*>
 

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_scan)

        setupBottomSheet()
        
        showSheet()
    }

  


    private fun setupBottomSheet() {
        bottomSheet = BottomSheetBehavior.from(bottom_sheet)
        bottomSheet.isHideable = true
        bottomSheet.skipCollapsed= true
        bottomSheet.isDraggable = true
        bottomSheet.state = BottomSheetBehavior.STATE_HIDDEN
        bottomSheet.addBottomSheetCallback(object : BottomSheetBehavior.BottomSheetCallback {
            override fun onSlide(bottomSheet: View, slideOffset: Float) {
            }

            @SuppressLint("SwitchIntDef")
            override fun onStateChanged(sheet: View, newState: Int) {
                when (newState) {
                    BottomSheetBehavior.STATE_HIDDEN -> {
                        codeScanner.startPreview()
                    }
                }
            }

        })
        
        
         plus_btn.setOnClickListener {
            var qty= qty_tv.text.toString().toInt()
            qty++
            qty_tv.text =qty.toString()
        }

        minus_btn.setOnClickListener {
            var qty= qty_tv.text.toString().toInt()
            if(qty!=0)
            {
                qty--
            }
            qty_tv.text =qty.toString()
        }


    }

    private fun showSheet() {
        bottomSheet.state = BottomSheetBehavior.STATE_EXPANDED
    }

   
}

标签: androidkotlinbottom-sheetmaterial-componentsmaterial-components-android

解决方案


谷歌工程师似乎给出了正确的答案

似乎正在发生一些事情,因为您正在使用 BottomSheetBehavior 在视图上设置 android:layout_gravity="bottom"。您应该删除该行。

这对我的案子有帮助


推荐阅读