Platform/Android

[TroubleShooting] androidx.constraintlayout.widget.Group 사용 시, visibility 적용 안될 때

개랭갱깽스타 2021. 8. 30. 20:16

문제

버튼을 눌러서 진입하는 화면이 있었다. 버튼을 1번 눌러서 진입했을 때는, 그려져야 하는 뷰가 잘 그려졌지만, 2번 누르면, 그려져야 하는 뷰가 그려지지 않았다.

버튼을 1번 누르게끔 처리해서, 해당 이슈를 close 할 수 있었지만, 기본적으로 왜 이런 이슈가 발생한 것인지 궁금해서 원인을 찾아보았다.

 

구조

XML 구조

<ConstraintLayout>
	...
	
    <include
    	android:id="@+id/include_A">
    
    <include
    	andorid:id="@+id/include_B">
    
    ...
    
    <androidx.constraintlayout.widget.Group
    	android:id="@+id/group_A"
        app:constraint_referenced_ids="include_A">
    
    <androidx.constraintlayout.widget.Group
    	android:id="@+id/group_B"
        app:constraint_referenced_ids="include_B">
        
</ConstraintLayout>

 

코드 구조 - 간략

onCreateView {
	return view inflate
}

onViewCreated {
	initLayout();	//관련 VIEW 들을 모두 GONE 상태로 초기화한다.
    
	getDataAsync();	//DB 에 접근해서 데이터를 가져온다.
}

initLayout {
	groupA.setVisibility(GONE);
	groupB.setVisibility(GONE);
}

getDataAsync {
	LiveData<Data> dataLiveData = viewModel.getDataAsync();
    dataLiveData.observe {
    	adjustLayout();	//데이터에 따라 groupA 나 B를 VISIBLE 상태로 만든다.
    }
}

adjustLayout() {
	조건에 따른 groupA or groupB .setVisiblity(VISILBE);
}

 

생각 과정

1. 처음 관련 VIEW 들을 모두 GONE 시켜놓고, 해당 레이아웃이 그려지기 위해서 DB 를 접근 한 뒤, 그 값에 맞는 레이아웃을 VISIBLE 시켜주는 비동기식 코드였다.

2. 비동기 부분에서 문제가 있을 것 같다고 섵부르게 판단하여, 그 부분을 중점적으로 보았다. 디버깅도 해보고, 로그도 찍어보았지만, 데이터를 가지고 오는 부분에서의 결과는 동일했다.(ㅠㅠ)

3. 그렇게 포기할까 하던 찰나에... layout 관련 문제니까 AndroidStudio 의 Layout Inspector 를 사용하면 어떨까 하는 생각이 들었다!

4. 그랬더니, 신기한 점을 발견했다. 

Layout Inspector 상에서 보이는 view 구조

view의 visibility 를 제어할 수 있는 include 된 view 는 gone 상태였다! group 의 visiblity 는 visible 인데!

include 된 layout 의 Attributes                                                                    group 의 Attributes 

include 된 layout 의 visiblity 를 Group 을 사용하지 않고, 직접 제어하는 곳이 있나 찾아보았지만, 찾을 수 없었다. 그래서... 역시 Googling! (키워드: widget.Group android constraint_referenced_ids > ... > group visible incldue gone android)

<StackOverflow BUGs>

  • Can't set visibility on constraint group
  • Constraint Layout - Group visibility is not working inside dynamic module

의 이슈가 있었다!

 찾아보니, ConstraintLayout 2.0.2 에서 해당 버그가 FIX 되었다.

constraintlayout-202 update 내용

 

프로젝트의 constraint-layout 버전을 확인해보니, 1.1.3 ! 최신버전(현재, 2.0.4)으로 업데이트를 진행했고, 결과는 성공!

include 된 layout 의 Attributes                                                                    group 의 Attributes 

뒷걸음질치다 원인을 찾은 듯 하지만! 그래도 뿌듯했다 :) LayoutInspector 에게도 무한 감사를!!!

 

해결

build.gradle 의 dependency > constraint-layout 버전을 최신(2021.08.30 기준, "2.0.4")으로 업데이트

build.gradle
[AS-IS]             version.gradle            [TO-DO]

(build.gradle 을 version.gradle 을 사용하여 분리하고 있으니, 궁금하신 분들은 아래 포스팅을 참고하시기 바랍니다!)

2020.10.19 - [Platform/Android] - [gradle] gradle 분리(1) (프로젝트 설정, root-dependencies 관리)

2020.10.19 - [Platform/Android] - [gradle] gradle 분리(2) (app-dependencies, sdkVersion 관리)

2020.10.19 - [GaengStroy] - [gradle] gradle 분리(3) (app-앱 버전 관리)

 

. 참고

https://androidstudio.googleblog.com/2020/10/constraintlayout-202.html

https://stackoverflow.com/questions/54944288/constraint-layout-group-visibility-is-not-working-inside-dynamic-module

https://stackoverflow.com/questions/47865436/cant-set-visibility-on-constraint-group/47893965

https://medium.com/android-ideas/constraintlayout-groups-be-careful-about-visibility-1e237914ce4f

반응형

'Platform > Android' 카테고리의 다른 글

setAllowFileAccess  (0) 2021.10.22
[Okhttp] timeout 설정  (0) 2021.10.08
[WebView] Android SDK 버전별 WebView 버전 확인  (0) 2021.07.27
[gradle] ViewBinding 추가  (0) 2021.07.25
[Error]  (0) 2021.07.22