Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ dependencies {
implementation 'androidx.core:core-ktx:1.3.1'
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation "androidx.recyclerview:recyclerview:1.1.0"
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,35 @@
package tw.andyang.kotlinandroidworkshop

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

private var todos = listOf<Todo>()

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

val adapter = TodoAdapter()
recyclerView.adapter = adapter
recyclerView.layoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
recyclerView.addItemDecoration(DividerItemDecoration(this, LinearLayoutManager.VERTICAL))

todos = todos.toMutableList().apply {
add(Todo.Title(getString(R.string.todo_list_title)))
}

adapter.submitList(todos)

buttonAdd.setOnClickListener {
todos = todos.toMutableList().apply {
add(Todo.Item("world", false))
}
adapter.submitList(todos)
}
}
}
14 changes: 14 additions & 0 deletions app/src/main/java/tw/andyang/kotlinandroidworkshop/Todo.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package tw.andyang.kotlinandroidworkshop

sealed class Todo(val viewType: Int) {
data class Title(val text: String) : Todo(TYPE_TITLE)
data class Item(
val memo: String,
val checked: Boolean
) : Todo(TYPE_ITEM)

companion object {
const val TYPE_TITLE = 0
const val TYPE_ITEM = 1
}
}
62 changes: 62 additions & 0 deletions app/src/main/java/tw/andyang/kotlinandroidworkshop/TodoAdapter.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package tw.andyang.kotlinandroidworkshop

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.appcompat.widget.AppCompatCheckBox
import androidx.appcompat.widget.AppCompatTextView
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import kotlinx.android.synthetic.main.item_todo.view.*

class TodoAdapter : ListAdapter<Todo, RecyclerView.ViewHolder>(
object : DiffUtil.ItemCallback<Todo>() {
override fun areItemsTheSame(oldItem: Todo, newItem: Todo): Boolean {
return oldItem.viewType == newItem.viewType
}

override fun areContentsTheSame(oldItem: Todo, newItem: Todo): Boolean {
return oldItem == newItem
}
}
) {

override fun getItemViewType(position: Int): Int {
return getItem(position).viewType
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
return when (viewType) {
Todo.TYPE_TITLE -> TodoTitleViewHolder(parent)
else -> TodoViewHolder(parent)
}
}

override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
when (val todo = getItem(position)) {
is Todo.Title -> (holder as TodoTitleViewHolder).bind(todo)
is Todo.Item -> (holder as TodoViewHolder).bind(todo)
}
}
}

class TodoViewHolder(parent: ViewGroup) : RecyclerView.ViewHolder(
LayoutInflater.from(parent.context).inflate(R.layout.item_todo, parent, false)
) {

private val checkbox: AppCompatCheckBox = itemView.checkbox

fun bind(todo: Todo.Item) {
checkbox.text = todo.memo
checkbox.isChecked = todo.checked
}

}

class TodoTitleViewHolder(parent: ViewGroup) : RecyclerView.ViewHolder(
LayoutInflater.from(parent.context).inflate(R.layout.item_title, parent, false)
) {
fun bind(todo: Todo.Title) {
(itemView as AppCompatTextView).text = todo.text
}
}
20 changes: 13 additions & 7 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,21 @@
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
tools:context=".MainActivity">

<TextView
android:layout_width="wrap_content"
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="60dp"
tools:listitem="@layout/item_todo" />

<androidx.appcompat.widget.AppCompatButton
android:id="@+id/buttonAdd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
android:text="@string/add_todo"
app:layout_constraintBottom_toBottomOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
9 changes: 9 additions & 0 deletions app/src/main/res/layout/item_title.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.AppCompatTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:gravity="center"
android:text="@string/todo_list_title"
android:textColor="#000"
android:textSize="26sp" />
15 changes: 15 additions & 0 deletions app/src/main/res/layout/item_todo.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?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="wrap_content"
android:orientation="horizontal"
android:padding="16dp">

<androidx.appcompat.widget.AppCompatCheckBox
android:id="@+id/checkbox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:text="Memo" />

</LinearLayout>
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
<resources>
<string name="app_name">KotlinAndroidWorkshop</string>
<string name="todo_list_title">備忘錄</string>
<string name="add_todo">新增</string>
</resources>