Skip to content

Commit 34ffcae

Browse files
authored
Implement custom CategoryAdapter for grid view
Refactor MainActivity to use a custom adapter for grid items and improve animations.
1 parent 93c9c23 commit 34ffcae

1 file changed

Lines changed: 64 additions & 5 deletions

File tree

‎app/src/main/kotlin/com/ximanta/opendiary/MainActivity.kt‎

Lines changed: 64 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,14 @@ import android.content.Intent
99
import android.content.pm.PackageManager
1010
import android.os.Build
1111
import android.os.Bundle
12+
import android.view.LayoutInflater
1213
import android.view.View
14+
import android.view.ViewGroup
1315
import android.widget.AdapterView
14-
import android.widget.ArrayAdapter
16+
import android.widget.BaseAdapter
1517
import android.widget.EditText
1618
import android.widget.GridView
19+
import android.widget.LinearLayout
1720
import android.widget.TextView
1821
import android.widget.Toast
1922

@@ -33,6 +36,10 @@ class MainActivity : Activity() {
3336
"Watched"
3437
)
3538

39+
private val icons = arrayOf(
40+
"🏃", "🍽️", "💡", "✅", "😞", "🏆", "🚗", "📅", "🎬"
41+
)
42+
3643
private fun rid(name: String, type: String): Int =
3744
resources.getIdentifier(name, type, packageName)
3845

@@ -48,15 +55,23 @@ class MainActivity : Activity() {
4855
}
4956
}
5057

51-
val title = findViewById<TextView>(rid("titleView", "id"))
52-
title.text = "Daymark"
58+
val header = findViewById<LinearLayout>(rid("header", "id"))
59+
header.alpha = 0f
60+
header.translationY = -40f
5361

5462
val grid = findViewById<GridView>(rid("gridButtons", "id"))
55-
val adapter = ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, categories)
56-
grid.adapter = adapter
63+
grid.adapter = CategoryAdapter()
5764

5865
grid.setOnItemClickListener(object : AdapterView.OnItemClickListener {
5966
override fun onItemClick(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
67+
if (view != null) {
68+
view.animate()
69+
.scaleX(0.94f).scaleY(0.94f)
70+
.setDuration(80)
71+
.withEndAction {
72+
view.animate().scaleX(1f).scaleY(1f).setDuration(120).start()
73+
}.start()
74+
}
6075
val category = categories[position]
6176
if (category == "Schedule/Tasks" || category == "Plan for Tomorrow") {
6277
val i = Intent(this@MainActivity, TasksActivity::class.java)
@@ -77,6 +92,50 @@ class MainActivity : Activity() {
7792
startSyncService(this)
7893
}
7994

95+
override fun onResume() {
96+
super.onResume()
97+
98+
val header = findViewById<LinearLayout>(rid("header", "id"))
99+
header.animate()
100+
.alpha(1f)
101+
.translationY(0f)
102+
.setDuration(500)
103+
.setStartDelay(50)
104+
.start()
105+
106+
val grid = findViewById<GridView>(rid("gridButtons", "id"))
107+
grid.postDelayed({
108+
for (i in 0 until grid.childCount) {
109+
val child = grid.getChildAt(i)
110+
child.alpha = 0f
111+
child.translationY = 60f
112+
child.animate()
113+
.alpha(1f)
114+
.translationY(0f)
115+
.setDuration(350)
116+
.setStartDelay((i * 55).toLong())
117+
.start()
118+
}
119+
}, 120)
120+
}
121+
122+
private inner class CategoryAdapter : BaseAdapter() {
123+
private val inflater = LayoutInflater.from(this@MainActivity)
124+
125+
override fun getCount(): Int = categories.size
126+
override fun getItem(position: Int): Any = categories[position]
127+
override fun getItemId(position: Int): Long = position.toLong()
128+
129+
override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
130+
val view = convertView ?: inflater.inflate(rid("item_category", "layout"), parent, false)
131+
val icon = view.findViewById<TextView>(rid("itemIcon", "id"))
132+
val label = view.findViewById<TextView>(rid("itemLabel", "id"))
133+
icon.text = icons[position]
134+
label.text = categories[position]
135+
return view
136+
}
137+
}
138+
80139
private fun showEntryDialog(category: String) {
81140
val input = EditText(this)
82141
input.hint = "Type here"

0 commit comments

Comments
 (0)