[안드로이드/Android/Kotlin] bottom sheet 띄우고 버튼 선택하기

SooYeon Yeon·2022년 2월 24일
0

안드로이드/Android

목록 보기
18/25

[안드로이드/Android/Kotlin] bottom sheet 띄우고 버튼 선택하기

build.gradle dependency 라이브러리 추가

implementation 'com.google.android.material:material:1.2.1'

패키지 내에서

오른쪽 클릭 후 New → Fragment → Fragment(Blank)

상속을 Fragment()에서 BottomSheetDialogFragment()로 변경, 다른 코드 지우고 onCreateView 남겨둠

xml 코드

<?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>

BottomSheet.kt

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()
            }
        }
    }

}
  1. 데이터 바인딩을 이용해 inflate해준다
  2. setOnClickListener 등록
  3. onClick 메소드로 각 버튼을 클릭했을 때 호출할 코드를 작성한다.

0개의 댓글