[TIL]Android Material Component:2

mandoofu·2024년 9월 9일

안드로이드

목록 보기
6/20
post-thumbnail
  • headerLayout + menu로 구성
  • headerLayout
    • 보통 layout xml 파일로 구성
  • menu
    • /res/menu/*.xml 로 구성
  • layout_gravity
    • start : 왼쪽에서 오른쪽
    • end : 오른쪽에서 왼쪽

DrawerLayout

  • NavigationView가 나타나도록 하기 위한 기능 제공
  • NavigationView를 감싸는 layout
  • DrawerLayout 구성
    • include
      • Main Content xml
      • 첫번째에 자식으로 위치 시킴

ActionBarDrawerToggle

  • DrawerLayout과 ActionBar(ToolBar)의 기능을 묶어서 동기화를 쉽게 다룰 수 있도록 하는 Helper Class
  • ActionBarDrawerToggle의 syncState()를 호출해 DrawerLaytou과 ActionBar의 상태를 동기화 한다.

BottomSheetDialog

  • Activity 나 Fragment에 숨겨져 있다가 밑에서 나타나는 화면
  • Map등에서 많이 사용
  • 속성
    • peekHeight : Bottom Sheet 완전히 접을 때 높이
    • isHideable : Bottom Sheet 를 접을 때 숨길 수 있는지 여부
    • skipCollapsed : Bottom Sheet를 숨길 때 접히는 상태를 무시할지 여부
    • draggable : 뷰를 드래그해서 접을지 펼칠지 여부
    • fitToContents : 펼쳐진 뷰의 높이가 content를 감쌀 것인지의 여부
    • halfExpandedRatio : 절반만 펼쳐졌을 때 뷰의 높이를 결정(false 일 경우만 동작)
    • expandedOffset : 완전히 펼쳐진 상태일 때 뷰의 오프셋을 결정

BottomNavigationView

  • 하단에 Navigation Menu를 구성하는 방법
  • BottomNavigationView를주메뉴(Primary-Fragment)로 구성
  • 주메뉴의 Sub-Menu에 TabLayout으로 다시 구분해야 할 때 사용

ViewPager2

  • RecyclerView 기반
  • RTL(Right-To-Left) 지원
  • 각 Page Item은 RecyclerView.Adapter가 담당
  • offscreenPageLimit = 3 : 현재 화면에서 몇 개의 페이지를 미리 로딩할지 결정

FragmentStateAdapter

  • ViewPager2와 Fragment를 연결시켜주는 Adapter
  • RecyclerView.Adapter를 이용해 구현 됨
  • 동적으로 Fragment 제거 및 삭제시에도 유연하게 동작
  • 중요 메소드
    • createFragment(int position)
      • position에 위치한 Fragment를 ViewPager2에 반환
    • getItemCount()
      • ViewPager2에 붙일 Fragment의 size

CoordinatorLayout

  • ActionBar영역을 Scroll시 사라지게 하거나 확장 또는 축소
  • 자식뷰(direct view)들과의 Behavior를 연동하여 사용
    • AppBarLayout.Behavior
  • Anchor
    • Anchor는 자식뷰(direct view)와의 연관성을 표현

SharedPreferences

  • 선호설정
  • 사용자정보, 구성옵션, 설정 등 앱에서 간단하게 저장 및 조회해야 하는 경우
  • 선호설정들은 Key/Valup Pair형태의 Bundle로 저장
    • boolean, float, double, int, long, String , Set(String)
  • ex) SharedPreferences
SharedPreferences getSharedPreferences(String ,Int)
PreferenceManager : SharedPreference getDefaultSharedPreferences(Context)
  • SharedPreferences.Editor
    • apply : SharedPreferences에변경된값을File에적용한다.
    • clear : SharedPreferences에있는모든값을제거한다.
    • commit : SharedPreferences에변경된값을File에적용한다.
    • putBoolean : boolean 값을key/value형식으로SharedPreferences에저장한다.
    • putFloat : float 값을key/value형식으로SharedPreferences에저장한다.
    • putInt : int 값을key/value형식으로SharedPreferences에저장한다.
    • putLong : long 값을key/value형식으로SharedPreferences에저장한다.
    • putString : String 값을key/value형식으로SharedPreferences에저장한다.
    • putStringSet : StringSet 값을key/value형식으로SharedPreferences에저장한다.
    • remove : SharedPreferences에서해당key항목을제거한다.

Jetpack DataStore

  • 환경설정 및 간단한 자료거장에 대한 Shared Prefrences의 대안

  • Shared Preferences 의 단점

    • Thread Safety 에 대한 불안전 지원
      • CRUD 작업 시 Main Thread 에서 Block 후 진행
        • ANR 가능성 내포
    • Error Handling
      • SharedPreferences API는 ClassCastException 등의발생 여지가 존재
    • Data Consistency
      • 데이터 원자성 지원이 부족하며 이는 데이터 일관성 유지에 치명적
  • DataStore 장점

    • UI Thread 가 아닌 Coroutine Scope 내에서 CRUD 작업이 Flow를 기반으로 동작하므로 ANR 가능성X
    • CRUD에 작업이 일관된 트랜잭션으로 처리
    • Runtime Exception 에도 안전(Flow 의 catch 체인함수 사용)

Jetpack Navigation Graph

  • Navigation Graph : 앱에서 화면을 이루는 전체 구조
  • Navigation Destination : 앱을 구성하는 각 화면을 의미
  • Navigation Back Stack : 앱 안에서 destination에 이르는 사용자의 경로를 추적, 사용자가 다른 destination으로 전환 시 전환 화면은 currentdestination이 되고 그 전 destination은 백스택에 놓여짐
  • NavHostController : destination 사이의 이동과 Navigation Stack관리의 모던 것을 책임 지는 클래스

0개의 댓글