티스토리 뷰

Android/Kotlin

[Kotlin] BottomNavigationView

혀가 길지 않은 개발자 2021. 4. 18. 14:58

 

1. BottomNavigationView

2. BottomNavigationView + ViewPager2


menu.xml

BottomNavigationView


bottom_menu.xml 생성 절차 1

 

bottom_navigation_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:title="Home"
        android:enabled="true"
        android:id="@+id/menu_home"
        android:icon="@drawable/ic_baseline_home_24" />
    <item
        android:title="Favorites"
        android:enabled="true"
        android:id="@+id/menu_favorites"
        android:icon="@drawable/ic_baseline_favorite_24" />
    <item
        android:title="Email"
        android:enabled="true"
        android:id="@+id/menu_email"
        android:icon="@drawable/ic_baseline_email_24" />
</menu>

bottom_navigation_menu.xml


HomeFragment.kt

package com.example.bottomnavigationexample

import androidx.fragment.app.Fragment

class HomeFragment : Fragment(R.layout.fragment_home)

FavoritesFragment.kt

package com.example.bottomnavigationexample

import androidx.fragment.app.Fragment

class FavoritesFragment : Fragment(R.layout.fragment_favorites)

EmailFragment.kt

package com.example.bottomnavigationexample

import androidx.fragment.app.Fragment

class EmailFragment : Fragment(R.layout.fragment_email)

 

xml 은 세 개다 중앙에 TextView에 화면 이름 적어놓음


activity_main.xml

<?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">

    <FrameLayout
        android:id="@+id/fl_"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toTopOf="@+id/bn_" />

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bn_"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:menu="@menu/bottom_navigation_menu"
        app:layout_constraintTop_toBottomOf="@+id/fl_"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toBottomOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

activity_main.xml

 

 

MainActivity.kt

package com.example.bottomnavigationexample

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.FrameLayout
import androidx.fragment.app.Fragment
import com.google.android.material.bottomnavigation.BottomNavigationView

class MainActivity : AppCompatActivity() {

    private val fl: FrameLayout by lazy {
        findViewById(R.id.fl_)
    }

    private val bn: BottomNavigationView by lazy {
        findViewById(R.id.bn_)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        supportFragmentManager.beginTransaction().add(fl.id, HomeFragment()).commit()

        bn.setOnNavigationItemSelectedListener {
            replaceFragment(
                when (it.itemId) {
                    R.id.menu_home -> HomeFragment()
                    R.id.menu_favorites -> FavoritesFragment()
                    else -> EmailFragment()
                }
            )
            true
        }
    }

    private fun replaceFragment(fragment: Fragment) {
        supportFragmentManager.beginTransaction().replace(fl.id, fragment).commit()
    }
}

실행 결과


 

2. BottomNavigationView + ViewPager2


activity_main.xml

<?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">

    <androidx.viewpager2.widget.ViewPager2
        android:id="@+id/vp_"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toTopOf="@+id/bn_" />

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bn_"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:menu="@menu/bottom_navigation_menu"
        app:layout_constraintTop_toBottomOf="@+id/vp_"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toBottomOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

activity_main.xml

 

 

MainActivity.kt

package com.example.bottomnavigationexample

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import com.google.android.material.bottomnavigation.BottomNavigationView

class MainActivity : AppCompatActivity() {

    private val vp: ViewPager2 by lazy {
        findViewById(R.id.vp_)
    }

    private val bn: BottomNavigationView by lazy {
        findViewById(R.id.bn_)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        vp.apply {
            adapter = ViewPagerAdapter(this@MainActivity)
            registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
                override fun onPageSelected(position: Int) {
                    super.onPageSelected(position)
                    bn.selectedItemId = when (position) {
                        0 -> R.id.menu_home
                        1 -> R.id.menu_favorites
                        else -> R.id.menu_email
                    }
                }
            })
        }

        bn.setOnNavigationItemSelectedListener {
            when (it.itemId) {
                R.id.menu_home -> vp.currentItem = 0
                R.id.menu_favorites -> vp.currentItem = 1
                else -> vp.currentItem = 2
            }
            true
        }
    }

    inner class ViewPagerAdapter(fa: FragmentActivity) : FragmentStateAdapter(fa) {
        override fun getItemCount() = 3

        override fun createFragment(position: Int): Fragment {
            return when (position) {
                0 -> HomeFragment()
                1 -> FavoritesFragment()
                else -> EmailFragment()
            }
        }
    }
}

실행 결과

 

 

 

 

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/12   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
글 보관함