Search in sources :

Example 6 with IExpandable

use of com.mikepenz.fastadapter.IExpandable 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 7 with IExpandable

use of com.mikepenz.fastadapter.IExpandable 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 8 with IExpandable

use of com.mikepenz.fastadapter.IExpandable in project MaterialDrawer by mikepenz.

the class DrawerBuilder method createContent.

/**
     * the helper method to create the content for the drawer
     */
private void createContent() {
    //if we have a customView use this
    if (mCustomView != null) {
        LinearLayout.LayoutParams contentParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        contentParams.weight = 1f;
        mSliderLayout.addView(mCustomView, contentParams);
        return;
    }
    //set the shadow for the drawer
    if (Build.VERSION.SDK_INT < 21 && mDrawerLayout != null) {
        if (ViewCompat.getLayoutDirection(mRootView) == ViewCompat.LAYOUT_DIRECTION_LTR) {
            mDrawerLayout.setDrawerShadow(mDrawerGravity == GravityCompat.START ? R.drawable.material_drawer_shadow_right : R.drawable.material_drawer_shadow_left, mDrawerGravity);
        } else {
            mDrawerLayout.setDrawerShadow(mDrawerGravity == GravityCompat.START ? R.drawable.material_drawer_shadow_left : R.drawable.material_drawer_shadow_right, mDrawerGravity);
        }
    }
    // if we have an adapter (either by defining a custom one or the included one add a list :D
    View contentView;
    if (mRecyclerView == null) {
        contentView = LayoutInflater.from(mActivity).inflate(R.layout.material_drawer_recycler_view, mSliderLayout, false);
        mRecyclerView = (RecyclerView) contentView.findViewById(R.id.material_drawer_recycler_view);
        //set the itemAnimator
        mRecyclerView.setItemAnimator(mItemAnimator);
        //some style improvements on older devices
        mRecyclerView.setFadingEdgeLength(0);
        //set the drawing cache background to the same color as the slider to improve performance
        //mRecyclerView.setDrawingCacheBackgroundColor(UIUtils.getThemeColorFromAttrOrRes(mActivity, R.attr.material_drawer_background, R.color.material_drawer_background));
        mRecyclerView.setClipToPadding(false);
        //additional stuff
        mRecyclerView.setLayoutManager(mLayoutManager);
        int paddingTop = 0;
        if ((mDisplayBelowStatusBar == null || mDisplayBelowStatusBar) && !mSystemUIHidden) {
            paddingTop = UIUtils.getStatusBarHeight(mActivity);
        }
        int paddingBottom = 0;
        int orientation = mActivity.getResources().getConfiguration().orientation;
        if (((mTranslucentNavigationBar || mFullscreen) && Build.VERSION.SDK_INT >= 21) && !mSystemUIHidden && (orientation == Configuration.ORIENTATION_PORTRAIT || (orientation == Configuration.ORIENTATION_LANDSCAPE && DrawerUIUtils.isSystemBarOnBottom(mActivity)))) {
            paddingBottom = UIUtils.getNavigationBarHeight(mActivity);
        }
        mRecyclerView.setPadding(0, paddingTop, 0, paddingBottom);
    } else {
        contentView = mRecyclerView;
    }
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
    params.weight = 1f;
    mSliderLayout.addView(contentView, params);
    if (mInnerShadow) {
        View innerShadow = mSliderLayout.findViewById(R.id.material_drawer_inner_shadow);
        innerShadow.setVisibility(View.VISIBLE);
        innerShadow.bringToFront();
        if (mDrawerGravity == GravityCompat.START) {
            innerShadow.setBackgroundResource(R.drawable.material_drawer_shadow_left);
        } else {
            innerShadow.setBackgroundResource(R.drawable.material_drawer_shadow_right);
        }
    }
    // set the background
    if (mSliderBackgroundColor != 0) {
        mSliderLayout.setBackgroundColor(mSliderBackgroundColor);
    } else if (mSliderBackgroundColorRes != -1) {
        mSliderLayout.setBackgroundColor(ContextCompat.getColor(mActivity, mSliderBackgroundColorRes));
    } else if (mSliderBackgroundDrawable != null) {
        UIUtils.setBackground(mSliderLayout, mSliderBackgroundDrawable);
    } else if (mSliderBackgroundDrawableRes != -1) {
        UIUtils.setBackground(mSliderLayout, mSliderBackgroundDrawableRes);
    }
    //handle the header
    DrawerUtils.handleHeaderView(this);
    //handle the footer
    DrawerUtils.handleFooterView(this, new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            IDrawerItem drawerItem = (IDrawerItem) v.getTag();
            DrawerUtils.onFooterDrawerItemClick(DrawerBuilder.this, drawerItem, v, true);
        }
    });
    //if MultiSelect is possible
    mAdapter.withMultiSelect(mMultiSelect);
    if (mMultiSelect) {
        mAdapter.withSelectOnLongClick(false);
        mAdapter.withAllowDeselection(true);
    }
    //set the adapter on the listView
    if (mAdapterWrapper == null) {
        mRecyclerView.setAdapter(mAdapter);
    } else {
        mRecyclerView.setAdapter(mAdapterWrapper);
    }
    //predefine selection (should be the first element
    if (mSelectedItemPosition == 0 && mSelectedItemIdentifier != 0L) {
        mSelectedItemPosition = DrawerUtils.getPositionByIdentifier(this, mSelectedItemIdentifier);
    }
    if (mHeaderView != null && mSelectedItemPosition == 0) {
        mSelectedItemPosition = 1;
    }
    mAdapter.deselect();
    mAdapter.select(mSelectedItemPosition);
    // add the onDrawerItemClickListener if set
    mAdapter.withOnClickListener(new FastAdapter.OnClickListener<IDrawerItem>() {

        @Override
        public boolean onClick(final View view, IAdapter<IDrawerItem> adapter, final IDrawerItem item, final int position) {
            if (!(item != null && item instanceof Selectable && !item.isSelectable())) {
                resetStickyFooterSelection();
                mCurrentStickyFooterSelection = -1;
            }
            //call the listener
            boolean consumed = false;
            //call the item specific listener
            if (item instanceof AbstractDrawerItem && ((AbstractDrawerItem) item).getOnDrawerItemClickListener() != null) {
                consumed = ((AbstractDrawerItem) item).getOnDrawerItemClickListener().onItemClick(view, position, item);
            }
            //call the drawer listener
            if (mOnDrawerItemClickListener != null) {
                if (mDelayDrawerClickEvent > 0) {
                    new Handler().postDelayed(new Runnable() {

                        @Override
                        public void run() {
                            mOnDrawerItemClickListener.onItemClick(view, position, item);
                        }
                    }, mDelayDrawerClickEvent);
                } else {
                    consumed = mOnDrawerItemClickListener.onItemClick(view, position, item);
                }
            }
            //we have to notify the miniDrawer if existing, and if the event was not consumed yet
            if (!consumed && mMiniDrawer != null) {
                consumed = mMiniDrawer.onItemClick(item);
            }
            //if we were a expandable item we consume the event closing makes no sense
            if (item instanceof IExpandable && ((IExpandable) item).getSubItems() != null) {
                //we consume the event and want no further handling
                return true;
            }
            if (!consumed) {
                //close the drawer after click
                closeDrawerDelayed();
            }
            return consumed;
        }
    });
    // add the onDrawerItemLongClickListener if set
    mAdapter.withOnLongClickListener(new FastAdapter.OnLongClickListener<IDrawerItem>() {

        @Override
        public boolean onLongClick(View view, IAdapter<IDrawerItem> adapter, final IDrawerItem item, final int position) {
            if (mOnDrawerItemLongClickListener != null) {
                return mOnDrawerItemLongClickListener.onItemLongClick(view, position, getDrawerItem(position));
            }
            return false;
        }
    });
    if (mRecyclerView != null) {
        mRecyclerView.scrollToPosition(0);
    }
    // try to restore all saved values again
    if (mSavedInstance != null) {
        if (!mAppended) {
            mAdapter.withSavedInstanceState(mSavedInstance, Drawer.BUNDLE_SELECTION);
            DrawerUtils.setStickyFooterSelection(this, mSavedInstance.getInt(Drawer.BUNDLE_STICKY_FOOTER_SELECTION, -1), null);
        } else {
            mAdapter.withSavedInstanceState(mSavedInstance, Drawer.BUNDLE_SELECTION_APPENDED);
            DrawerUtils.setStickyFooterSelection(this, mSavedInstance.getInt(Drawer.BUNDLE_STICKY_FOOTER_SELECTION_APPENDED, -1), null);
        }
    }
    // call initial onClick event to allow the dev to init the first view
    if (mFireInitialOnClick && mOnDrawerItemClickListener != null) {
        int selection = mAdapter.getSelections().size() == 0 ? -1 : mAdapter.getSelections().iterator().next();
        mOnDrawerItemClickListener.onItemClick(null, selection, getDrawerItem(selection));
    }
}
Also used : IDrawerItem(com.mikepenz.materialdrawer.model.interfaces.IDrawerItem) IExpandable(com.mikepenz.fastadapter.IExpandable) Handler(android.os.Handler) View(android.view.View) RecyclerView(android.support.v7.widget.RecyclerView) AbstractDrawerItem(com.mikepenz.materialdrawer.model.AbstractDrawerItem) Selectable(com.mikepenz.materialdrawer.model.interfaces.Selectable) FastAdapter(com.mikepenz.fastadapter.FastAdapter) LinearLayout(android.widget.LinearLayout)

