Java Code Examples for android.databinding.ObservableList

The following examples show how to use android.databinding.ObservableList. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: views-widgets-samples   Source File: ListBindingAdapters.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void onItemRangeChanged(ObservableList observableList,
        int start, int count) {
    if (mLayoutId == 0) {
        return;
    }
    LayoutInflater inflater = (LayoutInflater) mTarget.getContext()
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    startTransition(mTarget);
    final int end = start + count;
    for (int i = start; i < end; i++) {
        Object data = observableList.get(i);
        ViewDataBinding binding = bindLayout(inflater,
                mTarget, mLayoutId, data);
        binding.setVariable(BR.data, observableList.get(i));
        mTarget.removeViewAt(i);
        mTarget.addView(binding.getRoot(), i);
    }
}
 
Example 2
Source Project: views-widgets-samples   Source File: ListBindingAdapters.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void onItemRangeInserted(ObservableList observableList,
        int start, int count) {
    if (mLayoutId == 0) {
        return;
    }
    startTransition(mTarget);
    final int end = start + count;
    LayoutInflater inflater = (LayoutInflater) mTarget.getContext()
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    for (int i = end - 1; i >= start; i--) {
        Object entry = observableList.get(i);
        ViewDataBinding binding =
                bindLayout(inflater, mTarget, mLayoutId, entry);
        mTarget.addView(binding.getRoot(), start);
    }
}
 
Example 3
Source Project: spline   Source File: LayerListAdapter.java    License: Apache License 2.0 6 votes vote down vote up
public ObservableList<Layer> getTwirledDownLayersForGroup(LayerGroup root) {
    ObservableList<Layer> twirledDownLayers = new ObservableArrayList<>();

    for (Layer l : root.getLayers()) {
        twirledDownLayers.add(l);
        if (l instanceof LayerGroup) {
            LayerGroup group = (LayerGroup) l;
            if (group.isTwirledDown()) {
                int i = twirledDownLayers.indexOf(l);
                List<Layer> childLayers = getTwirledDownLayersForGroup(group);
                twirledDownLayers.addAll(i + 1, childLayers);
            }
        }
    }

    return twirledDownLayers;
}
 
Example 4
@Override
public void onItemRangeChanged(ObservableList observableList,
        int start, int count) {
    if (mLayoutId == 0) {
        return;
    }
    LayoutInflater inflater = (LayoutInflater) mTarget.getContext()
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    startTransition(mTarget);
    final int end = start + count;
    for (int i = start; i < end; i++) {
        Object data = observableList.get(i);
        ViewDataBinding binding = bindLayout(inflater,
                mTarget, mLayoutId, data);
        binding.setVariable(BR.data, observableList.get(i));
        mTarget.removeViewAt(i);
        mTarget.addView(binding.getRoot(), i);
    }
}
 
Example 5
@Override
public void onItemRangeInserted(ObservableList observableList,
        int start, int count) {
    if (mLayoutId == 0) {
        return;
    }
    startTransition(mTarget);
    final int end = start + count;
    LayoutInflater inflater = (LayoutInflater) mTarget.getContext()
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    for (int i = end - 1; i >= start; i--) {
        Object entry = observableList.get(i);
        ViewDataBinding binding =
                bindLayout(inflater, mTarget, mLayoutId, entry);
        mTarget.addView(binding.getRoot(), start);
    }
}
 
Example 6
Source Project: views-widgets-samples   Source File: ListBindingAdapters.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Assign a list of items to a ViewGroup. This is used with the {@code entries} and
 * {@code layout} attributes in the application namespace. Example Usage:
 * <pre><code>&lt;LinearLayout
 *     android:layout_width="match_parent"
 *     android:layout_height="wrap_content"
 *     android:orientation="vertical"
 *     app:entries="@{items}"
 *     app:layout="@{@layout/item}"/&gt;
 * </code></pre>
 * <p>
 * In the above, {@code items} is a List or ObservableList. {@code layout} does not
 * need to be hard-coded, but most commonly will be. This BindingAdapter will work
 * with any ViewGroup that only needs addView() and removeView() to manage its Views.
 * <p>
 * The layout, &commat;layout/item for example, must have a single variable named
 * {@code data}.
 */
