summaryrefslogtreecommitdiffhomepage
path: root/app/src/main/java/com/wireguard/android/BindingAdapters.java
blob: aa5b8c1ec0fced2eae4939a18fa13fd4687fb85e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package com.wireguard.android;

import android.databinding.BindingAdapter;
import android.databinding.ObservableList;
import android.widget.ListView;

/**
 * Static methods for use by generated code in the Android data binding library.
 */

public final class BindingAdapters {
    @BindingAdapter({"items", "layout"})
    public static <T> void listBinding(ListView view, ObservableList<T> oldList, int oldLayoutId,
                                       ObservableList<T> newList, int newLayoutId) {
        // Remove any existing binding when there is no new list.
        if (newList == null) {
            view.setAdapter(null);
            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)
            adapter = null;
        // Add a new binding if there was none, or if it must be replaced due to a layout change.
        if (adapter == null) {
            adapter = new ObservableListAdapter<>(view.getContext(), newLayoutId, newList);
            view.setAdapter(adapter);
        } else if (newList != oldList) {
            // Changing the list only requires modifying the existing adapter.
            adapter.setList(newList);
        }
    }

    private BindingAdapters() {
    }
}