1. 리사이클러뷰를 띄울 xml파일에 RecyclerView
만들기
2. 리사이클러뷰에 들어갈 item
만들고 (rv_item.xml
), 리사이클러뷰에 연결
3. 리사이클러뷰 어댑터 생성 RVAdapter
Adapter
의 구성 요소
- getItemCount()
-onCreateViewHolder(parent: ViewGroup, viewType: Int) : RVAdapter.ViewHolder
- onBindViewHolder(holder: RVAdapter.ViewHolder, position: Int)
- ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView)
class RVAdapter(val items: MutableList<String>) : RecyclerView.Adapter<RVAdapter.ViewHolder>() {
//전체 리사이클러뷰의 개수
override fun getItemCount(): Int {
return items.size
}
//리사이클러뷰에 들어갈 아이템을 불러옴
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RVAdapter.ViewHolder{
val view = LayoutInflater.from(parent.context).inflate(R.layout.rv_item, parent, false)
return ViewHolder(view)
}
override fun onBindViewHolder(holder: RVAdapter.ViewHolder, position: Int) {
holder.bindItems(items[position])
}
inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView){
fun bindItems(item : String) {
}
}
}
4. 리사이클러뷰를 띄울 Activity.kt에서 item 생성하고 xml파일의 리사이클러뷰에 RVAdapter
연결해줌
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
val items = mutableListOf<String>()
items.add("1")
items.add("2")
items.add("3")
items.add("4")
items.add("5")
items.add("6")
items.add("7")
items.add("8")
items.add("9")
items.add("10")
items.add("11")
items.add("12")
binding.rv.adapter = RVAdapter(items)
binding.rv.layoutManager = LinearLayoutManager(this)
}
}
5. RVAdapter
의 ViewHolder
에서 item
에 items
의 각각의 아이템 입력
inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView){
fun bindItems(item : String) {
val rvTitle = itemView.findViewById<TextView>(R.id.rv_title)
rvTitle.text = item
}
}
1. 'app' 에서 Android Resource File 생성. 이때 Resource type을 Navigation
으로 설정 -> res
폴더에 자동으로 navigation폴더 생성됨(이름: nav.xml
)
2. activity_main
에 NavHostFragment container
생성, nav.xml
연결
<LinearLayout
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.fragment.app.FragmentContainerView
android:id="@+id/fragmentContainerView"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
app:defaultNavHost="true"
app:navGraph="@navigation/nav" />
</LinearLayout>
3. 각각의 Fragment
생성 - FirstFragment
,SecondFragment
,ThirdFragment
4. nav.xml
파일에서 fragment
들 추가. action
도 설정
<?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/nav"
app:startDestination="@id/firstFragment">
<fragment
android:id="@+id/firstFragment"
android:name="com.example.inflearn5.Fragment.FirstFragment"
android:label="fragment_first"
tools:layout="@layout/fragment_first" >
<action
android:id="@+id/action_firstFragment_to_secondFragment"
app:destination="@id/secondFragment" />
<action
android:id="@+id/action_firstFragment_to_thirdFragment"
app:destination="@id/thirdFragment" />
</fragment>
<fragment
android:id="@+id/secondFragment"
android:name="com.example.inflearn5.Fragment.SecondFragment"
android:label="fragment_second"
tools:layout="@layout/fragment_second" >
<action
android:id="@+id/action_secondFragment_to_firstFragment"
app:destination="@id/firstFragment" />
<action
android:id="@+id/action_secondFragment_to_thirdFragment"
app:destination="@id/thirdFragment" />
</fragment>
<fragment
android:id="@+id/thirdFragment"
android:name="com.example.inflearn5.Fragment.ThirdFragment"
android:label="fragment_third"
tools:layout="@layout/fragment_third" >
<action
android:id="@+id/action_thirdFragment_to_firstFragment"
app:destination="@id/firstFragment" />
<action
android:id="@+id/action_thirdFragment_to_secondFragment"
app:destination="@id/secondFragment" />
</fragment>
</navigation>
5. 각각의 fragment
파일에서 버튼 setOnClickListener
설정
ex) FirstFragment.kt
class FirstFragment : Fragment() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
val view = inflater.inflate(R.layout.fragment_first, container, false)
view.findViewById<Button>(R.id.btn2).setOnClickListener {
it.findNavController().navigate(R.id.action_firstFragment_to_secondFragment)
}
view.findViewById<Button>(R.id.btn3).setOnClickListener {
it.findNavController().navigate(R.id.action_firstFragment_to_thirdFragment)
}
return view
}
}
RecyclerView
, Glide