@BindingAdapter({"entries", "layout"})
public static <T> void setEntries(ViewGroup viewGroup,
        List<T> oldEntries, int oldLayoutId,
        List<T> newEntries, int newLayoutId) {
    if (oldEntries == newEntries && oldLayoutId == newLayoutId) {
        return; // nothing has changed
    }

    EntryChangeListener listener =
            ListenerUtil.getListener(viewGroup, R.id.entryListener);
    if (oldEntries != newEntries && listener != null && oldEntries instanceof ObservableList) {
        ((ObservableList)oldEntries).removeOnListChangedCallback(listener);
    }

    if (newEntries == null) {
        viewGroup.removeAllViews();
    } else {
        if (newEntries instanceof ObservableList) {
            if (listener == null) {
                listener =
                        new EntryChangeListener(viewGroup, newLayoutId);
                ListenerUtil.trackListener(viewGroup, listener,
                        R.id.entryListener);
            } else {
                listener.setLayoutId(newLayoutId);
            }
            if (newEntries != oldEntries) {
                ((ObservableList)newEntries).addOnListChangedCallback(listener);
            }
        }
        resetViews(viewGroup, newLayoutId, newEntries);
    }
}
 
Example 7
Source Project: views-widgets-samples   Source File: ListBindingAdapters.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void onItemRangeMoved(ObservableList observableList,
        int from, int to, int count) {
    if (mLayoutId == 0) {
        return;
    }
    startTransition(mTarget);
    for (int i = 0; i < count; i++) {
        View view = mTarget.getChildAt(from);
        mTarget.removeViewAt(from);
        int destination = (from > to) ? to + i : to;
        mTarget.addView(view, destination);
    }
}
 
Example 8
Source Project: views-widgets-samples   Source File: ListBindingAdapters.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void onItemRangeRemoved(ObservableList observableList,
        int start, int count) {
    if (mLayoutId == 0) {
        return;
    }
    startTransition(mTarget);
    for (int i = 0; i < count; i++) {
        mTarget.removeViewAt(start);
    }
}
 
Example 9
@Override
public void onItemRangeChanged(final ObservableList<ItemTypeT> items,
        final int start, final int count) {
    mainHandler.post(() -> {
        for (int i = start; i < start + count; ++i) {
            adapterItems.set(i, items.get(i));
        }

        notifyItemRangeChanged(getItemLayoutPosition(start), count);
    });
}
 
Example 10
@Override
public void onItemRangeInserted(final ObservableList<ItemTypeT> items,
        final int start, final int count) {
    mainHandler.post(() -> {
        for (int i = start + count - 1; i >= start; --i) {
            adapterItems.add(start, items.get(i));
        }

        notifyItemRangeInserted(getItemLayoutPosition(start), count);
    });
}
 
Example 11
@Override
public void onItemRangeMoved(ObservableList sender, int fromPosition, int toPosition, int itemCount)
{
    RecyclerView.Adapter adapter = adapterReference.get();
    if (adapter != null)
    {
        adapter.notifyItemMoved(fromPosition, toPosition);
    }
}
 
Example 12
@Override
public void onItemRangeInserted(ObservableList sender, int positionStart, int itemCount)
{
    RecyclerView.Adapter adapter = adapterReference.get();
    if (adapter != null)
    {
        adapter.notifyItemRangeInserted(positionStart, itemCount);
    }
}
 
Example 13
/**
 * Binding "messageSource" to display a message.
 *
 * @param view the RecyclerView which binds the item list.
 * @param itemList the list of items to be bound to the view.
 * @param itemBrId the data binding BR ID for the variable to be bound to the item.
 * @param itemLayoutId the layout ID for list items.
 */
