Search in sources :

Example 16 with IDrawerItem

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

the class MiniDrawer method createItems.

/**
     * creates the items for the MiniDrawer
     */
public void createItems() {
    mItemAdapter.clear();
    int profileOffset = 0;
    if (mAccountHeader != null && mAccountHeader.getAccountHeaderBuilder().mProfileImagesVisible) {
        IProfile profile = mAccountHeader.getActiveProfile();
        if (profile instanceof IDrawerItem) {
            mItemAdapter.add(generateMiniDrawerItem((IDrawerItem) profile));
            profileOffset = 1;
        }
    }
    int select = -1;
    if (mDrawer != null) {
        if (getDrawerItems() != null) {
            //migrate to miniDrawerItems
            int length = getDrawerItems().size();
            int position = 0;
            for (int i = 0; i < length; i++) {
                IDrawerItem miniDrawerItem = generateMiniDrawerItem(getDrawerItems().get(i));
                if (miniDrawerItem != null) {
                    if (miniDrawerItem.isSelected()) {
                        select = position;
                    }
                    mItemAdapter.add(miniDrawerItem);
                    position = position + 1;
                }
            }
            if (select >= 0) {
                //+1 because of the profile
                mAdapter.select(select + profileOffset);
            }
        }
    }
    //listener
    if (mOnMiniDrawerItemOnClickListener != null) {
        mAdapter.withOnClickListener(mOnMiniDrawerItemOnClickListener);
    } else {
        mAdapter.withOnClickListener(new FastAdapter.OnClickListener<IDrawerItem>() {

            @Override
            public boolean onClick(View v, IAdapter<IDrawerItem> adapter, final IDrawerItem item, final int position) {
                int type = getMiniDrawerType(item);
                //if a listener is defined and we consume the event return
                if (mOnMiniDrawerItemClickListener != null && mOnMiniDrawerItemClickListener.onItemClick(v, position, item, type)) {
                    return false;
                }
                if (type == ITEM) {
                    //fire the onClickListener also if the specific drawerItem is not Selectable
                    if (item.isSelectable()) {
                        //make sure we are on the original drawerItemList
                        if (mAccountHeader != null && mAccountHeader.isSelectionListShown()) {
                            mAccountHeader.toggleSelectionList(v.getContext());
                        }
                        if (!mDrawer.getDrawerItem(item.getIdentifier()).isSelected()) {
                            //set the selection
                            mDrawer.setSelection(item, true);
                        }
                    } else if (mDrawer.getOnDrawerItemClickListener() != null) {
                        //get the original `DrawerItem` from the Drawer as this one will contain all information
                        mDrawer.getOnDrawerItemClickListener().onItemClick(v, position, DrawerUtils.getDrawerItem(getDrawerItems(), item.getIdentifier()));
                    }
                } else if (type == PROFILE) {
                    if (mAccountHeader != null && !mAccountHeader.isSelectionListShown()) {
                        mAccountHeader.toggleSelectionList(v.getContext());
                    }
                    if (mCrossFader != null) {
                        mCrossFader.crossfade();
                    }
                }
                return false;
            }
        });
    }
    mAdapter.withOnLongClickListener(mOnMiniDrawerItemLongClickListener);
    mRecyclerView.scrollToPosition(0);
}
Also used : IDrawerItem(com.mikepenz.materialdrawer.model.interfaces.IDrawerItem) FastAdapter(com.mikepenz.fastadapter.FastAdapter) IProfile(com.mikepenz.materialdrawer.model.interfaces.IProfile) RecyclerView(android.support.v7.widget.RecyclerView) View(android.view.View)

Example 17 with IDrawerItem

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

the class MainActivity method setupDrawer.

