@@ -5,28 +5,31 @@ import android.content.Context
5
5
import android.content.Intent
6
6
import android.os.AsyncTask
7
7
import android.os.Bundle
8
+ import android.view.LayoutInflater
8
9
import android.view.View
9
- import android.widget.AdapterView
10
- import android.widget.GridView
11
- import android.widget.TextView
10
+ import android.view.ViewGroup
11
+ import android.widget.ProgressBar
12
12
import android.widget.Toast
13
13
import androidx.annotation.LayoutRes
14
+ import androidx.appcompat.widget.Toolbar
15
+ import androidx.recyclerview.widget.GridLayoutManager
16
+ import androidx.recyclerview.widget.RecyclerView
14
17
import me.ycdev.android.arch.ArchConstants
15
18
import me.ycdev.android.arch.ArchConstants.IntentType
16
19
import me.ycdev.android.arch.activity.AppCompatBaseActivity
17
20
import me.ycdev.android.arch.wrapper.ToastHelper
18
21
import me.ycdev.android.lib.common.utils.IntentUtils
19
22
import me.ycdev.android.lib.common.wrapper.BroadcastHelper
20
23
import me.ycdev.android.lib.commonui.R
21
- import me.ycdev.android.lib.commonui.base.ListAdapterBase
22
- import me.ycdev.android.lib.commonui.base.ViewHolderBase
24
+ import me.ycdev.android.lib.commonui.databinding.CommonuiGridEntriesItemBinding
25
+ import me.ycdev.android.lib.commonui.recyclerview.MarginItemDecoration
23
26
24
27
@Suppress(" MemberVisibilityCanBePrivate" , " unused" )
25
- abstract class GridEntriesActivity : AppCompatBaseActivity (), AdapterView.OnItemClickListener,
26
- AdapterView .OnItemLongClickListener {
28
+ abstract class GridEntriesActivity : AppCompatBaseActivity () {
27
29
28
- protected lateinit var adapter: SystemEntriesAdapter
29
- protected lateinit var gridView: GridView
30
+ protected lateinit var entriesAdapter: SystemEntriesAdapter
31
+ protected lateinit var gridView: RecyclerView
32
+ protected lateinit var loadingView: ProgressBar
30
33
31
34
protected open val contentViewLayout: Int
32
35
@LayoutRes get() = R .layout.commonui_grid_entries
@@ -53,15 +56,15 @@ abstract class GridEntriesActivity : AppCompatBaseActivity(), AdapterView.OnItem
53
56
override val clickAction: ((Context ) -> Unit )? = ::onItemClicked
54
57
override val longClickAction: ((Context ) -> Unit )? = ::onItemLongClicked
55
58
56
- fun onItemClicked (context : Context ) {
59
+ protected open fun onItemClicked (context : Context ) {
57
60
if (type == ArchConstants .INTENT_TYPE_ACTIVITY ) {
58
61
IntentUtils .startActivity(context, intent)
59
62
} else if (type == ArchConstants .INTENT_TYPE_BROADCAST ) {
60
63
BroadcastHelper .sendToExternal(context, intent, perm)
61
64
}
62
65
}
63
66
64
- private fun onItemLongClicked (context : Context ) {
67
+ protected open fun onItemLongClicked (context : Context ) {
65
68
ToastHelper .show(context, desc, Toast .LENGTH_LONG )
66
69
}
67
70
}
@@ -70,71 +73,91 @@ abstract class GridEntriesActivity : AppCompatBaseActivity(), AdapterView.OnItem
70
73
super .onCreate(savedInstanceState)
71
74
setContentView(contentViewLayout)
72
75
73
- adapter = SystemEntriesAdapter (this )
76
+ val toolbar = findViewById<Toolbar >(R .id.toolbar)
77
+ setSupportActionBar(toolbar)
78
+ supportActionBar?.setDisplayHomeAsUpEnabled(true )
79
+
80
+ entriesAdapter = SystemEntriesAdapter (this )
74
81
75
82
gridView = findViewById(R .id.grid)
76
- gridView.adapter = adapter
77
- gridView.onItemClickListener = this
78
- gridView.onItemLongClickListener = this
83
+ loadingView = findViewById(R .id.progress)
84
+
85
+ gridView.apply {
86
+ adapter = entriesAdapter
87
+ layoutManager = GridLayoutManager (this @GridEntriesActivity, 3 )
88
+ addItemDecoration(MarginItemDecoration .create(getGridEntriesMargin()))
89
+ }
79
90
80
91
loadItems()
81
92
}
82
93
94
+ open fun getGridEntriesMargin (): Int {
95
+ val a = obtainStyledAttributes(intArrayOf(R .attr.commonuiGridEntriesItemMargin))
96
+ val margin: Int = a.getDimensionPixelSize(0 , 0 )
97
+ a.recycle()
98
+ return margin
99
+ }
100
+
83
101
@SuppressLint(" StaticFieldLeak" )
84
- private fun loadItems () {
102
+ protected open fun loadItems () {
85
103
if (needLoadIntentsAsync) {
104
+ loadingView.visibility = View .VISIBLE
105
+
86
106
object : AsyncTask <Void , Void , List <Entry >>() {
87
107
override fun doInBackground (vararg params : Void ): List <Entry > {
88
108
return intents
89
109
}
90
110
91
111
override fun onPostExecute (result : List <Entry >) {
92
- adapter.setData(intents)
112
+ loadingView.visibility = View .GONE
113
+ entriesAdapter.data = intents
114
+ entriesAdapter.notifyDataSetChanged()
93
115
}
94
116
}.execute()
95
117
} else {
96
- adapter.setData(intents)
118
+ loadingView.visibility = View .GONE
119
+ entriesAdapter.data = intents
97
120
}
98
121
}
99
122
100
- override fun onItemClick (parent : AdapterView <* >, view : View , position : Int , id : Long ) {
101
- val item = adapter.getItem(position)
102
- item.clickAction?.invoke(this )
103
- }
104
-
105
- override fun onItemLongClick (
106
- parent : AdapterView <* >,
107
- view : View ,
108
- position : Int ,
109
- id : Long
110
- ): Boolean {
111
- val item = adapter.getItem(position)
112
- item.longClickAction?.invoke(this )
113
- return true
114
- }
115
-
116
123
/* *
117
124
* Decide if we need to invoke [.getIntent] async.
118
125
* @return true for async and false for sync. false by default
119
126
*/
120
127
protected open val needLoadIntentsAsync: Boolean = false
121
128
122
- protected open class SystemEntriesAdapter (cxt : Context ) :
123
- ListAdapterBase <Entry , SystemEntriesAdapter .ViewHolder >(cxt) {
129
+ protected open class SystemEntriesAdapter (val context : Context ) :
130
+ RecyclerView .Adapter <SystemEntriesAdapter .ViewHolder >() {
131
+
132
+ var data: List <Entry >? = null
133
+
134
+ private fun getItem (position : Int ): Entry {
135
+ return data!! [position]
136
+ }
124
137
125
- override val itemLayoutResId: Int = R .layout.commonui_grid_entries_item
138
+ override fun getItemCount (): Int {
139
+ return data?.size ? : 0
140
+ }
126
141
127
- override fun createViewHolder (itemView : View , position : Int ): ViewHolder {
128
- return ViewHolder (itemView, position)
142
+ override fun onCreateViewHolder (parent : ViewGroup , viewType : Int ): ViewHolder {
143
+ val itemView = LayoutInflater .from(context)
144
+ .inflate(R .layout.commonui_grid_entries_item, parent, false )
145
+ return ViewHolder (itemView)
129
146
}
130
147
131
- override fun bindView (item : Entry , holder : ViewHolder ) {
132
- holder.titleView.text = item.title
148
+ override fun onBindViewHolder (holder : ViewHolder , position : Int ) {
149
+ val item = getItem(position)
150
+ holder.binding.title.text = item.title
151
+
152
+ holder.binding.root.setOnClickListener { item.clickAction?.invoke(context) }
153
+ holder.binding.root.setOnLongClickListener {
154
+ item.longClickAction?.invoke(context)
155
+ return @setOnLongClickListener true
156
+ }
133
157
}
134
158
135
- class ViewHolder (itemView : View , position : Int ) :
136
- ViewHolderBase (itemView, position) {
137
- var titleView: TextView = itemView.findViewById(R .id.title)
159
+ class ViewHolder (itemView : View ) : RecyclerView.ViewHolder(itemView) {
160
+ val binding: CommonuiGridEntriesItemBinding = CommonuiGridEntriesItemBinding .bind(itemView)
138
161
}
139
162
}
140
163
}
0 commit comments