Search in sources :

Example 11 with IItem

use of com.mikepenz.fastadapter.IItem in project FastAdapter by mikepenz.

the class AdvancedSampleActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    findViewById(android.R.id.content).setSystemUiVisibility(findViewById(android.R.id.content).getSystemUiVisibility() | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
    //as we use an icon from Android-Iconics via xml we add the IconicsLayoutInflater
    //https://github.com/mikepenz/Android-Iconics
    LayoutInflaterCompat.setFactory(getLayoutInflater(), new IconicsLayoutInflater(getDelegate()));
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sample);
    // Handle Toolbar
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setTitle(R.string.sample_advanced);
    //style our ui
    new MaterializeBuilder().withActivity(this).build();
    //create our FastAdapter
    mFastAdapter = new FastAdapter<>();
    //we init our ActionModeHelper
    mActionModeHelper = new ActionModeHelper(mFastAdapter, R.menu.cab, new ActionBarCallBack());
    mActionModeHelper.withSupportSubItems(true);
    //create our adapters
    final StickyHeaderAdapter stickyHeaderAdapter = new StickyHeaderAdapter();
    mItemAdapter = new ItemAdapter<>();
    mHeaderAdapter = new HeaderAdapter<>();
    //configure our mFastAdapter
    //as we provide id's for the items we want the hasStableIds enabled to speed up things
    mFastAdapter.withSelectable(true);
    mFastAdapter.withMultiSelect(true);
    mFastAdapter.withSelectOnLongClick(true);
    mFastAdapter.withPositionBasedStateManagement(false);
    mFastAdapter.withOnPreClickListener(new FastAdapter.OnClickListener<IItem>() {

        @Override
        public boolean onClick(View v, IAdapter adapter, IItem item, int position) {
            //we handle the default onClick behavior for the actionMode. This will return null if it didn't do anything and you can handle a normal onClick
            Boolean res = mActionModeHelper.onClick(item);
            return res != null ? res : false;
        }
    });
    mFastAdapter.withOnPreLongClickListener(new FastAdapter.OnLongClickListener<IItem>() {

        @Override
        public boolean onLongClick(View v, IAdapter adapter, IItem item, int position) {
            //we do not want expandable items to be selected
            if (item instanceof IExpandable) {
                if (((IExpandable) item).getSubItems() != null) {
                    return true;
                }
            }
            //handle the longclick actions
            ActionMode actionMode = mActionModeHelper.onLongClick(AdvancedSampleActivity.this, position);
            if (actionMode != null) {
                //we want color our CAB
                findViewById(R.id.action_mode_bar).setBackgroundColor(UIUtils.getThemeColorFromAttrOrRes(AdvancedSampleActivity.this, R.attr.colorPrimary, R.color.material_drawer_primary));
            }
            //if we have no actionMode we do not consume the event
            return actionMode != null;
        }
    });
    //get our recyclerView and do basic setup
    RecyclerView rv = (RecyclerView) findViewById(R.id.rv);
    rv.setLayoutManager(new LinearLayoutManager(this));
    rv.setItemAnimator(new DefaultItemAnimator());
    rv.setAdapter(stickyHeaderAdapter.wrap(mItemAdapter.wrap(mHeaderAdapter.wrap(mFastAdapter))));
    final StickyRecyclerHeadersDecoration decoration = new StickyRecyclerHeadersDecoration(stickyHeaderAdapter);
    rv.addItemDecoration(decoration);
    //so the headers are aware of changes
    stickyHeaderAdapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {

        @Override
        public void onChanged() {
            decoration.invalidateHeaders();
        }
    });
    //init cache with the added items, this is useful for shorter lists with many many different view types (at least 4 or more
    //new RecyclerViewCacheUtil().withCacheSize(2).apply(rv, items);
    //set the back arrow in the toolbar
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeButtonEnabled(false);
    //we define the items
    setItems();
    //restore selections (this has to be done after the items were added
    mFastAdapter.withSavedInstanceState(savedInstanceState);
}
Also used : LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) DefaultItemAnimator(android.support.v7.widget.DefaultItemAnimator) StickyHeaderAdapter(com.mikepenz.fastadapter.app.adapters.StickyHeaderAdapter) IItem(com.mikepenz.fastadapter.IItem) IAdapter(com.mikepenz.fastadapter.IAdapter) Toolbar(android.support.v7.widget.Toolbar) IExpandable(com.mikepenz.fastadapter.IExpandable) MaterializeBuilder(com.mikepenz.materialize.MaterializeBuilder) View(android.view.View) RecyclerView(android.support.v7.widget.RecyclerView) StickyRecyclerHeadersDecoration(com.timehop.stickyheadersrecyclerview.StickyRecyclerHeadersDecoration) ActionMode(android.support.v7.view.ActionMode) RecyclerView(android.support.v7.widget.RecyclerView) FastAdapter(com.mikepenz.fastadapter.FastAdapter) IconicsLayoutInflater(com.mikepenz.iconics.context.IconicsLayoutInflater) ActionModeHelper(com.mikepenz.fastadapter_extensions.ActionModeHelper)

Example 12 with IItem

use of com.mikepenz.fastadapter.IItem in project FastAdapter by mikepenz.

the class RangeSelectorHelper method selectRange.

/**
     * selects all items in a range, from and to indizes are inclusive
     *
     * @param from the from index
     * @param to the to index
     * @param select true, if the provided range should be selected, false otherwise
     * @param skipHeaders true, if you do not want to process headers, false otherwise
     */