private void setupDrawer() {
    headerResult = new AccountHeaderBuilder().withActivity(this).withSelectionListEnabledForSingleProfile(false).withDividerBelowHeader(false).withOnAccountHeaderProfileImageListener(new AccountHeader.OnAccountHeaderProfileImageListener() {

        @Override
        public boolean onProfileImageClick(View view, IProfile profile, boolean current) {
            if (current && loggedInAccountId != null) {
                Intent intent = new Intent(MainActivity.this, AccountActivity.class);
                intent.putExtra("id", loggedInAccountId);
                startActivity(intent);
                return true;
            }
            return false;
        }

        @Override
        public boolean onProfileImageLongClick(View view, IProfile profile, boolean current) {
            return false;
        }
    }).withCompactStyle(true).build();
    headerResult.getView().findViewById(R.id.material_drawer_account_header_current).setContentDescription(getString(R.string.action_view_profile));
    DrawerImageLoader.init(new AbstractDrawerImageLoader() {

        @Override
        public void set(ImageView imageView, Uri uri, Drawable placeholder) {
            Picasso.with(imageView.getContext()).load(uri).placeholder(placeholder).into(imageView);
        }

        @Override
        public void cancel(ImageView imageView) {
            Picasso.with(imageView.getContext()).cancelRequest(imageView);
        }
    });
    VectorDrawableCompat muteDrawable = VectorDrawableCompat.create(getResources(), R.drawable.ic_mute_24dp, getTheme());
    ThemeUtils.setDrawableTint(this, muteDrawable, R.attr.toolbar_icon_tint);
    drawer = new DrawerBuilder().withActivity(this).withAccountHeader(headerResult).withHasStableIds(true).withSelectedItem(-1).addDrawerItems(new PrimaryDrawerItem().withIdentifier(0).withName(getString(R.string.action_edit_profile)).withSelectable(false).withIcon(GoogleMaterial.Icon.gmd_person), new PrimaryDrawerItem().withIdentifier(1).withName(getString(R.string.action_view_favourites)).withSelectable(false).withIcon(GoogleMaterial.Icon.gmd_star), new PrimaryDrawerItem().withIdentifier(2).withName(getString(R.string.action_view_mutes)).withSelectable(false).withIcon(muteDrawable), new PrimaryDrawerItem().withIdentifier(3).withName(getString(R.string.action_view_blocks)).withSelectable(false).withIcon(GoogleMaterial.Icon.gmd_block), new DividerDrawerItem(), new SecondaryDrawerItem().withIdentifier(4).withName(getString(R.string.action_view_preferences)).withSelectable(false).withIcon(GoogleMaterial.Icon.gmd_settings), new SecondaryDrawerItem().withIdentifier(5).withName(getString(R.string.about_title_activity)).withSelectable(false).withIcon(GoogleMaterial.Icon.gmd_info), new SecondaryDrawerItem().withIdentifier(6).withName(getString(R.string.action_logout)).withSelectable(false).withIcon(GoogleMaterial.Icon.gmd_exit_to_app)).withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {

        @Override
        public boolean onItemClick(View view, int position, IDrawerItem drawerItem) {
            if (drawerItem != null) {
                long drawerItemIdentifier = drawerItem.getIdentifier();
                if (drawerItemIdentifier == 0) {
                    Intent intent = new Intent(MainActivity.this, EditProfileActivity.class);
                    startActivity(intent);
                } else if (drawerItemIdentifier == 1) {
                    Intent intent = new Intent(MainActivity.this, FavouritesActivity.class);
                    startActivity(intent);
                } else if (drawerItemIdentifier == 2) {
                    Intent intent = new Intent(MainActivity.this, AccountListActivity.class);
                    intent.putExtra("type", AccountListActivity.Type.MUTES);
                    startActivity(intent);
                } else if (drawerItemIdentifier == 3) {
                    Intent intent = new Intent(MainActivity.this, AccountListActivity.class);
                    intent.putExtra("type", AccountListActivity.Type.BLOCKS);
                    startActivity(intent);
                } else if (drawerItemIdentifier == 4) {
                    Intent intent = new Intent(MainActivity.this, PreferencesActivity.class);
                    startActivity(intent);
                } else if (drawerItemIdentifier == 5) {
                    Intent intent = new Intent(MainActivity.this, AboutActivity.class);
                    startActivity(intent);
                } else if (drawerItemIdentifier == 6) {
                    logout();
                } else if (drawerItemIdentifier == 7) {
                    Intent intent = new Intent(MainActivity.this, AccountListActivity.class);
                    intent.putExtra("type", AccountListActivity.Type.FOLLOW_REQUESTS);
                    startActivity(intent);
                }
            }
            return false;
        }
    }).build();
}
Also used : AbstractDrawerImageLoader(com.mikepenz.materialdrawer.util.AbstractDrawerImageLoader) PrimaryDrawerItem(com.mikepenz.materialdrawer.model.PrimaryDrawerItem) IDrawerItem(com.mikepenz.materialdrawer.model.interfaces.IDrawerItem) DividerDrawerItem(com.mikepenz.materialdrawer.model.DividerDrawerItem) Drawable(android.graphics.drawable.Drawable) AccountHeaderBuilder(com.mikepenz.materialdrawer.AccountHeaderBuilder) Intent(android.content.Intent) Drawer(com.mikepenz.materialdrawer.Drawer) ImageView(android.widget.ImageView) BindView(butterknife.BindView) View(android.view.View) FloatingSearchView(com.arlib.floatingsearchview.FloatingSearchView) TextView(android.widget.TextView) Uri(android.net.Uri) VectorDrawableCompat(android.support.graphics.drawable.VectorDrawableCompat) SecondaryDrawerItem(com.mikepenz.materialdrawer.model.SecondaryDrawerItem) ImageView(android.widget.ImageView) IProfile(com.mikepenz.materialdrawer.model.interfaces.IProfile) DrawerBuilder(com.mikepenz.materialdrawer.DrawerBuilder)

