5
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

BottomSheetDialogFragmentの忘備録

Posted at

こんにちは、m_saekiです。

久しぶりの投稿です!

Material Designになってから2年くらいかな?
BottomSheetの記事がかなり少ないので投稿しようと思います!
自分の忘備録としても投稿しているので指摘などがあればお手柔らかにお願いしますmm
Modal Bottom Sheetsって何ってかたはこの辺り見てください。

Enviroment

  • Android Studio 3.5 Canary5
  • kotlin ver: 1.3.21

実装

app/build.gradle

dataBindingしているコードになるので気をつけてくださいmm

dataBinding {
    enabled = true
}

fragment_bottom_sheet.xml

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto">

    <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:behavior_hideable="true"
            app:behavior_peekHeight="56dp">

        <LinearLayout
                android:id="@+id/shareLayout"
                android:layout_width="match_parent"
                android:layout_height="56dp"
                android:orientation="horizontal"
                android:background="?android:attr/selectableItemBackground"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintEnd_toEndOf="parent">

            <ImageView
                    android:layout_width="32dp"
                    android:layout_height="32dp"
                    android:layout_marginStart="16dp"
                    android:layout_gravity="center_vertical"
                    android:src="@drawable/ic_????"/>

            <TextView
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="Share"
                    android:textColor="#000000"
                    android:layout_gravity="center_vertical"
                    android:layout_marginStart="16dp"
                    android:layout_marginEnd="16dp"/>

        </LinearLayout>

        <LinearLayout
                android:id="@+id/getLinkLayout"
                android:layout_width="match_parent"
                android:layout_height="56dp"
                android:orientation="horizontal"
                android:background="?android:attr/selectableItemBackground"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/shareLayout">

            <ImageView
                    android:layout_width="32dp"
                    android:layout_height="32dp"
                    android:layout_marginStart="16dp"
                    android:layout_gravity="center_vertical"
                    android:src="@drawable/ic_????"/>

            <TextView
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="Get Link"
                    android:textColor="#000000"
                    android:layout_gravity="center_vertical"
                    android:layout_marginStart="16dp"
                    android:layout_marginEnd="16dp"/>

        </LinearLayout>

    </androidx.constraintlayout.widget.ConstraintLayout>

</layout>

CustomBottomSheetFragment.kt

import com.google.android.material.bottomsheet.BottomSheetDialogFragment

class CustomBottomSheetDialogFragment: BottomSheetDialogFragment() {

    private lateinit var binding: FragmentBottomSheetBinding

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_bottom_sheet, container, false)
        return binding.root
    }

    companion object {
        fun newInstance(): CustomBottomSheetDialogFragment {
            val args = Bundle()
            val fragment = CustomBottomSheetDialogFragment()
            fragment.arguments = args
            return fragment
        }
    }
}

MainActivity.kt

MainActivityの使用する箇所に実装する

val dialog = CustomBottomSheetDialogFragment.newInstance()
dialog.show(supportFragmentManager, dialog.tag)

思ったより簡単に実装できるので皆さんもBottomSheetFragmentに挑戦してはどうでしょうか??

5
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?