Skip to content
This repository was archived by the owner on Oct 18, 2024. It is now read-only.

Commit 25272b3

Browse files
committed
Added attribute adapter for ListView (closes #145)
1 parent fd70bd7 commit 25272b3

File tree

2 files changed

+149
-0
lines changed

2 files changed

+149
-0
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* This file is part of AndroidIDE.
3+
*
4+
* AndroidIDE is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* AndroidIDE is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with AndroidIDE. If not, see <https://www.gnu.org/licenses/>.
16+
*/
17+
18+
package com.itsaky.inflater.adapters.android.widget
19+
20+
import android.util.DisplayMetrics
21+
import android.view.View
22+
import android.widget.AbsListView
23+
import com.itsaky.inflater.IAttribute
24+
import com.itsaky.inflater.IResourceTable
25+
26+
/**
27+
* Attribute adapter for the [AbsListView] widget.
28+
*
29+
* @author Akash Yadav
30+
*/
31+
open class AbsListViewAttrAdapter(resourceTable: IResourceTable, displayMetrics: DisplayMetrics) :
32+
AdapterViewAttrAdapter(resourceTable, displayMetrics) {
33+
34+
override fun isApplicableTo(view: View?): Boolean {
35+
return view is AbsListView
36+
}
37+
38+
override fun apply(attribute: IAttribute, view: View): Boolean {
39+
val list = view as AbsListView
40+
val context = list.context
41+
val value = attribute.value
42+
43+
if (!canHandleNamespace(attribute)) {
44+
return false
45+
}
46+
47+
var handled = true
48+
when (attribute.attributeName) {
49+
"cacheColorHint" -> list.cacheColorHint = parseColor(value, context)
50+
"choiceMode" -> list.choiceMode = parseChoiceMode(value)
51+
"drawSelectorOnTop" -> list.isDrawSelectorOnTop = parseBoolean(value)
52+
"fastScrollEnabled" -> list.isFastScrollEnabled = parseBoolean(value)
53+
"listSelector" -> list.selector = parseDrawable(value, context)
54+
"smoothScrollbar" -> list.isSmoothScrollbarEnabled = parseBoolean(value)
55+
"stackFromBottom" -> list.isStackFromBottom = parseBoolean(value)
56+
"textFilterEnabled" -> list.isTextFilterEnabled = parseBoolean(value)
57+
"transcriptMode" -> list.transcriptMode = parseTranscriptMode(value)
58+
else -> handled = false
59+
}
60+
61+
if (!handled) {
62+
handled = super.apply(attribute, view)
63+
}
64+
65+
return handled
66+
}
67+
68+
protected open fun parseTranscriptMode(value: String): Int {
69+
return when (value) {
70+
"normal" -> AbsListView.TRANSCRIPT_MODE_NORMAL
71+
"alwaysScroll" -> AbsListView.TRANSCRIPT_MODE_ALWAYS_SCROLL
72+
else -> AbsListView.TRANSCRIPT_MODE_DISABLED
73+
}
74+
}
75+
76+
protected open fun parseChoiceMode(value: String): Int {
77+
return when (value) {
78+
"multipleChoice" -> AbsListView.CHOICE_MODE_MULTIPLE
79+
"multipleChoiceModal" -> AbsListView.CHOICE_MODE_MULTIPLE_MODAL
80+
"singleChoice" -> AbsListView.CHOICE_MODE_SINGLE
81+
else -> AbsListView.CHOICE_MODE_NONE
82+
}
83+
}
84+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* This file is part of AndroidIDE.
3+
*
4+
* AndroidIDE is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* AndroidIDE is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with AndroidIDE. If not, see <https://www.gnu.org/licenses/>.
16+
*/
17+
18+
package com.itsaky.inflater.adapters.android.widget
19+
20+
import android.util.DisplayMetrics
21+
import android.view.View
22+
import android.widget.ArrayAdapter
23+
import android.widget.ListView
24+
import com.blankj.utilcode.util.SizeUtils
25+
import com.itsaky.inflater.IAttribute
26+
import com.itsaky.inflater.IResourceTable
27+
28+
/** @author Akash Yadav */
29+
class ListViewAttrAdapter(resourceTable: IResourceTable, displayMetrics: DisplayMetrics) :
30+
AbsListViewAttrAdapter(resourceTable, displayMetrics) {
31+
32+
override fun isApplicableTo(view: View?): Boolean {
33+
return view is ListView
34+
}
35+
36+
override fun apply(attribute: IAttribute, view: View): Boolean {
37+
val list = view as ListView
38+
val value = attribute.value
39+
val context = list.context
40+
41+
if (!canHandleNamespace(attribute)) {
42+
return false
43+
}
44+
45+
var handled = true
46+
47+
when (attribute.attributeName) {
48+
"divider" -> list.divider = parseDrawable(value, context)
49+
"dividerHeight" ->
50+
list.dividerHeight = parseDimension(value, SizeUtils.dp2px(1f), displayMetrics)
51+
"entries" -> {
52+
val entries = parseArray(value)
53+
list.adapter = ArrayAdapter(context, android.R.layout.simple_list_item_1, entries)
54+
}
55+
"footerDividersEnabled" -> list.setFooterDividersEnabled(parseBoolean(value))
56+
"headerDividersEnabled" -> list.setHeaderDividersEnabled(parseBoolean(value))
57+
}
58+
59+
if (!handled) {
60+
handled = super.apply(attribute, view)
61+
}
62+
63+
return handled
64+
}
65+
}

0 commit comments

Comments
 (0)