안드로이드 세계

[Android] DataBinding 편하게 사용하기(BaseActivity, BaseFragment) 본문

안드로이드(Android)/코틀린(Kotlin)

[Android] DataBinding 편하게 사용하기(BaseActivity, BaseFragment)

리안94 2021. 1. 23. 14:55

데이터 바인딩을 사용하다 보면 매번 액티비티나 프래그먼트마다 이렇게 만들어야 한다.

class MainActivity : AppCompatActivity(){

        private lateinit var binding: ActivityMainBinding

        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            binding = DataBindingUtil.setContentView(this, R.layout.activity_main)
        }
}

 

class FragmentMain :Fragment() {

        private lateinit var binding: FragmentMainBinding

        override fun onCreateView(
            inflater: LayoutInflater,
            container: ViewGroup?,
            savedInstanceState: Bundle?
        ): View? {
            binding = DataBindingUtil.inflate(inflater, R.layout.fragment_main, container, false) 
            return binding.root
        }
}

 

한두 개면 상관없지만 프로젝트가 커지다 보면 불편함이 느껴질 수 있다.

 

그럴 때면 다음과 같이 만들어주면 편하게 사용 가능하다.

open class BaseAppCompatActivity<T: ViewDataBinding>(@LayoutRes val layoutRes: Int) : AppCompatActivity(){

        lateinit var binding: T

        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            binding = DataBindingUtil.setContentView(this, layoutRes)
            binding.onCreate()
        }

        open fun T.onCreate() = Unit

}

 

open class BaseFragment<T: ViewDataBinding>(@LayoutRes val layoutRes: Int) : Fragment(){

        lateinit var binding: T

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

        open fun T.onCreateView() = Unit
        open fun T.onViewCreated() = Unit
}

 

다음과 같이 만들어주는데, 하나하나 만들면 생각보다 많은 클래스가 생성된다.

Activity, AppCompatActivity, Fragment, FragmentDialog 등등

 

sealed class를 사용했다.

sealed class UtilityBase{
    open class BaseFragment<T: ViewDataBinding>(@LayoutRes val layoutRes: Int) : Fragment(){

        lateinit var binding: T

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

        open fun T.onCreateView() = Unit
        open fun T.onViewCreated() = Unit
    }

    open class BaseAppCompatActivity<T: ViewDataBinding>(@LayoutRes val layoutRes: Int) : AppCompatActivity(){

        lateinit var binding: T

        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            binding = DataBindingUtil.setContentView(this, layoutRes)
            binding.onCreate()
        }

        open fun T.onCreate() = Unit

    }
}

 

사용법은 다음과 같다.

 

1. Activity

class MainActivity : UtilityBase.BaseAppCompatActivity<ActivityMainBinding>(R.layout.activity_main) {

    override fun ActivityMainBinding.onCreate() {
        binding.레이아웃아이디 ....
    }
}

 

2. Fragment

class FragmentMain :
    UtilityBase.BaseFragment<FragmentMainBinding>(R.layout.fragment_main) {

    override fun FragmentMainBinding.onCreateView() {
        
    }

    override fun FragmentMainBinding.onViewCreated() {
	binding.레이아웃아이디....
    }
}
Comments