티스토리 뷰
Android/Kotlin
[Kotlin] CoordinatorLayout + AppBarLayout + Toolbar + RecyclerView
혀가 길지 않은 개발자 2020. 7. 4. 10:50CoordinatorLayout + AppBarLayout + Toolbar + RecyclerView
build.gradle (Module: app)
dependencies {
implementation 'com.google.android.material:material:1.1.0'
}
CoordinatorLayout, AppBarLayout 는 머티리얼 디자인이므로 의존성 추가
res/values/styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
</resources>
DarkActionBar 에서 NoActionBar 로 변경 (기본 타이틀바 없어짐)
recyclerview_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/txtView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="혀가 길지 않은 개발자"
android:textSize="20dp"
android:textStyle="bold"
android:gravity="center"
/>
</LinearLayout>
MyAdapter.kt
package com.jwsoft.kotlinproject
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
class MyAdapter(arrData: ArrayList<String>) : RecyclerView.Adapter<MyAdapter.MyViewHolder>() {
var arrListData = arrData
inner class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
var textView: TextView = itemView.findViewById(R.id.txtView)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
val context: Context = parent.context
val inflater: LayoutInflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
val view: View = inflater.inflate(R.layout.recyclerview_item, parent, false)
return MyViewHolder(view)
}
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
holder.textView.text = "혀가 길지 않은 개발자 " + arrListData[position]
}
override fun getItemCount(): Int {
return arrListData.size
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
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.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.appcompat.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:title="혀가 길지 않은 개발자"
app:titleTextColor="@android:color/white"
app:subtitle="CoordinatorLayout + AppBarLayout + Toolbar"
app:subtitleTextColor="@android:color/white"
app:layout_scrollFlags="scroll|enterAlways"
/>
</com.google.android.material.appbar.AppBarLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
MainActivity.kt
package com.jwsoft.kotlinproject
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
class MainActivity : AppCompatActivity() {
private lateinit var recyclerView: RecyclerView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
var arrListStrData = ArrayList<String>()
for (i in 1..100) {
arrListStrData.add(i.toString())
}
val adapter = MyAdapter(arrListStrData)
recyclerView = findViewById(R.id.recyclerView)
recyclerView.layoutManager = LinearLayoutManager(this, RecyclerView.VERTICAL, false)
recyclerView.adapter = adapter
}
}
'Android > Kotlin' 카테고리의 다른 글
[Kotlin] DataBinding (0) | 2020.07.09 |
---|---|
[Kotlin] ViewPager2 + FragmentStateAdapter (0) | 2020.07.06 |
[Kotlin] ViewPager2 + RecyclerView.Adapter (0) | 2020.07.03 |
[Kotlin] TabLayout (0) | 2020.07.02 |
[Kotlin] FrameLayout (0) | 2020.07.02 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- View
- 자바
- fragment
- activity
- ArrayList
- MVVM
- handler
- ViewPager2
- coroutine
- ViewModel
- CoordinatorLayout
- Design Pattern
- XML
- java
- 코틀린
- Vue.js #Vue.js + javascript
- Livedata
- 안드로이드 #코틀린 #Android #Kotlin
- TabLayout
- 혀가 길지 않은 개발자
- James Kim
- Architecture Pattern
- 안드로이드
- Kotlin
- DataBinding
- Intent
- recyclerview
- JSONObject
- Android
- JSONArray
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함