Search in sources :

Example 11 with IDrawerItem

use of com.mikepenz.materialdrawer.model.interfaces.IDrawerItem in project MaterialDrawer by mikepenz.

the class Drawer method updateBadge.

/**
     * update the badge for a specific drawerItem
     * identified by its id
     *
     * @param identifier
     * @param badge
     */
public void updateBadge(long identifier, StringHolder badge) {
    IDrawerItem drawerItem = getDrawerItem(identifier);
    if (drawerItem instanceof Badgeable) {
        Badgeable badgeable = (Badgeable) drawerItem;
        badgeable.withBadge(badge);
        updateItem((IDrawerItem) badgeable);
    }
}
Also used : IDrawerItem(com.mikepenz.materialdrawer.model.interfaces.IDrawerItem) Badgeable(com.mikepenz.materialdrawer.model.interfaces.Badgeable)

Example 12 with IDrawerItem

use of com.mikepenz.materialdrawer.model.interfaces.IDrawerItem 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 13 with IDrawerItem

use of com.mikepenz.materialdrawer.model.interfaces.IDrawerItem in project LollipopShowcase by mikepenz.

the class MainActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    // Handle Toolbar
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    final SharedPreferences pref = getSharedPreferences("com.mikepenz.applicationreader", 0);
    drawer = new DrawerBuilder(this).withToolbar(toolbar).addDrawerItems(new SwitchDrawerItem().withOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(IDrawerItem drawerItem, CompoundButton compoundButton, boolean b) {
            SharedPreferences.Editor editor = pref.edit();
            editor.putBoolean("autouploadenabled", b);
            editor.apply();
        }
    }).withName(R.string.drawer_switch).withChecked(pref.getBoolean("autouploadenabled", false))).addStickyDrawerItems(new SecondaryDrawerItem().withName(R.string.drawer_opensource).withIdentifier(DRAWER_ITEM_OPEN_SOURCE).withIcon(FontAwesome.Icon.faw_github).withSelectable(false)).withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {

        @Override
        public boolean onItemClick(View view, int i, IDrawerItem drawerItem) {
            if (drawerItem.getIdentifier() == DRAWER_ITEM_OPEN_SOURCE) {
                new LibsBuilder().withFields(R.string.class.getFields()).withVersionShown(true).withLicenseShown(true).withActivityTitle(getString(R.string.drawer_opensource)).withActivityStyle(Libs.ActivityStyle.LIGHT_DARK_TOOLBAR).start(MainActivity.this);
            }
            return false;
        }
    }).withSelectedItem(-1).withSavedInstance(savedInstanceState).build();
    // Handle ProgressBar
    mProgressBar = (ProgressBar) findViewById(R.id.progressBar);
    // Fab Button
    mFabButton = (FloatingActionButton) findViewById(R.id.fab_normal);
    mFabButton.setImageDrawable(new IconicsDrawable(this, GoogleMaterial.Icon.gmd_file_upload).color(Color.WHITE).actionBar());
    mFabButton.setOnClickListener(fabClickListener);
    mRecyclerView = (RecyclerView) findViewById(R.id.list);
    mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
    mRecyclerView.setItemAnimator(new CustomItemAnimator());
    //mRecyclerView.setItemAnimator(new ReboundItemAnimator());
    mAdapter = new ApplicationAdapter(new ArrayList<AppInfo>(), R.layout.row_application, MainActivity.this);
    mRecyclerView.setAdapter(mAdapter);
    mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_container);
    mSwipeRefreshLayout.setColorSchemeColors(getResources().getColor(R.color.theme_accent));
    mSwipeRefreshLayout.setRefreshing(true);
    mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {

        @Override
        public void onRefresh() {
            new InitializeApplicationsTask().execute();
        }
    });
    new InitializeApplicationsTask().execute();
    if (savedInstanceState != null) {
        if (uploadComponentInfoTask != null) {
            if (uploadComponentInfoTask.isRunning) {
                uploadComponentInfoTask.showProgress(this);
            }
        }
    }
    //show progress
    mRecyclerView.setVisibility(View.GONE);
    mProgressBar.setVisibility(View.VISIBLE);
}
Also used : IDrawerItem(com.mikepenz.materialdrawer.model.interfaces.IDrawerItem) OnCheckedChangeListener(com.mikepenz.materialdrawer.interfaces.OnCheckedChangeListener) SharedPreferences(android.content.SharedPreferences) ArrayList(java.util.ArrayList) SwitchDrawerItem(com.mikepenz.materialdrawer.model.SwitchDrawerItem) Drawer(com.mikepenz.materialdrawer.Drawer) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) SecondaryDrawerItem(com.mikepenz.materialdrawer.model.SecondaryDrawerItem) View(android.view.View) RecyclerView(android.support.v7.widget.RecyclerView) SwipeRefreshLayout(android.support.v4.widget.SwipeRefreshLayout) LibsBuilder(com.mikepenz.aboutlibraries.LibsBuilder) CustomItemAnimator(com.mikepenz.lollipopshowcase.itemanimator.CustomItemAnimator) DrawerBuilder(com.mikepenz.materialdrawer.DrawerBuilder) ApplicationAdapter(com.mikepenz.lollipopshowcase.adapter.ApplicationAdapter) IconicsDrawable(com.mikepenz.iconics.IconicsDrawable) CompoundButton(android.widget.CompoundButton) Toolbar(android.support.v7.widget.Toolbar)