public <T extends IItem & IExpandable> void selectRange(int from, int to, boolean select, boolean skipHeaders) {
    if (from > to) {
        int temp = from;
        from = to;
        to = temp;
    }
    IItem item;
    for (int i = from; i <= to; i++) {
        item = mFastAdapter.getAdapterItem(i);
        if (item.isSelectable()) {
            if (select) {
                mFastAdapter.select(i);
            } else {
                mFastAdapter.deselect(i);
            }
        }
        if (mSupportSubItems && !skipHeaders) {
            // if a group is collapsed, select all sub items
            if (item instanceof IExpandable && !((IExpandable) item).isExpanded()) {
                SubItemUtil.selectAllSubItems(mFastAdapter, (T) mFastAdapter.getAdapterItem(i), select, true, mPayload);
            }
        }
    }
    if (mActionModeHelper != null) {
        // works with null as well, as the ActionMode is active for sure!
        mActionModeHelper.checkActionMode(null);
    }
}
Also used : IExpandable(com.mikepenz.fastadapter.IExpandable) IItem(com.mikepenz.fastadapter.IItem)

Example 13 with IItem

use of com.mikepenz.fastadapter.IItem in project FastAdapter by mikepenz.

the class AdapterUtil method getAllItems.

/**
     * Gets all items (including sub items) from the FastAdapter
     *
     * @param fastAdapter the FastAdapter
     * @return a list of all items including the whole subItem hirachy
     */
public static <Item extends IItem> List<Item> getAllItems(FastAdapter<Item> fastAdapter) {
    int size = fastAdapter.getItemCount();
    List<Item> items = new ArrayList<>(size);
    for (int i = 0; i < size; i++) {
        Item item = fastAdapter.getItem(i);
        items.add(item);
        addAllSubItems(item, items);
    }
    return items;
}
Also used : IItem(com.mikepenz.fastadapter.IItem) ArrayList(java.util.ArrayList)

Example 14 with IItem

use of com.mikepenz.fastadapter.IItem in project FastAdapter by mikepenz.

the class AdapterUtil method restoreSubItemSelectionStatesForAlternativeStateManagement.

/**
     * internal method to restore the selection state of subItems
     *
     * @param item          the parent item
     * @param selectedItems the list of selectedItems from the savedInstanceState
     */
public static <Item extends IItem> void restoreSubItemSelectionStatesForAlternativeStateManagement(Item item, List<String> selectedItems) {
    if (item instanceof IExpandable && !((IExpandable) item).isExpanded() && ((IExpandable) item).getSubItems() != null) {
        List<Item> subItems = (List<Item>) ((IExpandable<Item, ?>) item).getSubItems();
        Item subItem;
        String id;
        for (int i = 0, size = subItems.size(); i < size; i++) {
            subItem = subItems.get(i);
            id = String.valueOf(subItem.getIdentifier());
            if (selectedItems != null && selectedItems.contains(id)) {
                subItem.withSetSelected(true);
            }
            restoreSubItemSelectionStatesForAlternativeStateManagement(subItem, selectedItems);
        }
    }
}
Also used : IItem(com.mikepenz.fastadapter.IItem) IExpandable(com.mikepenz.fastadapter.IExpandable) List(java.util.List) ArrayList(java.util.ArrayList)

Example 15 with IItem

use of com.mikepenz.fastadapter.IItem in project FastAdapter by mikepenz.

the class AdapterUtil method addAllSubItems.

/**
     * Gets all subItems from a given parent item
     *
     * @param item  the parent from which we add all items
     * @param items the list in which we add the subItems
     */
public static <Item extends IItem> void addAllSubItems(Item item, List<Item> items) {
    if (item instanceof IExpandable && !((IExpandable) item).isExpanded() && ((IExpandable) item).getSubItems() != null) {
        List<Item> subItems = (List<Item>) ((IExpandable<Item, ?>) item).getSubItems();
        Item subItem;
        for (int i = 0, size = subItems.size(); i < size; i++) {
            subItem = subItems.get(i);
            items.add(subItem);
            addAllSubItems(subItem, items);
        }
    }
}
Also used : IItem(com.mikepenz.fastadapter.IItem) IExpandable(com.mikepenz.fastadapter.IExpandable) List(java.util.List) ArrayList(java.util.ArrayList)

Aggregations

IItem (com.mikepenz.fastadapter.IItem)18 ArrayList (java.util.ArrayList)12 IExpandable (com.mikepenz.fastadapter.IExpandable)10 RecyclerView (android.support.v7.widget.RecyclerView)5 Toolbar (android.support.v7.widget.Toolbar)5 MaterializeBuilder (com.mikepenz.materialize.MaterializeBuilder)5 LinkedList (java.util.LinkedList)5 LinearLayoutManager (android.support.v7.widget.LinearLayoutManager)4 IAdapter (com.mikepenz.fastadapter.IAdapter)4 SimpleSubExpandableItem (com.mikepenz.fastadapter.app.items.expandable.SimpleSubExpandableItem)4 SimpleSubItem (com.mikepenz.fastadapter.app.items.expandable.SimpleSubItem)4 IconicsLayoutInflater (com.mikepenz.iconics.context.IconicsLayoutInflater)4 FastAdapter (com.mikepenz.fastadapter.FastAdapter)3 SimpleItem (com.mikepenz.fastadapter.app.items.SimpleItem)3 SlideDownAlphaAnimator (com.mikepenz.itemanimators.SlideDownAlphaAnimator)3 List (java.util.List)3 ActionMode (android.support.v7.view.ActionMode)2 DefaultItemAnimator (android.support.v7.widget.DefaultItemAnimator)2 View (android.view.View)2 IItemAdapter (com.mikepenz.fastadapter.IItemAdapter)2