public void setupSelectionCallback(){ try { if (removeSelectionCallback != null) { removeSelectionCallback.close(); } if(selectedItems!=null) { selectedItems.addOnListChangedCallback(selectionCallback); ObservableList old = selectedItems; removeSelectionCallback = new ClosableAction(() -> { old.removeOnListChangedCallback(selectionCallback); }); }else{ removeSelectionCallback = null; } }catch (Exception ex){ ex.printStackTrace(); } }
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; }
@BindingAdapter({ "bind:items", "bind:itemLayout", "bind:itemBindingId", "bind:spanCount", "bind:layoutType", "bind:orientation"}) public static void createAdapterStaggeredGrid(final RecyclerView list, ObservableList items, int itemLayout, int itemBindingId, int spanCount, @LayoutManagerType.LayoutType int layoutType, int orientation) { if (null == list.getAdapter()) { SimpleBindingListAdapter adapter = new SimpleBindingListAdapter(items, itemBindingId, itemLayout); RecyclerView.LayoutManager layoutManager = createLayoutManager(list, spanCount, layoutType, orientation); list.setLayoutManager(layoutManager); list.setAdapter(adapter); } }
@Test @SuppressWarnings("unchecked") public void multipleReplaceButFilteredAll() { FilteredReadOnlyObservableList<String> list = new FilteredReadOnlyObservableList<>(rule.getSource(), new Filter<String>() { @Override public boolean execute(String element) { return !element.contains("2") && !element.contains("3"); } }); rule.setUpList(list); rule.getSource().setAll(1, Arrays.asList("element2-2", "element3-2")); verify(rule.getCallback(), never()) .onItemRangeChanged((ObservableList) any(), anyInt(), anyInt()); assertThat(list.size(), is(3)); }
@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); } }
@Test @SuppressWarnings("unchecked") public void moveButFilteredAll() { FilteredReadOnlyObservableList<String> list = new FilteredReadOnlyObservableList<>(rule.getSource(), new Filter<String>() { @Override public boolean execute(String element) { return !element.contains("2") && !element.contains("3"); } }); rule.setUpList(list); rule.getSource().move(1, 0, 2); verify(rule.getCallback(), never()) .onItemRangeRemoved((ObservableList) any(), anyInt(), anyInt()); verify(rule.getCallback(), never()) .onItemRangeInserted((ObservableList) any(), anyInt(), anyInt()); assertThat(list.get(0), is("element1")); assertThat(list.get(1), is("element4")); }
@Test public void computableListElementsAreComputable() throws Exception { String string1 = "STRING 1"; String string2 = "STRING 2"; StringClass stringClass = new StringClass(string1); ObservableList<StringClass> strings = new ObservableArrayList<>(); strings.add(0, stringClass); ObservableField<String> computedString = Cinder.computeField(()->strings.get(0).element.get(), Cinder.observable(strings, StringClass.class, "element")).immediate(); assertEquals(computedString.get(), string1); strings.get(0).element.set(string2); assertEquals(computedString.get(), string2); }
/** * Checks whether the ObservableArrayList notifies its callbacks when an iterator is used * to remove items * * This test is temporarily disabled because of an Android bug. * A bug report has been filed: https://code.google.com/p/android/issues/detail?id=201481 */ // @Test public void testRemoveWithIterator() { final ObservableList<String> items = new ObservableArrayList<>(); final ListChangeMock<String> mock = new ListChangeMock<>(); items.addOnListChangedCallback(mock); // Add items to list items.addAll(Arrays.asList("Cake", "Pie", "Galette", "Pudding")); // Change listener reflects the added items assertEquals(items, mock.getCurrentList()); // Remove all items from the list using an iterator for (Iterator<String> iter = items.iterator(); iter.hasNext(); ) { iter.next(); iter.remove(); } // List is now empty assertTrue(items.isEmpty()); // Change listener should reflect this assertEquals(items, mock.getCurrentList()); }
@Test @SuppressWarnings("unchecked") public void remove() { FilteredReadOnlyObservableList<ObservableItem> list = new FilteredReadOnlyObservableList<>(rule.getSource()); rule.setUpList(list); ObservableItem willBeRemoved = rule.getSource().get(0); rule.getSource().remove(new ObservableItem("element1")); verify(rule.getCallback()).onItemRangeRemoved(list, 0, 1); assertThat(list.get(0).getValue(), is("element2")); willBeRemoved.setValue("element1-changed"); verify(rule.getCallback(), never()) .onItemRangeChanged((ObservableList) any(), anyInt(), anyInt()); }
@Test @SuppressWarnings("unchecked") public void multipleRemoveButFilteredAll() { FilteredReadOnlyObservableList<String> list = new FilteredReadOnlyObservableList<>(rule.getSource(), new Filter<String>() { @Override public boolean execute(String element) { return !element.equals("element2") && !element.equals("element3"); } }); rule.setUpList(list); rule.getSource().removeRange(1, 3); verify(rule.getCallback(), never()) .onItemRangeRemoved((ObservableList) any(), anyInt(), anyInt()); assertThat(list.size(), is(3)); }
@BindingAdapter("selectedItems") public static <T> void setSelectedItems( AtomListView view, @Nullable ObservableList<T> list){ if(view.getSelectedItems() == list) return; view.setSelectedItems(list); }
@BindingAdapter("items") public static <T> void adapterSetItems(AtomListView view, ObservableList<T> items){ if(view.getItems() == items) return; view.setItems(items); if(view.getLayoutManager() == null){ LinearLayoutManager linearLayoutManager = new LinearLayoutManager(view.getContext(), LinearLayoutManager.VERTICAL, false); view.setLayoutManager(linearLayoutManager); } }
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); } } }
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); }
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) { } }; }
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); } } }
public final void setItems(@Nullable List<T> items) { if (items == this.items) { return; } this.items = items; if (null == recyclerView) { if (this.items instanceof ObservableList) { removeObservableListCallback(); } if (items instanceof ObservableList) { addObservableListCallback(); } } notifyDataSetChanged(); }
private void addObservableListCallback() { if (null == this.items) { return; } callback = new ObservableListChangedCallback<>(this, (ObservableList<T>) items); ((ObservableList<T>) this.items).addOnListChangedCallback(callback); }
private void removeObservableListCallback() { if (null == this.items) { return; } ((ObservableList<T>) this.items).removeOnListChangedCallback(callback); callback = null; }
@Override public void onChanged(ObservableList<T> sender) { MvvmAdapter<T> adapter = getAdapter(); if (null == adapter) { return; } adapter.notifyDataSetChanged(); }
@Override public void onItemRangeChanged(ObservableList<T> sender, int positionStart, int itemCount) { MvvmAdapter<T> adapter = getAdapter(); if (null == adapter) { return; } adapter.notifyDataSetChanged(); }
@Override public void onItemRangeInserted(ObservableList<T> sender, int positionStart, int itemCount) { MvvmAdapter<T> adapter = getAdapter(); if (null == adapter) { return; } adapter.notifyDataSetChanged(); adapter.notifyItemRangeChanged(positionStart, itemCount); }
@Override public void onItemRangeMoved(ObservableList<T> sender, int fromPosition, int toPosition, int itemCount) { MvvmAdapter<T> adapter = getAdapter(); if (null == adapter) { return; } for (int i = 0; i < itemCount; i++) { adapter.notifyItemMoved(fromPosition + i, toPosition + i); } }
@Override public void onItemRangeRemoved(ObservableList<T> sender, int positionStart, int itemCount) { MvvmAdapter<T> adapter = getAdapter(); if (null == adapter) { return; } adapter.notifyDataSetChanged(); adapter.notifyItemRangeRemoved(positionStart, itemCount); }
static <T, A extends MvvmAdapter<T>> AdapterWRef<T, A> createRef(A adapter, ObservableList<T> items, ObservableList.OnListChangedCallback<ObservableList<T>> callback) { if (thread == null || !thread.isAlive()) { thread = new PollReferenceThread(); thread.start(); } return new AdapterWRef<>(adapter, items, callback); }
Listener(final ObservableArrayList<T> observableArrayList, final Observer<? super ObservableArrayListItemRangeChangeEvent<T>> observer) { this.observableArrayList = observableArrayList; this.onListChangedCallback = new ObservableList.OnListChangedCallback<ObservableArrayList<T>>() { @Override public void onChanged(ObservableArrayList<T> observableArrayList) { } @Override public void onItemRangeChanged(ObservableArrayList<T> observableArrayList, int positionStart, int itemCount) { observer.onNext( ObservableArrayListItemRangeChangeEvent.create(observableArrayList, positionStart, itemCount)); } @Override public void onItemRangeInserted(ObservableArrayList<T> observableArrayList, int positionStart, int itemCount) { } @Override public void onItemRangeMoved(ObservableArrayList<T> observableArrayList, int positionStart, int positionEnd, int itemCount) { } @Override public void onItemRangeRemoved(ObservableArrayList<T> observableArrayList, int positionStart, int itemCount) { } }; }
Listener(final ObservableArrayList<T> observableArrayList, final Observer<? super ObservableArrayListItemRangeInsertionEvent<T>> observer) { this.observableArrayList = observableArrayList; this.onListChangedCallback = new ObservableList.OnListChangedCallback<ObservableArrayList<T>>() { @Override public void onChanged(ObservableArrayList<T> observableArrayList) { } @Override public void onItemRangeChanged(ObservableArrayList<T> observableArrayList, int positionStart, int itemCount) { } @Override public void onItemRangeInserted(ObservableArrayList<T> observableArrayList, int positionStart, int itemCount) { observer.onNext(ObservableArrayListItemRangeInsertionEvent.create(observableArrayList, positionStart, itemCount)); } @Override public void onItemRangeMoved(ObservableArrayList<T> observableArrayList, int positionStart, int positionEnd, int itemCount) { } @Override public void onItemRangeRemoved(ObservableArrayList<T> observableArrayList, int positionStart, int itemCount) { } }; }
Listener(final ObservableArrayList<T> observableArrayList, final Observer<? super ObservableArrayListItemRangeRemovalEvent<T>> observer) { this.observableArrayList = observableArrayList; this.onListChangedCallback = new ObservableList.OnListChangedCallback<ObservableArrayList<T>>() { @Override public void onChanged(ObservableArrayList<T> observableArrayList) { } @Override public void onItemRangeChanged(ObservableArrayList<T> observableArrayList, int positionStart, int itemCount) { } @Override public void onItemRangeInserted(ObservableArrayList<T> observableArrayList, int positionStart, int itemCount) { } @Override public void onItemRangeMoved(ObservableArrayList<T> observableArrayList, int positionStart, int positionEnd, int itemCount) { } @Override public void onItemRangeRemoved(ObservableArrayList<T> observableArrayList, int positionStart, int itemCount) { observer.onNext(ObservableArrayListItemRangeRemovalEvent.create(observableArrayList, positionStart, itemCount)); } }; }
@Override public void onChanged(ObservableList sender) { RecyclerView.Adapter adapter = mAdapterWeakRef.get(); if (adapter != null) { adapter.notifyDataSetChanged(); } }
@Override public void onItemRangeRemoved(ObservableList sender, int positionStart, int itemCount) { RecyclerView.Adapter adapter = adapterReference.get(); if (adapter != null) { adapter.notifyItemRangeRemoved(positionStart, itemCount); } }
@Override public void onItemRangeInserted(ObservableList sender, int positionStart, int itemCount) { RecyclerView.Adapter adapter = mAdapterWeakRef.get(); if (adapter != null) { adapter.notifyItemRangeInserted(positionStart, itemCount); } }
@Override public void onItemRangeMoved(ObservableList sender, int fromPosition, int toPosition, int itemCount) { RecyclerView.Adapter adapter = mAdapterWeakRef.get(); if (adapter != null) { adapter.notifyItemMoved(fromPosition, toPosition); } }
@Override public void onItemRangeRemoved(ObservableList sender, int positionStart, int itemCount) { RecyclerView.Adapter adapter = mAdapterWeakRef.get(); if (adapter != null) { adapter.notifyItemRangeRemoved(positionStart, itemCount); } }
public SimpleBindingHeaderedListAdapter(final ObservableList<T> items, final int bindingVariableId, final int rowLayoutResId, final Object headerModel, final int bindingVariableIdHeader, final int headerLayoutResId) { mItems = items; mBindingVariableId = bindingVariableId; mRowLayoutResId = rowLayoutResId; mHeaderModel = headerModel; mBindingVariableIdHeader = bindingVariableIdHeader; mHeaderLayoutResId = headerLayoutResId; OnBindingListChangedCallback callback = new OnBindingListChangedCallback(this); mItems.addOnListChangedCallback(new HeaderedListChangedCallbackWrapper(callback)); }
public SimpleBindingListAdapter(final ObservableList<T> items, final int bindingVariableId, final int rowLayoutResId) { mItems = items; mBindingVariableId = bindingVariableId; mRowLayoutResId = rowLayoutResId; this.mOnListChangedCallback = new OnBindingListChangedCallback(this); mItems.addOnListChangedCallback(this.mOnListChangedCallback); }
public void setItems(ObservableList<T> items) { if (this.items != items) { this.items.removeOnListChangedCallback(callback); this.items = items; this.items.addOnListChangedCallback(callback); notifyDataSetChanged(); } }
@BindingAdapter({"bind:items", "bind:itemLayout", "bind:itemBindingId", "bind:orientation"}) public static void createAdapter(final RecyclerView list, ObservableList items, int itemLayout, int itemBindingId, int orientation) { if (null == list.getAdapter()) { SimpleBindingListAdapter adapter = new SimpleBindingListAdapter(items, itemBindingId, itemLayout); final LinearLayoutManager layoutManager = new LinearLayoutManager(list.getContext()); layoutManager.setOrientation(orientation); list.setLayoutManager(layoutManager); list.setAdapter(adapter); } }
@Override public void onChanged(ObservableList sender) { RecyclerView.Adapter adapter = adapterReference.get(); if (adapter != null) { adapter.notifyDataSetChanged(); } }
public ObservableRecyclerViewAdapter(@NonNull ObservableList<T> items, int itemLayout, int itemBindVariable) { this.items = items; this.itemLayout = itemLayout; this.itemBindVariable = itemBindVariable; this.callback = new WeakReferenceOnListChangedCallback<>(this); this.items.addOnListChangedCallback(callback); }
@Test @SuppressWarnings("unchecked") public void notMove() { FilteredReadOnlyObservableList<String> list = new FilteredReadOnlyObservableList<>(rule.getSource()); rule.setUpList(list); rule.getSource().move(1, 2, 3); verify(rule.getCallback(), never()) .onItemRangeRemoved((ObservableList) any(), anyInt(), anyInt()); verify(rule.getCallback(), never()) .onItemRangeInserted((ObservableList) any(), anyInt(), anyInt()); assertThat(list.get(1), is("element2")); }