implementation 'com.google.android.material:material:1.2.1'
패키지 내에서
오른쪽 클릭 후 New → Fragment → Fragment(Blank)
상속을 Fragment()에서 BottomSheetDialogFragment()로 변경, 다른 코드 지우고 onCreateView 남겨둠
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".BottomSheet"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/hello_blank_fragment"
android:textAlignment="center" />
<Button
android:id="@+id/deleteButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/text_delete"/>
<Button
android:id="@+id/cancelButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/text_cancel"/>
</LinearLayout>
package org.inu.events
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
import androidx.fragment.app.viewModels
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
import org.inu.events.databinding.FragmentBottomSheetBinding
import org.inu.events.viewmodel.CommentViewModel
private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"
/**
* A simple [Fragment] subclass.
* Use the [BottomSheet.newInstance] factory method to
* create an instance of this fragment.
*/
class BottomSheet : BottomSheetDialogFragment(), View.OnClickListener {
private lateinit var binding: FragmentBottomSheetBinding
private val commentViewModel: CommentViewModel by activityViewModels()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
binding =
DataBindingUtil.inflate(inflater, R.layout.fragment_bottom_sheet, container, false)
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
initButton()
}
private fun initButton() {
binding.deleteButton.setOnClickListener(this)
binding.cancelButton.setOnClickListener(this)
}
override fun onClick(v: View?) {
when (v?.id) {
R.id.deleteButton -> {
Log.i("Button", "delete click")
}
R.id.cancelButton -> {
Log.i("Button", "cancel click")
dismiss()
}
}
}
}