참고 강의
구현 계획
하단 네비게이션바 탭에 맞춰 프래그먼트 전환하기
gradle(Module) 파일에 라이브러리 싱크
dependencies {
implementation 'com.fxn769:ariana:1.0'
}
각 fragment의 xml 파일 작성
TextView 추가
<TextView
android:id="@+id/text_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ADD"
android:textSize="50dp"
android:textColor="#fff"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"/>
activity_main.xml
ImageView, ViewPager 추가
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/img_view"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintTop_toBottomOf="parent"
app:layout_constraintBottom_toTopOf="@+id/chip_navigation"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"/>
<androidx.viewpager.widget.ViewPager
android:id="@+id/view_pg"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintTop_toBottomOf="parent"
app:layout_constraintBottom_toTopOf="@+id/chip_navigation"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
/>
<com.ismaeldivita.chipnavigation.ChipNavigationBar
android:id="@+id/chip_navigation"
android:layout_width="match_parent"
android:layout_height="70dp"
android:background="#fff"
android:gravity="center"
android:elevation="16dp"
app:cnb_menuResource="@menu/bottom_navigation"
app:layout_constraintBottom_toBottomOf="parent"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
뷰페이저 어댑터 만들기
package com.myapplication2.Adapter
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentPagerAdapter
import com.myapplication2.R
import kotlinx.android.synthetic.main.fragment_mood.view.*
private const val ARG_PARAM1 = "param1"
class NavigationViewPagerAdapter (supportFragmentManager: FragmentManager): FragmentPagerAdapter(supportFragmentManager) {
var list = ArrayList<String>()
override fun getItem(position: Int): Fragment {
//첫 페이지가 될 fragment
return Mood.newInstance(list[position])
}
override fun getCount(): Int {
return list.size
}
class Mood: Fragment() {
private var param1: String? = ""
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
arguments?.let {
param1 = it.getString(ARG_PARAM1)
}
}
companion object {
@JvmStatic
fun newInstance(
param1: String
)=Mood().apply {
arguments = Bundle().apply {
putString(ARG_PARAM1, param1)
}
}
}
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
var rootView = LayoutInflater.from(context).inflate(R.layout.fragment_mood, null, false)
rootView.text_mood.text = param1
return rootView
}
}
}
MainActivity.kt
class MainActivity : AppCompatActivity() {
private val fragmentManager: FragmentManager = supportFragmentManager
private var fragmentAdd: AddFragment = AddFragment()
private var fragmentMood: MoodFragment = MoodFragment()
private var fragmentHabit: HabitFragment = HabitFragment()
private var fragmentChart: ChartFragment = ChartFragment()
private var fragmentProfile: ProfileFragment = ProfileFragment()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_loginresult)
//chip_navigation: activity_main.xml에서 네비게이션바 id
//id: menu디렉토리에서 정한 각 item의 id
chip_navigation.setOnItemSelectedListener { id ->
when (id) {
R.id.mood -> view_pg.currentItem = 0
R.id.habit_tracker -> view_pg.currentItem = 1
R.id.add -> view_pg.currentItem = 2
R.id.chart -> view_pg.currentItem = 3
R.id.profile -> view_pg.currentItem = 4
}
}
//view_pg: activity_main.xml에서 viewpager의 id
view_pg.adapter = NavigationViewPagerAdapter(supportFragmentManager).apply {
list = ArrayList<String>().apply {
add("Mood")
add("Habit")
add("Add")
add("Chart")
add("Profile")
}
}
//img_view: activity_main.xml의 이미지뷰 id
view_pg.addOnPageChangeListener(
ArianaBackgroundListener(
getColors(),
img_view,
view_pg
)
)
}
private fun getColors():IntArray{
return intArrayOf(
//R.color.색깔name(color.xml파일 확인)
ContextCompat.getColor(this, R.color.navigation_color_moodtracker),
ContextCompat.getColor(this, R.color.navigation_color_habittracker),
ContextCompat.getColor(this, R.color.navigation_color_add),
ContextCompat.getColor(this, R.color.navigation_color_chart),
ContextCompat.getColor(this, R.color.navigation_color_profile)
)
}
}
수정해야 할 사항
로그인 후 보일 첫 프래그먼트의 네비게이션바 탭이 선택돼 있도록
'인문학도 개발일지 > 앱프로그래밍' 카테고리의 다른 글
[코틀린 kotlin] 새차원의 코틀린 강좌 - 2. Basic Syntax (0) | 2020.04.16 |
---|---|
[코틀린 kotlin] 새차원의 코틀린 강좌 - 1. why kotlin (0) | 2020.04.15 |
[안드로이드][레이아웃] 하단바 커스텀 - bottom navigation (0) | 2020.03.27 |
[안드로이드][레이아웃] 패스워드 기능 구현(비밀번호 숨기기) (0) | 2020.03.27 |
[안드로이드][코틀린] 클래스 상속방법 (0) | 2020.03.24 |