@BindingAdapter({"items", "itemBrId", "itemLayout"})
public static <ItemTypeT> void setItems(final RecyclerView view, final ObservableList<ItemTypeT> itemList,
        final int itemBrId, @LayoutRes final int itemLayoutId) {
    if (itemList == null) {
        view.setAdapter(null);
    } else {
        final RecyclerViewDatabindingAdapter<ItemTypeT> adapter =
                new RecyclerViewDatabindingAdapter<>(itemList, itemBrId, itemLayoutId);
        view.setAdapter(adapter);
    }
}
 
Example 14
/**
 * Binding "messageSource" to display a message.
 *
 * @param view the RecyclerView which binds the item list.
 * @param itemList the list of items to be bound to the view.
 * @param itemBrId the data binding BR ID for the variable to be bound to the item.
 * @param itemLayoutId the layout ID for list items.
 * @param headerData the object bound to the header.
 * @param headerLayoutId the layout ID for list header.
 */
@BindingAdapter({"items", "itemBrId", "itemLayout", "headerData", "headerBrId", "headerLayout"})
public static <ItemTypeT> void setItemsAndHeader(final RecyclerView view, final ObservableList<ItemTypeT> itemList,
        final int itemBrId, @LayoutRes final int itemLayoutId,
        @NonNull final Object headerData, final int headerBrId, @LayoutRes final int headerLayoutId) {
    if (itemList == null) {
        view.setAdapter(null);
    } else {
        final HeaderedRecyclerViewDatabindingAdapter.HeaderParams headerParams =
                new HeaderedRecyclerViewDatabindingAdapter.HeaderParams(headerLayoutId, headerBrId, headerData);
        final HeaderedRecyclerViewDatabindingAdapter<ItemTypeT> adapter =
                new HeaderedRecyclerViewDatabindingAdapter<>(itemList, itemBrId, itemLayoutId, headerParams);
        view.setAdapter(adapter);
    }
}
 
Example 15
/**
 * Binding "visibility" to the presence of the movie view models.
 *
 * @param view the View to set visibility
 * @param movies movies view model
 */
@BindingAdapter({"visibility"})
public static void setVisibilityByListSize(final View view, final ObservableList<MovieViewModel> movies) {
    if (movies.size() > 0) {
        view.setVisibility(View.VISIBLE);
    } else {
        view.setVisibility(View.GONE);
    }
}
 
Example 16
@Override
public void onItemRangeChanged(ObservableList sender, int positionStart, int itemCount)
{
    RecyclerView.Adapter adapter = adapterReference.get();
    if (adapter != null)
    {
        adapter.notifyItemRangeChanged(positionStart, itemCount);
    }
}
 
Example 17
public void setItems(@Nullable Collection<T> items)
{
    if (this.items == items)
    {
        return;
    }

    if (this.items != null)
    {
        this.items.removeOnListChangedCallback(onListChangedCallback);
        notifyItemRangeRemoved(0, this.items.size());
    }

    if (items instanceof ObservableList)
    {
        this.items = (ObservableList<T>) items;
        notifyItemRangeInserted(0, this.items.size());
        this.items.addOnListChangedCallback(onListChangedCallback);
    }
    else if (items != null)
    {
        this.items = new ObservableArrayList<>();
        this.items.addOnListChangedCallback(onListChangedCallback);
        this.items.addAll(items);
    }
    else
    {
        this.items = null;
    }
}
 
Example 18
Source Project: spline   Source File: DocumentView.java    License: Apache License 2.0 5 votes vote down vote up
private void addPropertyChangedCallbacks(List<Layer> layers, int start, int end) {
    for (int i = start; i < end; i++) {
        Layer l = layers.get(i);
        l.addOnPropertyChangedCallback(mOnPropertyChangedCallback);
        if (l instanceof LayerGroup) {
            LayerGroup lg = (LayerGroup) l;
            ObservableList<Layer> childLayers = lg.getLayers();
            // Add list listener for future changes to the layer group's list of children
            childLayers.addOnListChangedCallback(mOnListChangedCallback);

            // Recursive call to add property listeners to each child layer
            addPropertyChangedCallbacks(childLayers);
        }
    }
}
 
