@@ -85,7 +85,7 @@ class TagLayout : ViewGroup {
8585 if (i in 0 until childCount && i == defChoicePosition)
8686 childView.isSelected = true
8787 }
88- if (childView.background == null ){
88+ if (childView.background == null ) {
8989 childView.setBackgroundResource(R .drawable.tag_selector_bg)
9090 }
9191 mChildViewList.add(childView)
@@ -146,6 +146,9 @@ class TagLayout : ViewGroup {
146146 }
147147 }
148148
149+ /* *
150+ * 改变选中
151+ */
149152 private fun changedCheckedItemView (position : Int ) {
150153 Log .i(TAG , " choiceMode is $choiceMode " )
151154 for ((index, view) in mChildViewList.withIndex()) {
@@ -185,38 +188,16 @@ class TagLayout : ViewGroup {
185188 return defChoicePosition
186189 }
187190
188- override fun generateLayoutParams (attrs : AttributeSet ? ): LayoutParams {
189- return MarginLayoutParams (context, attrs)
190- }
191-
192- override fun generateDefaultLayoutParams (): LayoutParams {
193- return MarginLayoutParams (LayoutParams .WRAP_CONTENT , LayoutParams .WRAP_CONTENT )
194- }
195-
196- override fun generateLayoutParams (p : LayoutParams ? ): LayoutParams {
197- return MarginLayoutParams (p)
198- }
199-
200- interface OnItemClickListener {
201- fun onItemClick (position : Int , view : View )
202- }
203-
204- interface OnItemLongClickListener {
205- fun onItemLongClick (position : Int , view : View )
206- }
207-
208- interface OnSingleCheckedChangeListener {
209- fun onCheckedChanged (position : Int )
210- }
211-
212- interface OnMultipleCheckedChangeListener {
213- fun onCheckedChanged (positionList : MutableList <Int >)
214- }
215-
191+ /* *
192+ * 设置数据适配器
193+ */
216194 fun setAdapter (adapter : TagAdapter ) {
217195 mAdapter = adapter
196+ changedAdapter()
197+ }
198+
199+ private fun changedAdapter () {
218200 removeAllViews()
219- Log .i(TAG , " count ${mAdapter.getItemCount()} " )
220201 for (i in 0 until mAdapter.getItemCount()) {
221202 val itemView = mAdapter.onCreateView(this )
222203 mAdapter.onBindView(itemView, i)
@@ -228,6 +209,10 @@ class TagLayout : ViewGroup {
228209 return mAdapter
229210 }
230211
212+ /* *
213+ * 设置选择模式
214+ * @see ChoiceMode
215+ */
231216 fun setChoiceMode (mode : ChoiceMode ) {
232217 this .choiceMode = mode.choiceMode
233218 if (mode.choiceMode == ChoiceMode .SingleChoice .choiceMode) {
@@ -241,6 +226,9 @@ class TagLayout : ViewGroup {
241226 }
242227 }
243228
229+ /* *
230+ * 单选模式下,设置默认选择项
231+ */
244232 fun setDefaultChoicePosition (position : Int ) {
245233 this .defChoicePosition = position
246234 if (choiceMode == ChoiceMode .SingleChoice .choiceMode) {
@@ -252,9 +240,48 @@ class TagLayout : ViewGroup {
252240 return choiceMode
253241 }
254242
243+ override fun generateLayoutParams (attrs : AttributeSet ? ): LayoutParams {
244+ return MarginLayoutParams (context, attrs)
245+ }
246+
247+ override fun generateDefaultLayoutParams (): LayoutParams {
248+ return MarginLayoutParams (LayoutParams .WRAP_CONTENT , LayoutParams .WRAP_CONTENT )
249+ }
250+
251+ override fun generateLayoutParams (p : LayoutParams ? ): LayoutParams {
252+ return MarginLayoutParams (p)
253+ }
254+
255+ interface OnItemClickListener {
256+ fun onItemClick (position : Int , view : View )
257+ }
258+
259+ interface OnItemLongClickListener {
260+ fun onItemLongClick (position : Int , view : View )
261+ }
262+
263+ interface OnSingleCheckedChangeListener {
264+ fun onCheckedChanged (position : Int )
265+ }
266+
267+ interface OnMultipleCheckedChangeListener {
268+ fun onCheckedChanged (positionList : MutableList <Int >)
269+ }
270+
255271 enum class ChoiceMode (var choiceMode : Int ) {
272+ /* *
273+ * 非选择模式
274+ */
256275 None (0 ),
276+
277+ /* *
278+ * 单选模式
279+ */
257280 SingleChoice (1 ),
281+
282+ /* *
283+ * 多选模式
284+ */
258285 MultipleChoice (2 );
259286 }
260287}
0 commit comments