안드로이두
10
2021-04-21 20:36:48
0
212

mvvm패턴, 버튼 onclick 질문


button layout 

<?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"
    xmlns:tools="http://schemas.android.com/tools">

    <data>

        <variable
            name="text"
            type="String" />

        <variable
            name="onClick"
            type="android.view.View.OnClickListener" />

    </data>

    <TextView
        android:id="@+id/textview_positive_button"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/shape_positive_button"
        android:backgroundTint="@color/main_blue"
        android:gravity="center"
        android:text="@{text}"
        android:textColor="@color/colorPrimary"
        android:textSize="18dp"
        android:onClick="@{onClick ?? null}"
        tools:text="@string/confirm" />
</layout>


include 한 Main SignActivity 

<data>

    <import type="enums.SignUpStep" />

    <variable
        name="signUpViewModel"
        type="viewmodel.SignUpViewModel" />
</data>
<include
    android:id="@+id/include"
    layout="@layout/layout_positive_button"
    android:layout_width="match_parent"
    android:layout_height="60dp"
    android:layout_margin="5dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/recommender_constraint"
    app:onSafeClick="@{() ->signUpViewModel.moveStep(SignUpStep.PASSWORD)}"
    />


bindingAdapter

@BindingAdapter("onSafeClick")
fun View.setOnSafeClickListener(clickListener: View.OnClickListener?) {
    clickListener?.also {
        setOnClickListener(OnSafeClickListener(it))
    } ?: setOnClickListener(null)
}


safeClick

abstract class OnSafeClickListener(
        private val clickDelayMilliSecond: Long = CLICK_DELAY_TIME
) : View.OnClickListener {

    companion object {
        private const val CLICK_DELAY_TIME = 1000L
    }

    private var lastClickTime = 0L

    protected abstract fun onSingleClick(view: View)

    override fun onClick(view: View) {
        val now = System.currentTimeMillis()
        if (now - lastClickTime > clickDelayMilliSecond) {
            onSingleClick(view)
            lastClickTime = now
        }
    }
}


인데 버튼을 클릭하면 fragment 를 바꾸려고하는데 Timber가 안뜨는거보면 클릭 자체가 안먹는데 

보통 버튼 클릭을 처리할때 어떻게하나요? activity 에서 처리안하고 xml 단에서 Viewmodel 에서 클릭함수 적용시켜서 처리하고싶은데 혹시 어떤식으로 고치는게 좋을지 팁좀 부탁드립니다 

0
  • 답변 0

  • 로그인을 하시면 답변을 등록할 수 있습니다.