【问题标题】:BottomSheet is jumping on button clicksBottomSheet 正在点击按钮
【发布时间】:2018-12-26 10:20:28
【问题描述】:

我有一个 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
    }

   
}

【问题讨论】:

  • @PrateekPhoenix ,你找到解决办法了吗?
  • 不,不是,我们取消了该设计,因为它是组件的问题,此后没有任何更新。

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


【解决方案1】:

看来google工程师给出了正确的答案

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

这对我的案子有帮助

【讨论】:

    【解决方案2】:

    在我看来这可能是BottomSheetBehavior 中的错误?似乎工作表的高度没有正确保存或恢复。按下按钮后,会再次发生改变高度的布局。你能在https://issuetracker.google.com/issues/new?component=439535提交一个错误吗?

    【讨论】:

    • 嗨,卡梅伦,感谢您的回复....刚刚报告了!这是链接 - issuetracker.google.com/issues/122017052
    • @CameronKetcham 我也面临同样的问题,即使我设置了固定的高度尺寸。
    猜你喜欢
    • 2023-02-25
    • 2020-06-24
    • 1970-01-01
    • 2011-07-08
    • 2015-02-22
    • 2019-09-28
    • 1970-01-01
    • 2017-01-27
    • 1970-01-01
    相关资源
    最近更新 更多