ListGroupPreference: Overrideable "add" button

This commit is contained in:
Jules Aguillon 2023-07-30 18:01:25 +02:00
parent 0856fb4e31
commit b4a1ac48bb

View File

@ -19,6 +19,7 @@ public abstract class ListGroupPreference extends PreferenceGroup
{ {
boolean _attached = false; boolean _attached = false;
List<String> _values; List<String> _values;
AddButton _prev_add_button;
public ListGroupPreference(Context context, AttributeSet attrs) public ListGroupPreference(Context context, AttributeSet attrs)
{ {
@ -36,6 +37,16 @@ public abstract class ListGroupPreference extends PreferenceGroup
return value; return value;
} }
/** Called every time the list changes and allows to change the "Add" button
appearance.
[prev_btn] is the previously attached button, might be null. */
AddButton on_attach_add_button(AddButton prev_btn)
{
if (prev_btn == null)
return new AddButton(getContext());
return prev_btn;
}
/** Called when an item is added or modified. Returns [null] to cancel the /** Called when an item is added or modified. Returns [null] to cancel the
action. */ action. */
abstract void select(SelectionCallback callback); abstract void select(SelectionCallback callback);
@ -125,6 +136,8 @@ public abstract class ListGroupPreference extends PreferenceGroup
void reattach() void reattach()
{ {
if (!_attached)
return;
removeAll(); removeAll();
int i = 0; int i = 0;
for (String v : _values) for (String v : _values)
@ -134,7 +147,9 @@ public abstract class ListGroupPreference extends PreferenceGroup
addPreference(item); addPreference(item);
i++; i++;
} }
addPreference(this.new AddButton(getContext())); _prev_add_button = on_attach_add_button(_prev_add_button);
_prev_add_button.setOrder(Preference.DEFAULT_ORDER);
addPreference(_prev_add_button);
} }
class Item extends Preference class Item extends Preference
@ -165,7 +180,7 @@ public abstract class ListGroupPreference extends PreferenceGroup
} }
} }
final class AddButton extends Preference class AddButton extends Preference
{ {
public AddButton(Context ctx) public AddButton(Context ctx)
{ {