viewBinding & Fragment & FragmentContainerView example > IT 기술백서

build.gragle

[code]

android {

    buildFeatures {

        viewBinding = true

    }

}

[/code]

 

ActivityMain.kt

[code]

package kr.kbay.example1

import androidx.appcompat.app.AppCompatActivity

import android.os.Bundle

import kr.kbay.bogosa.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {

    val TAG: String = “로그”

    lateinit var binding: ActivityMainBinding

    lateinit var firstFragment: FirstFragment

    lateinit var secondFragment: SecondFragment

    lateinit var thirdFragment: ThirdFragment

    override fun onCreate(savedInstanceState: Bundle?) {

        super.onCreate(savedInstanceState)

        binding = ActivityMainBinding.inflate(layoutInflater)

        setContentView(binding.root)

        setSupportActionBar(binding.toolbar.root)

        binding.bottomNavigation.setOnNavigationItemSelectedListener {

            when(it.itemId) {

                R.id.firstFragment -> {

                    firstFragment = FirstFragment.newInstance()

                    supportFragmentManager.beginTransaction().replace(R.id.fragments_frame, firstFragment).commit()

                }

                R.id.secondFragment -> {

                    secondFragment = SecondFragment.newInstance()

                    supportFragmentManager.beginTransaction().replace(R.id.fragments_frame, secondFragment).commit()

                }

                R.id.thirdFragment -> {

                    thirdFragment = ThirdFragment.newInstance()

                    supportFragmentManager.beginTransaction().replace(R.id.fragments_frame, thirdFragment).commit()

                }

            }

            true

        }

    }

}

[/code]

res.layout

activity_main.xml

[code]

<?xml version=”1.0″ encoding=”utf-8″?>

<RelativeLayout 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”>

    <androidx.fragment.app.FragmentContainerView

        android:id=”@+id/fragments_frame”

        android:layout_width=”match_parent”

        android:layout_height=”match_parent”

        android:layout_below=”@id/toolbar”

        android:layout_above=”@id/bottom_navigation”

        />

    <com.google.android.material.bottomnavigation.BottomNavigationView

        android:id=”@+id/bottom_navigation”

        android:layout_width=”match_parent”

        android:layout_height=”wrap_content”

        app:itemBackground=”@color/cardview_dark_background”

        app:itemTextColor=”@drawable/selector”

        app:itemIconTint=”@drawable/selector”

        app:menu=”@menu/menu_navigation”

        android:layout_alignParentBottom=”true”

        />

</RelativeLayout>

[/code]

 

res.navigation

bot_navi.xml

 

[code]

<?xml version=”1.0″ encoding=”utf-8″?>

<navigation 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:id=”@+id/bot_navi”

    app:startDestination=”@id/firstFragment”>

    <fragment

        android:id=”@+id/firstFragment”

        android:name=”kr.kbay.bogosa.FirstFragment”

        android:label=”fragment_first”

        tools:layout=”@layout/fragment_first”

        />

    <fragment

        android:id=”@+id/secondFragment”

        android:name=”kr.kbay.bogosa.SecondFragment”

        android:label=”fragment_second”

        tools:layout=”@layout/fragment_second” />

    <fragment

        android:id=”@+id/thirdFragment”

        android:name=”kr.kbay.bogosa.ThirdFragment”

        android:label=”fragment_third”

        tools:layout=”@layout/fragment_third” />

</navigation>

[/code]

 

res.layout

fragment_first.xml

[code]

<?xml version=”1.0″ encoding=”utf-8″?>

<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”

    xmlns:tools=”http://schemas.android.com/tools”

    android:layout_width=”match_parent”

    android:layout_height=”match_parent”

    xmlns:app=”http://schemas.android.com/apk/res-auto”

    tools:context=”.FirstFragment”

    android:orientation=”vertical”

    android:gravity=”center”>

    <TextView

        android:layout_width=”wrap_content”

        android:layout_height=”wrap_content”

        android:text=”First”

        android:textSize=”50dp”

        />

</LinearLayout>

[/code]

 

res.layout

fragment_second.xml

생략

 

res.layout

fragment_third.xml

생략

 

댓글 달기

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다

위로 스크롤