๐Ÿ”ฅTIL๐Ÿ”ฅ์ŠคํŒŒ๋ฅดํƒ€ | ์ˆ™๋ จ ํŒ€๊ณผ์ œ ํŠธ๋Ÿฌ๋ธ”์ŠˆํŒ…

hyihyiยท2024๋…„ 1์›” 16์ผ
0

TIL

๋ชฉ๋ก ๋ณด๊ธฐ
36/70
post-thumbnail

๐Ÿ“–ViewPager2

๐Ÿ™BottomNavigationView์™€ ViewPaging์„ ๋™์‹œ์— ๊ตฌํ˜„ํ•  ๋•Œ ์ƒ๊ฒผ๋˜ ๋ฌธ์ œ

๋ทฐํŽ˜์ด์ง•์œผ๋กœ ๋‹ค๋ฅธ ํ™”๋ฉด์œผ๋กœ ๋„˜๊ฒจ๋„ ๋’ค์— ํ™”๋ฉด์ด ๊ทธ๋Œ€๋กœ ๋‚จ์•„์žˆ์Œ

fragment ํƒœ๊ทธ๋Š” name ํƒœ๊ทธ๋กœ ์–ด๋–ค ํ”„๋ž˜๊ทธ๋จผํŠธ๊ฐ€ ๋ณด์ผ์ง€ ์ •ํ•ด๋†“๋Š” ์ •์  ๋™์ž‘ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋ทฐํŽ˜์ด์ง•์œผ๋กœ ํ™”๋ฉด์„ ๋„˜๊ฒผ์„ ๋•Œ ์•ˆ ๋„˜์–ด๊ฐ€๊ณ  ๋’ค์— ํ™”๋ฉด์ด ๋‚จ์•„์žˆ์„ ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋ž˜์„œ FragmentContainerView๋ฅผ ์‚ฌ์šฉํ•ด ๋™์  ๋™์ž‘ํ•˜๊ฒŒ ๋ณ€๊ฒฝํ•ด์•ผ ํ•œ๋‹ค.

์ œ๋Œ€๋กœ ๋™์ž‘ํ•˜์ง€ ์•Š๋Š” ์ฝ”๋“œ

๋™์ž‘ ํ™”๋ฉด

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">


        <com.google.android.material.bottomnavigation.BottomNavigationView
            android:id="@+id/nav_view"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="0dp"
            android:layout_marginEnd="0dp"
            android:background="?android:attr/windowBackground"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:menu="@menu/bottom_nav_menu" />

        <androidx.viewpager2.widget.ViewPager2
            android:id="@+id/view_pager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_constraintBottom_toTopOf="@+id/nav_view"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <fragment
            android:id="@+id/nav_host_fragment"
            android:name="androidx.navigation.fragment.NavHostFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:defaultNavHost="true"
            app:layout_constraintBottom_toTopOf="@+id/nav_view"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:navGraph="@navigation/mobile_navigation" />


</androidx.constraintlayout.widget.ConstraintLayout>

์ œ๋Œ€๋กœ ๋™์ž‘ํ•˜๋Š” ์ฝ”๋“œ

๋™์ž‘ ํ™”๋ฉด

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">


        <com.google.android.material.bottomnavigation.BottomNavigationView
            android:id="@+id/nav_view"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="0dp"
            android:layout_marginEnd="0dp"
            android:background="?android:attr/windowBackground"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:menu="@menu/bottom_nav_menu" />

        <androidx.viewpager2.widget.ViewPager2
            android:id="@+id/view_pager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_constraintBottom_toTopOf="@+id/nav_view"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <androidx.fragment.app.FragmentContainerView
            android:id="@+id/nav_host_fragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:defaultNavHost="true"
            app:layout_constraintBottom_toTopOf="@+id/nav_view"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:navGraph="@navigation/mobile_navigation" />


</androidx.constraintlayout.widget.ConstraintLayout>

๐Ÿ“–Binding

๐Ÿ™binding์œผ๋กœ ๋ฐ์ดํ„ฐ ๋ฐ”์ธ๋”ฉ์„ ํ•  ๋•Œ ์•ฑ์ด ๊บผ์ง€๋Š” ๋ฌธ์ œ