Example 9 with IExpandable

use of com.mikepenz.fastadapter.IExpandable 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 10 with IExpandable

use of com.mikepenz.fastadapter.IExpandable 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

IExpandable (com.mikepenz.fastadapter.IExpandable)11 IItem (com.mikepenz.fastadapter.IItem)10 ArrayList (java.util.ArrayList)6 IAdapter (com.mikepenz.fastadapter.IAdapter)3 List (java.util.List)3 RecyclerView (android.support.v7.widget.RecyclerView)2 View (android.view.View)2 FastAdapter (com.mikepenz.fastadapter.FastAdapter)2 IItemAdapter (com.mikepenz.fastadapter.IItemAdapter)2 LinkedList (java.util.LinkedList)2 Handler (android.os.Handler)1 ActionMode (android.support.v7.view.ActionMode)1 DefaultItemAnimator (android.support.v7.widget.DefaultItemAnimator)1 LinearLayoutManager (android.support.v7.widget.LinearLayoutManager)1 Toolbar (android.support.v7.widget.Toolbar)1 LinearLayout (android.widget.LinearLayout)1 StickyHeaderAdapter (com.mikepenz.fastadapter.app.adapters.StickyHeaderAdapter)1 ActionModeHelper (com.mikepenz.fastadapter_extensions.ActionModeHelper)1 IconicsLayoutInflater (com.mikepenz.iconics.context.IconicsLayoutInflater)1 AbstractDrawerItem (com.mikepenz.materialdrawer.model.AbstractDrawerItem)1