뚜벅이!
Mobile :)
뚜벅이!
전체 방문자
오늘
어제
  • 분류 전체보기 (52)
    • 코딩테스트 (16)
      • programmers level1 (7)
      • codility (9)
    • 프로그래밍 공부 (31)
      • Spring Boot (6)
      • Nuxt.js (5)
      • Node.js (3)
      • Etc (11)
      • Android (6)
    • 잡다한 글 (3)
    • 토이프로젝트 (1)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

인기 글

태그

  • lesson3
  • 연습
  • level1
  • Jetpack
  • 프로그래머스
  • token
  • Vue
  • Kotlin
  • ad
  • JS
  • NavBar
  • nuxt.js
  • programmers
  • nuxt
  • codillity
  • javascript
  • Notification
  • 부트
  • node.js
  • Vue.js
  • 초보자
  • lesson4
  • lesson2
  • 스킬체크테스트
  • node
  • firebase
  • docker
  • Spring
  • Spring boot
  • AndroidX

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
뚜벅이!

Mobile :)

[android] migration 진행중 ... kotlin v, IDE v, library v...etc...
프로그래밍 공부/Android

[android] migration 진행중 ... kotlin v, IDE v, library v...etc...

2022. 3. 29. 15:46
728x90

최근에 작업하던 프로젝트의 kotlin 버전을 업그레이드 하면서 참 많은 문제가 터졌다.

 

우선적으로 kotlin plugin을 최신화 하기 위해선 IDE를 재설치 해야한다.

2022년3월29일 기준으로 stable version인 bumblebee를 다운로드 받았다.

 

android studio 4.2v 이상으로 넘어오니 지원하던

kotlinx.android.synthetic

이녀석이 사라졌다.

 

이런식으로 아이디만 가져오면 자동으로 매핑해주는 친구였는데

ViewBinding으로 바꾸도록 가이드가 되어있다.

 

 

관련한 자세한 내용은 링크로 대체한다.

https://medium.com/androiddevelopers/use-view-binding-to-replace-findviewbyid-c83942471fc

 

Use view binding to replace findViewById

New in Android Studio and Android Gradle Plugin 3.6, view binding gives you the ability to replace findViewId with generated binding…

medium.com

 

해결방법은 의외로 간단하다.

https://developer.android.com/topic/libraries/view-binding

 

뷰 결합  |  Android 개발자  |  Android Developers

뷰 결합 뷰 결합 기능을 사용하면 뷰와 상호작용하는 코드를 쉽게 작성할 수 있습니다. 모듈에서 사용 설정된 뷰 결합은 모듈에 있는 각 XML 레이아웃 파일의 결합 클래스를 생성합니다. 바인딩

developer.android.com

Activity는 이런식으로 쉽게 변환이 가능하고,

 

 

 

RecyclerView 같은 Adapter를 사용하는 녀석들은

onCreateViewHolder에서 ViewHolder로 넘겨줄때 바인딩으로 넘겨주거나 내부에서 따로 설정해도 된다.

 

방법 1.

private lateinit var binding: AdpaterDirectoryBinding

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TestViewHolder {
    binding = AdapterTestBinding.inflate(LayoutInflater.from(parent.context))
    return TestViewHolder(binding)
}

class TestViewHolder(binding: AdapterTestBinding) : RecyclerView.ViewHolder(binding.root) {

     binding.tvTest.text = "짠"
}

기존에 view를 넘겨주던 것과는 달리 binding을 넘겨준다.

 

 

 

 

방법 2. (하나의 ViewHolder의 다양한 inflater가 들어가는 경우)

class WholeInfoViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
    fun testCase1(testCase1: TestCase1) {
        val testCase1Binding = AdapterTestCase1Binding.bind(itemView)
        testCase1Binding.testCase1.text = "짠1"
    }
    fun testCase2(testCase2: TestCase2) {
        val testCase2Binding = AdapterTestCase2Binding.bind(itemView)
        testCase1Binding.testCase2.text = "짠2"
    }
}

이런식으로 onCreateViewHolder는 동일하게 하고, 내부 ViewHolder에서도 추가 가능하다.

 

 

 

 

 

 

 

 

Fragment에서 parent Activity의 Component를 가져와야하는 상황이 있을 수 있는데,

private lateinit var binding: ActivityMainBinding
val bind get() = binding

get을 추가해주자.

(requireActivity() as MainActivity).bind.testText.text = "짠"
728x90
저작자표시 (새창열림)

'프로그래밍 공부 > Android' 카테고리의 다른 글

[android] context 란  (0) 2022.12.21
[android-kotlin] firebase notification / message  (0) 2022.07.14
[kotlin] LiveData & DataBinding  (0) 2021.07.20
[kotlin] Android jetpack Navigation  (2) 2021.05.14
[Android&Kotlin] gps tracking  (3) 2021.02.04
    '프로그래밍 공부/Android' 카테고리의 다른 글
    • [android] context 란
    • [android-kotlin] firebase notification / message
    • [kotlin] LiveData & DataBinding
    • [kotlin] Android jetpack Navigation
    뚜벅이!
    뚜벅이!
    2022. 4년차 안드로이드 개발자 wndnjs19@gmail.com

    티스토리툴바