BottomSheet跳上按钮单击

时间:2018-12-26 10:20:29

标签: android kotlin bottom-sheet material-components material-components-android

我有一个 BottomSheet ,其中装有产品详细信息卡。问题是,当底部工作表处于 Expanded (展开)状态时,当我单击产品详细信息上的 + -按钮时,它跳下来

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

我已经附上了GIF以显示实际情况

enter image description here

这是代码

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
    }

   
}

2 个答案:

答案 0 :(得分:0)

在我看来,这可能是BottomSheetBehavior中的错误?似乎无法正确保存或还原图纸的高度。按下按钮后,会再次发生更改高度的布局。您可以在https://issuetracker.google.com/issues/new?component=439535

处提交错误吗?

答案 1 :(得分:0)

似乎Google工程师给出了正确答案

  

似乎正在发生某些事情,因为您正在设置   带有android:layout_gravity =“ bottom”的视图   BottomSheet行为。您应该删除该行。

对我的案子有帮助