Example 18 with IDrawerItem

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

the class EmbeddedDrawerActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_embedded);
    // Handle Toolbar
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    //set the back arrow in the toolbar
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setTitle(R.string.drawer_item_embedded_drawer);
    // Create a few sample profile
    // NOTE you have to define the loader logic too. See the CustomApplication for more details
    final IProfile profile = new ProfileDrawerItem().withName("Mike Penz").withEmail("mikepenz@gmail.com").withIcon("https://avatars3.githubusercontent.com/u/1476232?v=3&s=460");
    final IProfile profile2 = new ProfileDrawerItem().withName("Bernat Borras").withEmail("alorma@github.com").withIcon(Uri.parse("https://avatars3.githubusercontent.com/u/887462?v=3&s=460"));
    final IProfile profile3 = new ProfileDrawerItem().withName("Max Muster").withEmail("max.mustermann@gmail.com").withIcon(getResources().getDrawable(R.drawable.profile2));
    final IProfile profile4 = new ProfileDrawerItem().withName("Felix House").withEmail("felix.house@gmail.com").withIcon(getResources().getDrawable(R.drawable.profile3));
    final IProfile profile5 = new ProfileDrawerItem().withName("Mr. X").withEmail("mister.x.super@gmail.com").withIcon(getResources().getDrawable(R.drawable.profile4)).withIdentifier(4);
    final IProfile profile6 = new ProfileDrawerItem().withName("Batman").withEmail("batman@gmail.com").withIcon(getResources().getDrawable(R.drawable.profile5));
    // Create the AccountHeader
    headerResult = new AccountHeaderBuilder().withActivity(this).withHeaderBackground(R.drawable.header).withTranslucentStatusBar(false).addProfiles(profile, profile2, profile3, profile4, profile5, profile6, //don't ask but google uses 14dp for the add account icon in gmail but 20dp for the normal icons (like manage account)
    new ProfileSettingDrawerItem().withName("Add Account").withDescription("Add new GitHub Account").withIcon(GoogleMaterial.Icon.gmd_plus).withIdentifier(PROFILE_SETTING), new ProfileSettingDrawerItem().withName("Manage Account").withIcon(GoogleMaterial.Icon.gmd_settings)).withOnAccountHeaderListener(new AccountHeader.OnAccountHeaderListener() {

        @Override
        public boolean onProfileChanged(View view, IProfile profile, boolean current) {
            //if the clicked item has the identifier 1 add a new profile ;)
            if (profile instanceof IDrawerItem && ((IDrawerItem) profile).getIdentifier() == PROFILE_SETTING) {
                IProfile newProfile = new ProfileDrawerItem().withNameShown(true).withName("Batman").withEmail("batman@gmail.com").withIcon(getResources().getDrawable(R.drawable.profile5));
                if (headerResult.getProfiles() != null) {
                    //we know that there are 2 setting elements. set the new profile above them ;)
                    headerResult.addProfile(newProfile, headerResult.getProfiles().size() - 2);
                } else {
                    headerResult.addProfiles(newProfile);
                }
            }
            //false if you have not consumed the event and it should close the drawer
            return false;
        }
    }).withSavedInstance(savedInstanceState).build();
    result = new DrawerBuilder().withActivity(this).withToolbar(toolbar).withTranslucentStatusBar(false).withAccountHeader(//set the AccountHeader we created earlier for the header
    headerResult).addDrawerItems(new PrimaryDrawerItem().withName(R.string.drawer_item_compact_header).withIcon(GoogleMaterial.Icon.gmd_sun).withIdentifier(1), new PrimaryDrawerItem().withName(R.string.drawer_item_action_bar_drawer).withIcon(FontAwesome.Icon.faw_home).withBadge("22").withBadgeStyle(new BadgeStyle(Color.RED, Color.RED)).withIdentifier(2), new PrimaryDrawerItem().withName(R.string.drawer_item_multi_drawer).withIcon(FontAwesome.Icon.faw_gamepad).withIdentifier(3), new PrimaryDrawerItem().withName(R.string.drawer_item_non_translucent_status_drawer).withIcon(FontAwesome.Icon.faw_eye).withIdentifier(4), new PrimaryDrawerItem().withDescription("A more complex sample").withName(R.string.drawer_item_advanced_drawer).withIcon(GoogleMaterial.Icon.gmd_adb).withIdentifier(5), new SectionDrawerItem().withName(R.string.drawer_item_section_header), new SecondaryDrawerItem().withName(R.string.drawer_item_open_source).withIcon(FontAwesome.Icon.faw_github), new SecondaryDrawerItem().withName(R.string.drawer_item_contact).withIcon(GoogleMaterial.Icon.gmd_format_color_fill).withTag("Bullhorn"), new DividerDrawerItem(), new SwitchDrawerItem().withName("Switch").withIcon(Octicons.Icon.oct_tools).withChecked(true).withOnCheckedChangeListener(onCheckedChangeListener), new ToggleDrawerItem().withName("Toggle").withIcon(Octicons.Icon.oct_tools).withChecked(true).withOnCheckedChangeListener(onCheckedChangeListener)).withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {

        @Override
        public boolean onItemClick(View view, int position, IDrawerItem drawerItem) {
            if (drawerItem instanceof Nameable) {
                Toast.makeText(EmbeddedDrawerActivity.this, ((Nameable) drawerItem).getName().getText(EmbeddedDrawerActivity.this), Toast.LENGTH_SHORT).show();
            }
            return false;
        }
    }).withSavedInstance(savedInstanceState).buildView();
    ((ViewGroup) findViewById(R.id.frame_container)).addView(result.getSlider());
}
Also used : IDrawerItem(com.mikepenz.materialdrawer.model.interfaces.IDrawerItem) PrimaryDrawerItem(com.mikepenz.materialdrawer.model.PrimaryDrawerItem) DividerDrawerItem(com.mikepenz.materialdrawer.model.DividerDrawerItem) ProfileSettingDrawerItem(com.mikepenz.materialdrawer.model.ProfileSettingDrawerItem) ProfileDrawerItem(com.mikepenz.materialdrawer.model.ProfileDrawerItem) ViewGroup(android.view.ViewGroup) AccountHeaderBuilder(com.mikepenz.materialdrawer.AccountHeaderBuilder) SwitchDrawerItem(com.mikepenz.materialdrawer.model.SwitchDrawerItem) View(android.view.View) SecondaryDrawerItem(com.mikepenz.materialdrawer.model.SecondaryDrawerItem) ToggleDrawerItem(com.mikepenz.materialdrawer.model.ToggleDrawerItem) Nameable(com.mikepenz.materialdrawer.model.interfaces.Nameable) SectionDrawerItem(com.mikepenz.materialdrawer.model.SectionDrawerItem) BadgeStyle(com.mikepenz.materialdrawer.holder.BadgeStyle) IProfile(com.mikepenz.materialdrawer.model.interfaces.IProfile) DrawerBuilder(com.mikepenz.materialdrawer.DrawerBuilder) Toolbar(android.support.v7.widget.Toolbar)