๋ทฐ์™€ ๊ด€๋ จ๋œ ์ž‘์—…์„ ํ•  ๋•Œ
ํ”„๋ž˜๊ทธ๋จผํŠธ์—์„œ ๋ทฐ์™€ ๊ด€๋ จ๋œ ์ž‘์—…์„ ํ•  ๋•Œ๋Š” onCreateView ๋˜๋Š” onViewCreated ๋ฉ”์„œ๋“œ ์•ˆ์—์„œ ์ˆ˜ํ–‰ํ•ด์•ผ ํ•œ๋‹ค.
binding์— ์ ‘๊ทผํ•˜๊ธฐ ์ „์—๋Š” ์ดˆ๊ธฐํ™”ํ•ด์•ผ ํ•œ๋‹ค.

์ œ๋Œ€๋กœ ๋™์ž‘ํ•˜์ง€ ์•Š๋Š” ์ฝ”๋“œ

class ContactDetailFragment : Fragment() {

    private lateinit var binding: FragmentContactDetailBinding
    private var contactData: Contact? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
            contactData = it.getParcelable(Constants.ARG_CONTACT)
        }
//        binding.imgDetailprofil.setImageResource(contactData!!.profileImage)
        binding.tvDetailname.text = contactData?.name.toString()
        binding.tvDetailMBTI.text = contactData?.mbti
        binding.tvDetailPhon.text = contactData?.phoneNumber
        binding.tvDetailEmail.text = contactData?.email
        binding.tvDetailBirth.text = contactData?.birthDate
        Log.d("๋ฐ›๋Š” Detail ํ”„๋ž˜๊ทธ๋จผํŠธ", contactData?.phoneNumber.toString())
    }

์ œ๋Œ€๋กœ ๋™์ž‘ํ•˜๋Š” ์ฝ”๋“œ

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    arguments?.let {
        contactData = it.getParcelable(Constants.ARG_CONTACT)
    }

    contactData?.let { data ->
//             binding.imgDetailprofil.setImageURI(data.profileImage)
        binding.tvDetailname.text = data.name
        binding.tvDetailMBTI.text = data.mbti
        binding.tvDetailPhon.text = data.phoneNumber
        binding.tvDetailEmail.text = data.email
        binding.tvDetailBirth.text = data.birthDate
        if(contactData?.favorite == true)
            binding.imgStar.setImageResource(R.drawable.icon_star_yellow)
        else
            binding.imgStar.setImageResource(R.drawable.icon_star_gray)
    }

}

๐Ÿ“–import android.R

import๋ฅผ ์ž˜๋ชปํ–ˆ์„ ๊ฐ€๋Šฅ์„ฑ์ด ๋†’๋‹ค.

๐Ÿ“–๋””๋ฒ„๊น…

adb shell setprop log.tag.FragmentManager DEBUG

๐Ÿ“–ํŠน์ • ํ”„๋ž˜๊ทธ๋จผํŠธ์— ๋ฐ”ํ…€ ๋‚ด๋น„๊ฒŒ์ด์…˜ ๋ฐ”๊ฐ€ ์•ˆ ๋ณด์ด๊ฒŒ ํ•  ๋•Œ

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

    val bottomNav = activity?.findViewById<BottomNavigationView>(R.id.nav_view)
    bottomNav?.visibility = View.GONE

}

*์ฃผ์˜ํ•  ์ 
destroy๋  ๋•Œ ๋‹ค์‹œ ๋ณด์ด๊ฒŒ ํ•ด์•ผ ํ•œ๋‹ค
override fun onDestroyView() {
super.onDestroyView()

    val bottomNav = activity?.findViewById<BottomNavigationView>(R.id.nav_view)
    bottomNav?.visibility = View.VISIBLE
}
profile
๋‚ด๊ฐ€ ์ดํ•ดํ•˜๊ธฐ ์‰ฝ๊ฒŒ ์“ฐ๋Š” ๋ธ”๋กœ๊ทธ

0๊ฐœ์˜ ๋Œ“๊ธ€