将底张卡片视图转换为顶张卡片视图

时间:2020-07-19 00:57:31

标签: android android-layout kotlin android-cardview android-coordinatorlayout

我能够创建底页卡片视图。我正在尝试将我拥有的内容转换为顶层卡片视图(当用户单击按钮时,卡片视图应从顶部而不是底部出现)。感谢您的协助。

SheetBehaviorActivity.kt

class SheetBehaviourActivity : AppCompatActivity() {

    lateinit var sheetBehavior: BottomSheetBehavior<*>

    override fun onCreate(savedInstanceState: Bundle?) {

        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val btn: Button = this.findViewById(R.id.testing_bottom_sheet_btn)

        btn.setOnClickListener {
            if (sheetBehavior.state != BottomSheetBehavior.STATE_EXPANDED) {
                sheetBehavior.state = BottomSheetBehavior.STATE_EXPANDED
                btn.text = "Close sheet"
            } else {
                sheetBehavior.state = BottomSheetBehavior.STATE_HIDDEN
                btn.text = "Expand sheet"
            }
        }

        val sheetContainer = findViewById<CardView>(R.id.bottom_sheet_container)

        sheetBehavior = BottomSheetBehavior.from(sheetContainer)
        sheetBehavior.isHideable = true
        sheetBehavior.state = BottomSheetBehavior.STATE_HIDDEN

    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.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=".activities.SheetBehaviourActivity">

    <Button
        android:id="@+id/testing_bottom_sheet_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Expand Sheet" />

    <android.support.v7.widget.CardView
        android:id="@+id/bottom_sheet_container"
        android:layout_width="match_parent"
        app:cardCornerRadius="20dp"
        app:cardBackgroundColor="@android:color/holo_blue_light"
        app:layout_behavior="android.support.design.widget.BottomSheetBehavior"
        android:layout_height="300dp"/>

</android.support.design.widget.CoordinatorLayout>

1 个答案:

答案 0 :(得分:1)

对于我来说,这是TopSheetBehavior实现topSheetLayout的正常工作

这是您的操作方式:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.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=".activities.SheetBehaviourActivity">

    <Button
        android:id="@+id/testing_bottom_sheet_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Expand Sheet" />

    <android.support.v7.widget.CardView
        android:id="@+id/bottom_sheet_container"
        android:layout_width="match_parent"
        app:cardCornerRadius="20dp"
        app:cardBackgroundColor="@android:color/holo_blue_light"
        app:behavior_hideable="true"
        app:behavior_peekHeight="56dp"
        app:layout_behavior="your.package.components.TopSheetBehavior"/>

</android.support.design.widget.CoordinatorLayout>

TopSheetBehavior只是替换了行为参数