본문 바로가기
프로그래밍/App 개발

[Android] File templates (BottomSheet)

by 엽기토기 2023. 11. 2.
반응형

 

https://dvlv.tistory.com/153

 

[Android] File Templates (Dialog)

2초만에 custom dialog 만드는 법 제가 쓰는 File Templates 를 소개합니다. 위치는 여기입니다. '+' 눌러서, 원하는 template 이름을 입력합니다. 그리고 이렇게 적어줍시다. 그리고 template 코드 적는 란에

dvlv.tistory.com

2초만에 BottomSheet 바텀시트 구현하기

 

전 게시글에 이어, BottomSheet 도 공유합니다.

 

.kt

 

package ${PACKAGE_NAME};

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.FragmentManager
import com.dreamfora.dreamfora.DreamforaApplication.Companion.getPreference
import com.dreamfora.dreamfora.DreamforaApplication.Companion.setPreference
import com.dreamfora.dreamfora.databinding.${NAME}BottomSheetBinding
import com.dreamfora.dreamfora.global.onThrottleClick
import com.google.android.material.bottomsheet.BottomSheetDialogFragment

#parse("File Header.java")
class ${NAME}BottomSheet : BottomSheetDialogFragment() {
    private lateinit var binding: ${NAME}BottomSheetBinding

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        binding = ${NAME}BottomSheetBinding.inflate(inflater, container, false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        
        
    }

    interface OnButtonClickListener {
        fun onButtonClicked()
    }

    fun setButtonClickListener(buttonClickListener: OnButtonClickListener) {
        this.buttonClickListener = buttonClickListener
    }

    private var buttonClickListener: OnButtonClickListener? = null

    companion object {
        private const val bottomSheetName = "${NAME}BottomSheet"

        private fun newInstance(buttonClickListener: OnButtonClickListener) = ${NAME}BottomSheet().apply {
            setButtonClickListener(buttonClickListener)
        }

        fun show(fragmentManager: FragmentManager, buttonClickListener: OnButtonClickListener) {
            if (fragmentManager.findFragmentByTag(bottomSheetName) != null) { return }
            fragmentManager.beginTransaction().add(newInstance(buttonClickListener), bottomSheetName).commitAllowingStateLoss()
        }
    }
}

 

 

.xml

File name

#set( $LAYOUT_FILE = $PACKAGE_NAME.replaceAll("\.", "/").replaceAll("\w+","\.\.").concat("/../res/layout/").concat($NAME.replaceAll("([a-z])([A-Z]+)", "$1_$2").toLowerCase().concat("_bottom_sheet")) )${LAYOUT_FILE}

 

 

<?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:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="40dp"
    android:paddingBottom="45dp">

</androidx.constraintlayout.widget.ConstraintLayout>

 

 

반응형