use of eu.davidea.samples.flexibleadapter.items.SimpleItem in project FlexibleAdapter by davideas.
the class DatabaseService method newSimpleItem.
/*
* Creates a normal item with a Header linked.
*/
public static SimpleItem newSimpleItem(int i, IHeader header) {
SimpleItem item = new SimpleItem("I" + i, (HeaderItem) header);
item.setTitle("Simple Item " + i);
return item;
}
use of eu.davidea.samples.flexibleadapter.items.SimpleItem in project FlexibleAdapter by davideas.
the class UpdateDataSetTest method testUpdateDataSet_WithWithoutNotifyChange.
@Test
public void testUpdateDataSet_WithWithoutNotifyChange() throws Exception {
CountDownLatch signal = new CountDownLatch(1);
createSignalAdapter(signal);
// Let's change the DB
changeDatabaseContent();
List<AbstractFlexibleItem> dbItems = DatabaseService.getInstance().getDatabaseList();
// updateDataSet with Notify
mAdapter.setNotifyChangeOfUnfilteredItems(true);
mAdapter.updateDataSet(dbItems, true);
List<AbstractFlexibleItem> updatedItems_withNotifyChange = mAdapter.getCurrentItems();
// Restart
setUp();
mAdapter = new FlexibleAdapter<>(mInitialItems);
mAdapter.showAllHeaders();
changeDatabaseContent();
// The content of the 2 DBs must coincide
assertThat(dbItems, Matchers.contains(DatabaseService.getInstance().getDatabaseList().toArray()));
// Change behavior and updateDataSet
mAdapter.setNotifyChangeOfUnfilteredItems(false);
mAdapter.updateDataSet(DatabaseService.getInstance().getDatabaseList(), true);
List<AbstractFlexibleItem> updatedItems_withoutNotifyChange = mAdapter.getCurrentItems();
// The content of the 2 lists "with Notify" and "without Notify" must coincide
signal.await(300L, TimeUnit.MILLISECONDS);
assertEquals(updatedItems_withNotifyChange.size(), updatedItems_withoutNotifyChange.size());
assertThat(updatedItems_withNotifyChange, Matchers.contains(updatedItems_withoutNotifyChange.toArray()));
for (int i = 0; i < mAdapter.getItemCount(); i++) {
IFlexible iFlexible = updatedItems_withNotifyChange.get(i);
if (iFlexible instanceof SimpleItem) {
SimpleItem item1 = (SimpleItem) updatedItems_withNotifyChange.get(i);
SimpleItem item2 = (SimpleItem) updatedItems_withoutNotifyChange.get(i);
// assertThat(item1, Matchers.samePropertyValuesAs(item2)); // Problem with Matchers
assertEquals(item1.getId(), item2.getId());
assertEquals(item1.getTitle(), item2.getTitle());
}
}
}
use of eu.davidea.samples.flexibleadapter.items.SimpleItem in project FlexibleAdapter by davideas.
the class MainActivity method onDeleteConfirmed.
@Override
public void onDeleteConfirmed(int action) {
// Disable Refreshing
mSwipeRefreshLayout.setRefreshing(false);
// Removing items from Database. Example:
for (AbstractFlexibleItem adapterItem : mAdapter.getDeletedItems()) {
try {
// NEW! You can take advantage of AutoMap and differentiate logic by viewType using "switch" statement
switch(adapterItem.getLayoutRes()) {
case R.layout.recycler_sub_item:
SubItem subItem = (SubItem) adapterItem;
DatabaseService.getInstance().removeSubItem(mAdapter.getExpandableOfDeletedChild(subItem), subItem);
Log.d(TAG, "Confirm removed " + subItem.getTitle());
break;
case R.layout.recycler_expandable_item:
DatabaseService.getInstance().removeItem(adapterItem);
Log.d(TAG, "Confirm removed " + adapterItem);
break;
}
} catch (IllegalStateException e) {
// AutoMap is disabled, fallback to if-else with "instanceof" statement
if (adapterItem instanceof SubItem) {
// SubItem
SubItem subItem = (SubItem) adapterItem;
IExpandable expandable = mAdapter.getExpandableOf(subItem);
DatabaseService.getInstance().removeSubItem(expandable, subItem);
Log.d(TAG, "Confirm removed " + subItem.getTitle());
} else if (adapterItem instanceof SimpleItem || adapterItem instanceof ExpandableItem) {
DatabaseService.getInstance().removeItem(adapterItem);
Log.d(TAG, "Confirm removed " + adapterItem);
}
}
}
}
use of eu.davidea.samples.flexibleadapter.items.SimpleItem in project FlexibleAdapter by davideas.
the class MainActivity method onItemClick.
/* ========================================================================
* FLEXIBLE ADAPTER LISTENERS IMPLEMENTATION
* Listeners implementation are in MainActivity to easily reuse the common
* components like SwipeToRefresh, ActionMode, NavigationView, etc...
* ======================================================================== */
@Override
public boolean onItemClick(int position) {
IFlexible flexibleItem = mAdapter.getItem(position);
if (flexibleItem instanceof OverallItem) {
OverallItem overallItem = (OverallItem) flexibleItem;
MenuItem menuItem = mNavigationView.getMenu().findItem(overallItem.getId());
onNavigationItemSelected(menuItem);
return false;
}
// Action on elements are allowed if Mode is IDLE, otherwise selection has priority
if (mAdapter.getMode() != SelectableAdapter.MODE_IDLE && mActionModeHelper != null) {
boolean activate = mActionModeHelper.onClick(position);
Log.d(TAG, "Last activated position " + mActionModeHelper.getActivatedPosition());
return activate;
} else {
// Notify the active callbacks or implement a custom action onClick
if (flexibleItem instanceof SimpleItem || flexibleItem instanceof SubItem) {
//TODO FOR YOU: call your custom Action on item click
String title = extractTitleFrom(flexibleItem);
EditItemDialog.newInstance(title, position).show(getFragmentManager(), EditItemDialog.TAG);
}
return false;
}
}
use of eu.davidea.samples.flexibleadapter.items.SimpleItem in project FlexibleAdapter by davideas.
the class MainActivity method onItemClick.
/* ========================================================================
* FLEXIBLE ADAPTER LISTENERS IMPLEMENTATION
* Listeners implementation are in MainActivity to easily reuse the common
* components like SwipeToRefresh, ActionMode, NavigationView, etc...
* ======================================================================== */
@Override
public boolean onItemClick(View view, int position) {
IFlexible flexibleItem = mAdapter.getItem(position);
if (flexibleItem instanceof OverallItem) {
OverallItem overallItem = (OverallItem) flexibleItem;
MenuItem menuItem = mNavigationView.getMenu().findItem(overallItem.getId());
onNavigationItemSelected(menuItem);
return false;
}
// Action on elements are allowed if Mode is IDLE, otherwise selection has priority
if (mAdapter.getMode() != Mode.IDLE && mActionModeHelper != null) {
boolean activate = mActionModeHelper.onClick(position);
Log.d("Last activated position %s", mActionModeHelper.getActivatedPosition());
return activate;
} else {
// Notify the active callbacks or implement a custom action onClick
if (flexibleItem instanceof SimpleItem || flexibleItem instanceof SubItem) {
mRecyclerView.postDelayed(() -> {
Log.d("scroll to position=%s item=%s", position, mAdapter.getItem(position));
int headers = mAdapter.areHeadersSticky() ? 1 : 0;
mRecyclerView.smoothScrollToPosition(Math.max(0, position - headers));
}, 300L);
// //TODO FOR YOU: call your custom Action on item click
// String title = extractTitleFrom(flexibleItem);
// EditItemDialog.newInstance(title, position).show(getFragmentManager(), EditItemDialog.TAG);
}
return false;
}
}
Aggregations