Example 19
Source Project: spline   Source File: ListFragment.java    License: Apache License 2.0 5 votes vote down vote up
private void updateList() {
    PickerViewModel viewModel = PickerViewModel.getInstance();
    ObservableList<File> files = viewModel.getFiles();

    if (mAdapter == null) {
        mAdapter = new FileAdapter();
        mList.setAdapter(mAdapter);
    }

    mAdapter.setFiles(files);
    mBinding.setViewModel(viewModel);
}
 
Example 20
Source Project: spline   Source File: ListFragment.java    License: Apache License 2.0 5 votes vote down vote up
public FileAdapter() {
    mOnListChangedCallback = new ObservableList
            .OnListChangedCallback<ObservableList<Layer>>() {
        @Override
        public void onChanged(ObservableList<Layer> layers) {

        }

        @Override
        public void onItemRangeChanged(ObservableList<Layer> layers, int i, int i1) {

        }

        @Override
        public void onItemRangeInserted(ObservableList<Layer> layers, int start, int
                count) {
            notifyItemRangeInserted(start, count);
        }

        @Override
        public void onItemRangeMoved(ObservableList<Layer> sender, int fromPosition, int
                toPosition, int itemCount) {

        }

        @Override
        public void onItemRangeRemoved(ObservableList<Layer> sender, int positionStart,
                                       int itemCount) {

        }
    };
}
 
Example 21
@Override
public void onChanged(ObservableList sender)
{
    RecyclerView.Adapter adapter = adapterReference.get();
    if (adapter != null)
    {
        adapter.notifyDataSetChanged();
    }
}
 
Example 22
Source Project: spline   Source File: LayerListAdapter.java    License: Apache License 2.0 5 votes vote down vote up
private void addListChangedCallbacks(List<Layer> layers, int start, int end) {
    for (int i = start; i < end; i++) {
        Layer l = layers.get(i);
        if (l instanceof LayerGroup) {
            LayerGroup lg = (LayerGroup) l;
            ObservableList<Layer> childLayers = lg.getLayers();
            // Add list listener for future changes to the layer group's list of children
            childLayers.addOnListChangedCallback(mOnListChangedCallback);
            // Recursive call to add property listeners to each child layer
            addListChangedCallbacks(childLayers);
        }
    }
}
 
Example 23
Source Project: Studio   Source File: BindingRecyclerView.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void onItemRangeMoved(ObservableList<T> sender,
                             int fromPosition, int toPosition, int itemCount) {
    for (int i = 0; i < itemCount; i++) {
        notifyItemMoved(fromPosition + i, toPosition + i);
    }
}
 
Example 24
Source Project: SimpleFTP   Source File: BindingRecyclerViewAdapter.java    License: MIT License 5 votes vote down vote up
/**
 * Sets the list of items to display.
 * @param items The list of items.
 */
public void setItems(@Nullable Collection<T> items)
{
    if (this.items == items)
    {
        return;
    }

    if (this.items != null)
    {
        this.items.removeOnListChangedCallback(onListChangedCallback);
        notifyItemRangeRemoved(0, this.items.size());
    }

    if (items instanceof ObservableList)
    {
        this.items = (ObservableList<T>) items;
        notifyItemRangeInserted(0, this.items.size());
        this.items.addOnListChangedCallback(onListChangedCallback);
    }
    else if (items != null)
    {
        this.items = new ObservableArrayList<>();
        this.items.addOnListChangedCallback(onListChangedCallback);
        this.items.addAll(items);
    }
    else
    {
        this.items = null;
    }
}
 
Example 25
Source Project: SimpleFTP   Source File: BindingRecyclerViewAdapter.java    License: MIT License 5 votes vote down vote up
/**
 * Called when the list changed
 * @param sender The current list.
 */
@Override
public void onChanged(ObservableList sender)
{
    RecyclerView.Adapter adapter = adapterReference.get();
    if (adapter != null)
    {
        adapter.notifyDataSetChanged();
    }
}
 
Example 26
Source Project: SimpleFTP   Source File: BindingRecyclerViewAdapter.java    License: MIT License 5 votes vote down vote up
/**
 * Called when an item range changed in the list.
 * @param sender The current list.
 * @param positionStart The position of the first item modified.
 * @param itemCount The number of items modified.
 */
