ListGroupPreference: Allow hide the "remove" button

This commit is contained in:
Jules Aguillon 2023-07-30 19:48:54 +02:00
parent b4a1ac48bb
commit 818aa4c7d5

View File

@ -19,7 +19,8 @@ public abstract class ListGroupPreference extends PreferenceGroup
{ {
boolean _attached = false; boolean _attached = false;
List<String> _values; List<String> _values;
AddButton _prev_add_button; /** The "add" button currently displayed. */
AddButton _add_button = null;
public ListGroupPreference(Context context, AttributeSet attrs) public ListGroupPreference(Context context, AttributeSet attrs)
{ {
@ -47,6 +48,14 @@ public abstract class ListGroupPreference extends PreferenceGroup
return prev_btn; return prev_btn;
} }
/** Called every time the list changes and allows to disable the "Remove"
buttons on every items. Might be used to enforce a minimum number of
items. */
boolean should_allow_remove_item()
{
return true;
}
/** 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);
@ -139,28 +148,30 @@ public abstract class ListGroupPreference extends PreferenceGroup
if (!_attached) if (!_attached)
return; return;
removeAll(); removeAll();
boolean allow_remove_item = should_allow_remove_item();
int i = 0; int i = 0;
for (String v : _values) for (String v : _values)
{ {
Item item = this.new Item(getContext(), v); Item item = this.new Item(getContext(), v, allow_remove_item);
item.setTitle(label_of_value(v, i)); item.setTitle(label_of_value(v, i));
addPreference(item); addPreference(item);
i++; i++;
} }
_prev_add_button = on_attach_add_button(_prev_add_button); _add_button = on_attach_add_button(_add_button);
_prev_add_button.setOrder(Preference.DEFAULT_ORDER); _add_button.setOrder(Preference.DEFAULT_ORDER);
addPreference(_prev_add_button); addPreference(_add_button);
} }
class Item extends Preference class Item extends Preference
{ {
final String _value; final String _value;
public Item(Context ctx, String value) public Item(Context ctx, String value, boolean allow_remove)
{ {
super(ctx); super(ctx);
_value = value; _value = value;
setPersistent(false); setPersistent(false);
if (allow_remove)
setWidgetLayoutResource(R.layout.pref_listgroup_item_widget); setWidgetLayoutResource(R.layout.pref_listgroup_item_widget);
} }
@ -168,8 +179,9 @@ public abstract class ListGroupPreference extends PreferenceGroup
protected View onCreateView(ViewGroup parent) protected View onCreateView(ViewGroup parent)
{ {
View v = super.onCreateView(parent); View v = super.onCreateView(parent);
v.findViewById(R.id.pref_listgroup_remove_btn) View remove_btn = v.findViewById(R.id.pref_listgroup_remove_btn);
.setOnClickListener(new View.OnClickListener() { if (remove_btn != null)
remove_btn.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View _v) public void onClick(View _v)
{ {