Bindings: Merge bugfixes and updates
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
@@ -28,6 +28,8 @@ public final class BindingAdapters {
|
||||
if (listener != null && oldList != null && oldLayoutId != newLayoutId) {
|
||||
listener.setList(null);
|
||||
listener = null;
|
||||
// Stop tracking the old listener.
|
||||
ListenerUtil.trackListener(view, null, R.id.item_change_listener);
|
||||
}
|
||||
// Avoid adding a listener when there is no new list or layout.
|
||||
if (newList == null || newLayoutId == 0)
|
||||
@@ -44,49 +46,52 @@ public final class BindingAdapters {
|
||||
public static <T> void setItems(final ListView view,
|
||||
final ObservableList<T> oldList, final int oldLayoutId,
|
||||
final ObservableList<T> newList, final int newLayoutId) {
|
||||
// Remove any existing binding when there is no new list or layout.
|
||||
if (newList == null || newLayoutId == 0) {
|
||||
view.setAdapter(null);
|
||||
if (oldList == newList && oldLayoutId == newLayoutId)
|
||||
return;
|
||||
}
|
||||
// The ListAdapter interface is not generic, so this cannot be checked.
|
||||
@SuppressWarnings("unchecked")
|
||||
ObservableListAdapter<T> adapter = (ObservableListAdapter<T>) view.getAdapter();
|
||||
// If the layout changes, any existing adapter must be replaced.
|
||||
if (newLayoutId != oldLayoutId)
|
||||
if (adapter != null && oldList != null && oldLayoutId != newLayoutId) {
|
||||
adapter.setList(null);
|
||||
adapter = null;
|
||||
// Add a new binding if there was none, or if it must be replaced due to a layout change.
|
||||
if (adapter == null) {
|
||||
view.setAdapter(new ObservableListAdapter<>(view.getContext(), newLayoutId, newList));
|
||||
} else if (newList != oldList) {
|
||||
// Changing the list only requires modifying the existing adapter.
|
||||
adapter.setList(newList);
|
||||
}
|
||||
// Avoid setting an adapter when there is no new list or layout.
|
||||
if (newList == null || newLayoutId == 0)
|
||||
return;
|
||||
if (adapter == null) {
|
||||
adapter = new ObservableListAdapter<>(view.getContext(), newLayoutId, newList);
|
||||
view.setAdapter(adapter);
|
||||
}
|
||||
// Either the list changed, or this is an entirely new listener because the layout changed.
|
||||
adapter.setList(newList);
|
||||
}
|
||||
|
||||
@BindingAdapter({"items", "layout"})
|
||||
public static <K extends Comparable<K>, V> void setItems(
|
||||
final ListView view,
|
||||
final ObservableSortedMap<K, V> oldMap, final int oldLayoutId,
|
||||
final ObservableSortedMap<K, V> newMap, final int newLayoutId) {
|
||||
// Remove any existing binding when there is no new map or layout.
|
||||
if (newMap == null || newLayoutId == 0) {
|
||||
view.setAdapter(null);
|
||||
public static <K extends Comparable<K>, V> void setItems(final ListView view,
|
||||
final ObservableSortedMap<K, V> oldMap,
|
||||
final int oldLayoutId,
|
||||
final ObservableSortedMap<K, V> newMap,
|
||||
final int newLayoutId) {
|
||||
if (oldMap == newMap && oldLayoutId == newLayoutId)
|
||||
return;
|
||||
}
|
||||
// The ListAdapter interface is not generic, so this cannot be checked.
|
||||
@SuppressWarnings("unchecked")
|
||||
ObservableMapAdapter<K, V> adapter = (ObservableMapAdapter<K, V>) view.getAdapter();
|
||||
// If the layout changes, any existing adapter must be replaced.
|
||||
if (newLayoutId != oldLayoutId)
|
||||
if (adapter != null && oldMap != null && oldLayoutId != newLayoutId) {
|
||||
adapter.setMap(null);
|
||||
adapter = null;
|
||||
// Add a new binding if there was none, or if it must be replaced due to a layout change.
|
||||
if (adapter == null) {
|
||||
view.setAdapter(new ObservableMapAdapter<>(view.getContext(), newLayoutId, newMap));
|
||||
} else if (newMap != oldMap) {
|
||||
// Changing the list only requires modifying the existing adapter.
|
||||
adapter.setMap(newMap);
|
||||
}
|
||||
// Avoid setting an adapter when there is no new list or layout.
|
||||
if (newMap == null || newLayoutId == 0)
|
||||
return;
|
||||
if (adapter == null) {
|
||||
adapter = new ObservableMapAdapter<>(view.getContext(), newLayoutId, newMap);
|
||||
view.setAdapter(adapter);
|
||||
}
|
||||
// Either the list changed, or this is an entirely new listener because the layout changed.
|
||||
adapter.setMap(newMap);
|
||||
}
|
||||
|
||||
@BindingAdapter({"filter"})
|
||||
|
||||
Reference in New Issue
Block a user