@Override
public void onItemRangeChanged(ObservableList sender, int positionStart, int itemCount)
{
    RecyclerView.Adapter adapter = adapterReference.get();
    if (adapter != null)
    {
        adapter.notifyItemRangeChanged(positionStart, itemCount);
    }
}
 
Example 27
Source Project: SimpleFTP   Source File: BindingRecyclerViewAdapter.java    License: MIT License 5 votes vote down vote up
/**
 * Called when an item range is inserted in the list.
 * @param sender The current list.
 * @param positionStart The position of the first item inserted.
 * @param itemCount The number of items inserted.
 */
@Override
public void onItemRangeInserted(ObservableList sender, int positionStart, int itemCount)
{
    RecyclerView.Adapter adapter = adapterReference.get();
    if (adapter != null)
    {
        adapter.notifyItemRangeInserted(positionStart, itemCount);
    }
}
 
Example 28
Source Project: SimpleFTP   Source File: BindingRecyclerViewAdapter.java    License: MIT License 5 votes vote down vote up
/**
 * Called when an item range is removed from the list.
 * @param sender The current list.
 * @param positionStart The position of the first item removed.
 * @param itemCount The number of items removed.
 */
@Override
public void onItemRangeRemoved(ObservableList sender, int positionStart, int itemCount)
{
    RecyclerView.Adapter adapter = adapterReference.get();
    if (adapter != null)
    {
        adapter.notifyItemRangeRemoved(positionStart, itemCount);
    }
}
 
Example 29
/**
 * Assign a list of items to a ViewGroup. This is used with the {@code entries} and
 * {@code layout} attributes in the application namespace. Example Usage:
 * <pre><code>&lt;LinearLayout
 *     android:layout_width="match_parent"
 *     android:layout_height="wrap_content"
 *     android:orientation="vertical"
 *     app:entries="@{items}"
 *     app:layout="@{@layout/item}"/&gt;
 * </code></pre>
 * <p>
 * In the above, {@code items} is a List or ObservableList. {@code layout} does not
 * need to be hard-coded, but most commonly will be. This BindingAdapter will work
 * with any ViewGroup that only needs addView() and removeView() to manage its Views.
 * <p>
 * The layout, &commat;layout/item for example, must have a single variable named
 * {@code data}.
 */
@BindingAdapter({"entries", "layout"})
public static <T> void setEntries(ViewGroup viewGroup,
        List<T> oldEntries, int oldLayoutId,
        List<T> newEntries, int newLayoutId) {
    if (oldEntries == newEntries && oldLayoutId == newLayoutId) {
        return; // nothing has changed
    }

    EntryChangeListener listener =
            ListenerUtil.getListener(viewGroup, R.id.entryListener);
    if (oldEntries != newEntries && listener != null && oldEntries instanceof ObservableList) {
        ((ObservableList)oldEntries).removeOnListChangedCallback(listener);
    }

    if (newEntries == null) {
        viewGroup.removeAllViews();
    } else {
        if (newEntries instanceof ObservableList) {
            if (listener == null) {
                listener =
                        new EntryChangeListener(viewGroup, newLayoutId);
                ListenerUtil.trackListener(viewGroup, listener,
                        R.id.entryListener);
            } else {
                listener.setLayoutId(newLayoutId);
            }
            if (newEntries != oldEntries) {
                ((ObservableList)newEntries).addOnListChangedCallback(listener);
            }
        }
        resetViews(viewGroup, newLayoutId, newEntries);
    }
}
 
Example 30
@Override
public void onItemRangeMoved(ObservableList observableList,
        int from, int to, int count) {
    if (mLayoutId == 0) {
        return;
    }
    startTransition(mTarget);
    for (int i = 0; i < count; i++) {
        View view = mTarget.getChildAt(from);
        mTarget.removeViewAt(from);
        int destination = (from > to) ? to + i : to;
        mTarget.addView(view, destination);
    }
}