Example 19 with IDrawerItem

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

the class AccountHeaderBuilder method buildDrawerSelectionList.

/**
     * helper method to build and set the drawer selection list
     */
protected void buildDrawerSelectionList() {
    int selectedPosition = -1;
    int position = 0;
    ArrayList<IDrawerItem> profileDrawerItems = new ArrayList<>();
    if (mProfiles != null) {
        for (IProfile profile : mProfiles) {
            if (profile == mCurrentProfile) {
                if (mCurrentHiddenInList) {
                    continue;
                } else {
                    selectedPosition = mDrawer.mDrawerBuilder.getItemAdapter().getGlobalPosition(position);
                }
            }
            if (profile instanceof IDrawerItem) {
                ((IDrawerItem) profile).withSetSelected(false);
                profileDrawerItems.add((IDrawerItem) profile);
            }
            position = position + 1;
        }
    }
    mDrawer.switchDrawerContent(onDrawerItemClickListener, onDrawerItemLongClickListener, profileDrawerItems, selectedPosition);
}
Also used : IDrawerItem(com.mikepenz.materialdrawer.model.interfaces.IDrawerItem) ArrayList(java.util.ArrayList) IProfile(com.mikepenz.materialdrawer.model.interfaces.IProfile)

Example 20 with IDrawerItem

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

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