Search in sources :

Example 1 with AbstractDrawerItem

use of com.mikepenz.materialdrawer.model.AbstractDrawerItem 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 2 with AbstractDrawerItem

use of com.mikepenz.materialdrawer.model.AbstractDrawerItem in project MaterialDrawer by mikepenz.

the class DrawerUtils method onFooterDrawerItemClick.

/**
     * helper method to handle the onClick of the footer
     *
     * @param drawer
     * @param drawerItem
     * @param v
     * @param fireOnClick true if we should call the listener, false if not, null to not call the listener and not close the drawer
     */
public static void onFooterDrawerItemClick(DrawerBuilder drawer, IDrawerItem drawerItem, View v, Boolean fireOnClick) {
    boolean checkable = !(drawerItem != null && drawerItem instanceof Selectable && !drawerItem.isSelectable());
    if (checkable) {
        drawer.resetStickyFooterSelection();
        v.setActivated(true);
        v.setSelected(true);
        //remove the selection in the list
        drawer.getAdapter().deselect();
        //find the position of the clicked footer item
        if (drawer.mStickyFooterView != null && drawer.mStickyFooterView instanceof LinearLayout) {
            LinearLayout footer = (LinearLayout) drawer.mStickyFooterView;
            for (int i = 0; i < footer.getChildCount(); i++) {
                if (footer.getChildAt(i) == v) {
                    drawer.mCurrentStickyFooterSelection = i;
                    break;
                }
            }
        }
    }
    if (fireOnClick != null) {
        boolean consumed = false;
        if (fireOnClick) {
            if (drawerItem instanceof AbstractDrawerItem && ((AbstractDrawerItem) drawerItem).getOnDrawerItemClickListener() != null) {
                consumed = ((AbstractDrawerItem) drawerItem).getOnDrawerItemClickListener().onItemClick(v, -1, drawerItem);
            }
            if (drawer.mOnDrawerItemClickListener != null) {
                consumed = drawer.mOnDrawerItemClickListener.onItemClick(v, -1, drawerItem);
            }
        }
        if (!consumed) {
            //close the drawer after click
            drawer.closeDrawerDelayed();
        }
    }
}
Also used : AbstractDrawerItem(com.mikepenz.materialdrawer.model.AbstractDrawerItem) Selectable(com.mikepenz.materialdrawer.model.interfaces.Selectable) LinearLayout(android.widget.LinearLayout)

Example 3 with AbstractDrawerItem

use of com.mikepenz.materialdrawer.model.AbstractDrawerItem in project MaterialDrawer by mikepenz.

the class Drawer method setSelectionAtPosition.

/*
     * set the current selection in the drawer
     * NOTE: this also deselects all other selections. if you do not want this. use the direct api of the adater .getAdapter().select(position, fireOnClick)
     * NOTE: This will trigger onDrawerItemSelected without a view if you pass fireOnClick = true;
     *
     * @param position
     * @param fireOnClick
     * @return true if the event was consumed
     */
public boolean setSelectionAtPosition(int position, boolean fireOnClick) {
    if (mDrawerBuilder.mRecyclerView != null) {
        mDrawerBuilder.mAdapter.deselect();
        mDrawerBuilder.mAdapter.select(position, false);
        if (fireOnClick && position >= 0) {
            IDrawerItem item = mDrawerBuilder.mAdapter.getItem(position);
            if (item instanceof AbstractDrawerItem && ((AbstractDrawerItem) item).getOnDrawerItemClickListener() != null) {
                ((AbstractDrawerItem) item).getOnDrawerItemClickListener().onItemClick(null, position, item);
            }
            if (mDrawerBuilder.mOnDrawerItemClickListener != null) {
                mDrawerBuilder.mOnDrawerItemClickListener.onItemClick(null, position, item);
            }
        }
        //we set the selection on a normal item in the drawer so we have to deselect the items in the StickyDrawer
        mDrawerBuilder.resetStickyFooterSelection();
    }
    return false;
}
Also used : IDrawerItem(com.mikepenz.materialdrawer.model.interfaces.IDrawerItem) AbstractDrawerItem(com.mikepenz.materialdrawer.model.AbstractDrawerItem)

Aggregations

AbstractDrawerItem (com.mikepenz.materialdrawer.model.AbstractDrawerItem)3 LinearLayout (android.widget.LinearLayout)2 IDrawerItem (com.mikepenz.materialdrawer.model.interfaces.IDrawerItem)2 Selectable (com.mikepenz.materialdrawer.model.interfaces.Selectable)2 Handler (android.os.Handler)1 RecyclerView (android.support.v7.widget.RecyclerView)1 View (android.view.View)1 FastAdapter (com.mikepenz.fastadapter.FastAdapter)1 IExpandable (com.mikepenz.fastadapter.IExpandable)1