CustomView

나고수·2022년 5월 6일
0

1일1공부

목록 보기
37/67
//custom_item_menu.xml - 커스텀 뷰 
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">

    <data>

    </data>

    <androidx.appcompat.widget.LinearLayoutCompat
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="25dp"
            android:layout_marginEnd="25dp">

            <androidx.appcompat.widget.AppCompatTextView
                android:id="@+id/tvMenu"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="4"
                android:textColor="@color/black"
                android:textSize="15sp"
                android:textStyle="bold"
                tools:text="타코야키 8알(기본맛, 매운맛)" />


            <androidx.appcompat.widget.AppCompatTextView
                android:id="@+id/tvPrice"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="end"
                android:textColor="@color/textGrey"
                android:textSize="15sp"
                tools:text="3,000원" />
        </TableRow>
    </androidx.appcompat.widget.LinearLayoutCompat>
</layout>
//values>attrs.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="MenuCommon">
        <attr name="tvMenu" format="string" /> //reference 는 strings.xml 에 있는 값 참조할 수 있음.
        <attr name="tvPrice" format="string" />
    </declare-styleable>
</resources>
//MenuCommon.kt

class MenuCommon : LinearLayoutCompat {
    private lateinit var binding: ItemMenuBinding
    private lateinit var tvMenu: AppCompatTextView
    private lateinit var tvPrice: AppCompatTextView

    constructor(context: Context) : super(context) {
        initView()
    }

    constructor(context: Context, attributeSet: AttributeSet) : super(context, attributeSet) { //AttributeSet 사용
        initView()
        getAttrs(attributeSet)
    }

    constructor(context: Context, attributeSet: AttributeSet, defStyleAttr: Int) : super( //AttributeSet , 기본값 사용
        context,
        attributeSet,
        defStyleAttr
    ) {
        initView()
        getAttrs(attributeSet, defStyleAttr)
    }

    private fun initView() {
        val inflater = LayoutInflater.from(context)
        binding = ItemMenuBinding.inflate(inflater, this, false)
        tvMenu = binding.tvMenu
        tvPrice = binding.tvPrice
        addView(binding.root)
    }

    private fun getAttrs(attrs: AttributeSet) {
        val typedArray = context.obtainStyledAttributes(attrs, R.styleable.MenuCommon) //AttributeSet 에서 MenuCommon에 해당하는 declare-styleable 가져오기
        setTypeArray(typedArray)
    }

    private fun getAttrs(attrs: AttributeSet, defStyleAttr: Int) {
        val typedArray = context.obtainStyledAttributes(attrs, R.styleable.MenuCommon, defStyleAttr, 0)
        setTypeArray(typedArray)
    }

    private fun setTypeArray(typedArray: TypedArray) {
        val menu = typedArray.getString(R.styleable.MenuCommon_tvMenu)
        val price = typedArray.getString(R.styleable.MenuCommon_tvPrice)

        this.tvMenu.text = menu ?: ""
        this.tvPrice.text = price ?: ""
//        val drawable = typedArray.getDrawable(R.styleable.MenuCommon_imageView)
//        binding.imageView.isGone = drawable == 0 //defStyleAttr 을 활용해서 view Visible 관리

        typedArray.recycle()//TypedArray를 캐싱해 재활용함. 이 함수 호출 이후로는 typeArray 변경 x
    }

//    fun setDrawable(resId: Int) {
//        binding.imageView.isVisible = resId > 0 //defStyleAttr 을 활용해서 view Visible 관리
//        binding.imageView.setImageResource(resId)
//    }

	//실제 사용 시 함수 
    fun setMenu(menu: String?) {
        this.tvMenu.text = menu
    }

    fun setContent(price: String?) {
        this.tvPrice.text = price
    }

}
//실제사용
   private fun initMenu(){
        val tr = MenuCommon(requireContext())
        tr.setMenu("닭꼬치")
        tr.setPrice("3,000원")
        binding.shopBottom.tl.addView(tr)
    }
profile
되고싶다

1개의 댓글

comment-user-thumbnail
2024년 2월 5일
답글 달기