Example 14 with IDrawerItem

use of com.mikepenz.materialdrawer.model.interfaces.IDrawerItem in project MaterialDrawer by mikepenz.

the class DrawerUtils method getStickyFooterPositionByIdentifier.

/**
     * calculates the position of an drawerItem inside the footer. searching by it's identifier
     *
     * @param identifier
     * @return
     */
public static int getStickyFooterPositionByIdentifier(DrawerBuilder drawer, long identifier) {
    if (identifier >= 0) {
        if (drawer.mStickyFooterView != null && drawer.mStickyFooterView instanceof LinearLayout) {
            LinearLayout footer = (LinearLayout) drawer.mStickyFooterView;
            int shadowOffset = 0;
            for (int i = 0; i < footer.getChildCount(); i++) {
                Object o = footer.getChildAt(i).getTag();
                //count up the shadowOffset to return the correct position of the given item
                if (o == null && drawer.mStickyFooterDivider) {
                    shadowOffset = shadowOffset + 1;
                }
                if (o != null && o instanceof IDrawerItem && ((IDrawerItem) o).getIdentifier() == identifier) {
                    return i - shadowOffset;
                }
            }
        }
    }
    return -1;
}
Also used : IDrawerItem(com.mikepenz.materialdrawer.model.interfaces.IDrawerItem) LinearLayout(android.widget.LinearLayout)

Example 15 with IDrawerItem

use of com.mikepenz.materialdrawer.model.interfaces.IDrawerItem in project MaterialDrawer by mikepenz.

the class MiniDrawer method setSelection.

/**
     * set the selection of the MiniDrawer
     *
     * @param identifier the identifier of the item which should be selected (-1 for none)
     */
public void setSelection(long identifier) {
    int count = mAdapter.getItemCount();
    for (int i = 0; i < count; i++) {
        IDrawerItem item = mAdapter.getItem(i);
        if (item.getIdentifier() == identifier && !item.isSelected()) {
            mAdapter.deselect();
            mAdapter.select(i);
        }
    }
}
Also used : IDrawerItem(com.mikepenz.materialdrawer.model.interfaces.IDrawerItem)

Aggregations

IDrawerItem (com.mikepenz.materialdrawer.model.interfaces.IDrawerItem)29 View (android.view.View)20 DrawerBuilder (com.mikepenz.materialdrawer.DrawerBuilder)16 PrimaryDrawerItem (com.mikepenz.materialdrawer.model.PrimaryDrawerItem)14 Toolbar (android.support.v7.widget.Toolbar)13 Drawer (com.mikepenz.materialdrawer.Drawer)12 SecondaryDrawerItem (com.mikepenz.materialdrawer.model.SecondaryDrawerItem)12 Nameable (com.mikepenz.materialdrawer.model.interfaces.Nameable)10 IProfile (com.mikepenz.materialdrawer.model.interfaces.IProfile)9 DividerDrawerItem (com.mikepenz.materialdrawer.model.DividerDrawerItem)8 AccountHeaderBuilder (com.mikepenz.materialdrawer.AccountHeaderBuilder)7 SectionDrawerItem (com.mikepenz.materialdrawer.model.SectionDrawerItem)7 ProfileDrawerItem (com.mikepenz.materialdrawer.model.ProfileDrawerItem)6 Intent (android.content.Intent)5 BadgeStyle (com.mikepenz.materialdrawer.holder.BadgeStyle)5 RecyclerView (android.support.v7.widget.RecyclerView)4 LinearLayout (android.widget.LinearLayout)4 LibsBuilder (com.mikepenz.aboutlibraries.LibsBuilder)4 IconicsDrawable (com.mikepenz.iconics.IconicsDrawable)4 ProfileSettingDrawerItem (com.mikepenz.materialdrawer.model.ProfileSettingDrawerItem)4