Search in sources :

Example 1 with IFlexible

use of eu.davidea.flexibleadapter.items.IFlexible in project FlexibleAdapter by davideas.

the class DatabaseService method updateNewItems.

/**
	 * This demonstrates that new content of existing items are really rebound and
	 * notified with CHANGE Payload in the Adapter list when refreshed.
	 */
public void updateNewItems() {
    for (IFlexible iFlexible : mItems) {
        if (iFlexible instanceof AbstractItem) {
            AbstractItem item = (AbstractItem) iFlexible;
            item.increaseUpdates();
        }
    }
}
Also used : IFlexible(eu.davidea.flexibleadapter.items.IFlexible) AbstractItem(eu.davidea.samples.flexibleadapter.items.AbstractItem)

Example 2 with IFlexible

use of eu.davidea.flexibleadapter.items.IFlexible in project FlexibleAdapter by davideas.

the class MainActivity method onItemSwipe.

@Override
public void onItemSwipe(final int position, int direction) {
    Log.i(TAG, "onItemSwipe position=" + position + " direction=" + (direction == ItemTouchHelper.LEFT ? "LEFT" : "RIGHT"));
    // Option 1 FULL_SWIPE: Direct action no Undo Action
    // Do something based on direction when item has been swiped:
    //   A) update item, set "read" if an email etc.
    //   B) remove the item from the adapter;
    // Option 2 FULL_SWIPE: Delayed action with Undo Action
    // Show action button and start a new Handler:
    //   A) on time out do something based on direction (open dialog with options);
    // Create list for single position (only in onItemSwipe)
    List<Integer> positions = new ArrayList<>(1);
    positions.add(position);
    // Build the message
    IFlexible abstractItem = mAdapter.getItem(position);
    StringBuilder message = new StringBuilder();
    message.append(extractTitleFrom(abstractItem)).append(" ");
    // Experimenting NEW feature
    if (abstractItem.isSelectable())
        mAdapter.setRestoreSelectionOnUndo(false);
    // Here, option 2A) is implemented
    if (direction == ItemTouchHelper.LEFT) {
        message.append(getString(R.string.action_archived));
        // Example of UNDO color
        int actionTextColor;
        if (Utils.hasMarshmallow()) {
            actionTextColor = getColor(R.color.material_color_orange_500);
        } else {
            //noinspection deprecation
            actionTextColor = getResources().getColor(R.color.material_color_orange_500);
        }
        new UndoHelper(mAdapter, this).withPayload(//You can pass any custom object (in this case Boolean is enough)
        null).withAction(UndoHelper.ACTION_UPDATE, new UndoHelper.SimpleActionListener() {

            @Override
            public boolean onPreAction() {
                // remove the item from Adapter list as usual.
                return true;
            }
        }).withActionTextColor(actionTextColor).remove(positions, findViewById(R.id.main_view), message, getString(R.string.undo), UndoHelper.UNDO_TIMEOUT);
    //Here, option 1B) is implemented
    } else if (direction == ItemTouchHelper.RIGHT) {
        message.append(getString(R.string.action_deleted));
        mSwipeRefreshLayout.setRefreshing(true);
        new UndoHelper(mAdapter, this).withPayload(//You can pass any custom object (in this case Boolean is enough)
        null).withAction(UndoHelper.ACTION_REMOVE, new UndoHelper.SimpleActionListener() {

            @Override
            public void onPostAction() {
                // Handle ActionMode title
                if (mAdapter.getSelectedItemCount() == 0)
                    mActionModeHelper.destroyActionModeIfCan();
                else
                    mActionModeHelper.updateContextTitle(mAdapter.getSelectedItemCount());
            }
        }).remove(positions, findViewById(R.id.main_view), message, getString(R.string.undo), UndoHelper.UNDO_TIMEOUT);
    }
}
Also used : UndoHelper(eu.davidea.flexibleadapter.helpers.UndoHelper) ArrayList(java.util.ArrayList) IFlexible(eu.davidea.flexibleadapter.items.IFlexible)

Example 3 with IFlexible

use of eu.davidea.flexibleadapter.items.IFlexible 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 4 with IFlexible

use of eu.davidea.flexibleadapter.items.IFlexible in project FlexibleAdapter by davideas.

the class FragmentViewPager method createList.

private List<IFlexible> createList(int size, int headers) {
    HeaderItem header = null;
    List<IFlexible> items = new ArrayList<>();
    int lastHeaderId = 0;
    for (int i = 0; i < size; i++) {
        header = i % Math.round(size / headers) == 0 ? DatabaseService.newHeader(++lastHeaderId) : header;
        items.add(DatabaseService.newSimpleItem(i + 1, header));
    }
    return items;
}
Also used : ArrayList(java.util.ArrayList) IFlexible(eu.davidea.flexibleadapter.items.IFlexible) HeaderItem(eu.davidea.samples.flexibleadapter.items.HeaderItem)

Aggregations

IFlexible (eu.davidea.flexibleadapter.items.IFlexible)4 ArrayList (java.util.ArrayList)2 MenuItem (android.view.MenuItem)1 UndoHelper (eu.davidea.flexibleadapter.helpers.UndoHelper)1 AbstractItem (eu.davidea.samples.flexibleadapter.items.AbstractItem)1 HeaderItem (eu.davidea.samples.flexibleadapter.items.HeaderItem)1 OverallItem (eu.davidea.samples.flexibleadapter.items.OverallItem)1 SimpleItem (eu.davidea.samples.flexibleadapter.items.SimpleItem)1 SubItem (eu.davidea.samples.flexibleadapter.items.SubItem)1