Search in sources :

Example 1 with SimpleItem

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;
}
Also used : SimpleItem(eu.davidea.samples.flexibleadapter.items.SimpleItem)

Example 2 with SimpleItem

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());
        }
    }
}
Also used : AbstractFlexibleItem(eu.davidea.flexibleadapter.items.AbstractFlexibleItem) IFlexible(eu.davidea.flexibleadapter.items.IFlexible) CountDownLatch(java.util.concurrent.CountDownLatch) SimpleItem(eu.davidea.samples.flexibleadapter.items.SimpleItem) Test(org.junit.Test)

Example 3 with SimpleItem

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);
            }
        }
    }
}
Also used : AbstractFlexibleItem(eu.davidea.flexibleadapter.items.AbstractFlexibleItem) SubItem(eu.davidea.samples.flexibleadapter.items.SubItem) IExpandable(eu.davidea.flexibleadapter.items.IExpandable) ExpandableItem(eu.davidea.samples.flexibleadapter.items.ExpandableItem) SimpleItem(eu.davidea.samples.flexibleadapter.items.SimpleItem)

Example 4 with SimpleItem

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;
    }
}
Also used : SubItem(eu.davidea.samples.flexibleadapter.items.SubItem) IFlexible(eu.davidea.flexibleadapter.items.IFlexible) MenuItem(android.view.MenuItem) SimpleItem(eu.davidea.samples.flexibleadapter.items.SimpleItem) OverallItem(eu.davidea.samples.flexibleadapter.items.OverallItem)

Example 5 with SimpleItem

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;
    }
}
Also used : SubItem(eu.davidea.samples.flexibleadapter.items.SubItem) IFlexible(eu.davidea.flexibleadapter.items.IFlexible) MenuItem(android.view.MenuItem) SimpleItem(eu.davidea.samples.flexibleadapter.items.SimpleItem) OverallItem(eu.davidea.samples.flexibleadapter.items.OverallItem)

Aggregations

SimpleItem (eu.davidea.samples.flexibleadapter.items.SimpleItem)6 AbstractFlexibleItem (eu.davidea.flexibleadapter.items.AbstractFlexibleItem)3 IFlexible (eu.davidea.flexibleadapter.items.IFlexible)3 SubItem (eu.davidea.samples.flexibleadapter.items.SubItem)3 MenuItem (android.view.MenuItem)2 OverallItem (eu.davidea.samples.flexibleadapter.items.OverallItem)2 IExpandable (eu.davidea.flexibleadapter.items.IExpandable)1 ExpandableItem (eu.davidea.samples.flexibleadapter.items.ExpandableItem)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Test (org.junit.Test)1