美文网首页Android自定义控件终端研发部Android知识
materialdrawer抽屉详解 ~~就是有你需要的 ~~

materialdrawer抽屉详解 ~~就是有你需要的 ~~

作者: 品味与回味 | 来源:发表于2017-12-04 14:17 被阅读20次

继续上次说的


image.png

咱们用的时候 完全用不上啊。 怎么修改呢


image.png image.png

上面的两个 发现了有哪里改变了吗。 下面我们来讲解(抛开官网)

 headerResult = new AccountHeaderBuilder()
                .withActivity(this)
                .withTranslucentStatusBar(true)
                .withHeaderBackground(R.drawable.header)
                .withSelectionFirstLine("1")
                .withSelectionSecondLine("2")
//                .withOnlyMainProfileImageVisible(true)
//                .withOnlySmallProfileImagesVisible(true)
                .withSelectionListEnabled(false)

很明显可以看到你们需要的 然后就是最后一个行就是隐藏那个小箭头的作用。,这样的改变相信大家就会有很好的作用了

西面开始唠叨的环节,学渣带你扣,那咱么那就开始扣~~~~


image.png

  final IProfile profile = new ProfileDrawerItem().withName("Mike Penz").withEmail("mikepenz@gmail.com").withIcon("https://avatars3.githubusercontent.com/u/1476232?v=3&s=460").withIdentifier(100);
        final IProfile profile2 = new ProfileDrawerItem().withName("Demo User").withEmail("demo@github.com").withIcon("https://avatars2.githubusercontent.com/u/3597376?v=3&s=460").withIdentifier(101);
        final IProfile profile3 = new ProfileDrawerItem().withName("Max Muster").withEmail("max.mustermann@gmail.com").withIcon(R.drawable.profile2).withIdentifier(102);
        final IProfile profile4 = new ProfileDrawerItem().withName("Felix House").withEmail("felix.house@gmail.com").withIcon(R.drawable.profile3).withIdentifier(103);
        final IProfile profile5 = new ProfileDrawerItem().withName("Mr. X").withEmail("mister.x.super@gmail.com").withIcon(R.drawable.profile4).withIdentifier(104);
        final IProfile profile6 = new ProfileDrawerItem().withName("Batman").withEmail("batman@gmail.com").withIcon(R.drawable.profile5).withIdentifier(105);

上面这个就是官网给的 接口去实例化一个对象,这个对象就是上面的最早的那些有emil图片名字那些。但是那些对我们日常的设计来说并没有什么用处。但是我们还是要了解一下。这个思路怎么。来作为我们一种结束的积累

image.png

直接粘贴了 这个就是实例化的那个对象。下面那些都是一些方法 来设置页面的值。重点不在这里,我们先来了解第一眼我们看到的

ProfileDrawerItem extends AbstractDrawerItem<ProfileDrawerItem, ProfileDrawerItem.ViewHolder> implements IProfile<ProfileDrawerItem>, Tagable<ProfileDrawerItem>, Typefaceable<ProfileDrawerItem> {

我曹第一眼。好复杂。


image.png

下面让我们静下心来 好好分析一下。

implements IProfile<ProfileDrawerItem>, Tagable<ProfileDrawerItem>, Typefaceable<ProfileDrawerItem> 

实现了三个接口。并且接口自带泛型 ,我们来分析一个这三个接口有什么用处

public interface IProfile<T> extends IIdentifyable<T> {
    T withName(String name);

    StringHolder getName();

    T withEmail(String email);

    StringHolder getEmail();

    T withIcon(Drawable icon);

    T withIcon(Bitmap bitmap);

    T withIcon(@DrawableRes int iconRes);

    T withIcon(String url);

    T withIcon(Uri uri);

    T withIcon(IIcon icon);

    ImageHolder getIcon();

    T withSelectable(boolean selectable);

    boolean isSelectable();
}

很直观就是一个设置图片什么的。 针对各种来源。

public interface Tagable<T> {
    T withTag(Object tag);

    Object getTag();
}

这个接口也很直观 。 就是设置tag ,。相信用过listview的都知道。但是在ProfileDrawerItem中并没有,为什么呢?在父类已经实现这个方法了。(有需要的同学可以看我的其他的系列讲泛型的)

public interface Typefaceable<T> {
    T withTypeface(Typeface typeface);

    Typeface getTypeface();
}

设置字体的

ProfileDrawerItem 通过这几个接口来实现那些公共的方法。 下面我们来看看他的父类AbstractDrawerItem 这个类从名字我们就可以看出来用图抽象抽屉item。

public abstract class AbstractDrawerItem<T, VH extends RecyclerView.ViewHolder> implements IDrawerItem<T, VH>, Selectable<T>, Tagable<T> {

从他实现的接口上来看 他三个接口的三个作用分别是
IDrawerItem

public interface IDrawerItem<T, VH extends RecyclerView.ViewHolder> extends IItem<T, VH>, IExpandable<T, IDrawerItem>, ISubItem<IDrawerItem, IDrawerItem> {

    Object getTag();

    boolean isEnabled();

    boolean isSelected();

    T withSetSelected(boolean selected);

    boolean isSelectable();

    T withSelectable(boolean selectable);

    int getType();

    int getLayoutRes();

    View generateView(Context ctx);

    View generateView(Context ctx, ViewGroup parent);

    VH getViewHolder(ViewGroup parent);

    void unbindView(VH holder);

    void bindView(VH holder, List<Object> payloads);

    boolean equals(long id);

就像listview一样 设置是不是点击 连接等 各种状态,另外仔细看 他还继承了三个接口,都是一系列各种状态的接口

然后我们看一下

  // Create the AccountHeader
        headerResult = new AccountHeaderBuilder()
                .withActivity(this)
                .withTranslucentStatusBar(true)
                .withHeaderBackground(R.drawable.header)
                .withSelectionFirstLine("1")
                .withSelectionSecondLine("2")
//                .withOnlyMainProfileImageVisible(true)
//                .withOnlySmallProfileImagesVisible(true)
                .withSelectionListEnabled(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(new IconicsDrawable(this, GoogleMaterial.Icon.gmd_plus).actionBar().paddingDp(5).colorRes(R.color.material_drawer_primary_text)).withIdentifier(PROFILE_SETTING),
//                        new ProfileSettingDrawerItem().withName("Manage Account").withIcon(GoogleMaterial.Icon.gmd_settings).withIdentifier(100001)
//                )
                .withOnAccountHeaderListener(new AccountHeader.OnAccountHeaderListener() {
                    @Override
                    public boolean onProfileChanged(View view, IProfile profile, boolean current) {
                        //sample usage of the onProfileChanged listener
                        //if the clicked item has the identifier 1 add a new profile ;)
                        if (profile instanceof IDrawerItem && profile.getIdentifier() == PROFILE_SETTING) {
                            int count = 100 + headerResult.getProfiles().size() + 1;
                            IProfile newProfile = new ProfileDrawerItem().withNameShown(true).withName("Batman" + count).withEmail("batman" + count + "@gmail.com").withIcon(R.drawable.profile5).withIdentifier(count);
                            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();

这些方法就是最字头对

image.png

这些事件的一个处理,还有背景等一些显示。 文章开头那些常用的样式就可以按照我一开发的那样去写
AccountHeaderBuilder 类
builder涉及模式

里面的大部分方法都是设定一下 我们从build去看

public AccountHeader build() {
        // if the user has not set a accountHeader use the default one :D
        if (mAccountHeaderContainer == null) {
            withAccountHeader(-1);
        }

        // get the header view within the container
        mAccountHeader = mAccountHeaderContainer.findViewById(R.id.material_drawer_account_header);

        //the default min header height by default 148dp
        int defaultHeaderMinHeight = mActivity.getResources().getDimensionPixelSize(R.dimen.material_drawer_account_header_height);
        int statusBarHeight = UIUtils.getStatusBarHeight(mActivity, true);

        // handle the height for the header
        int height;
        if (mHeight != null) {
            height = mHeight.asPixel(mActivity);
        } else {
            if (mCompactStyle) {
                height = mActivity.getResources().getDimensionPixelSize(R.dimen.material_drawer_account_header_height_compact);
            } else {
                //calculate the header height by getting the optimal drawer width and calculating it * 9 / 16
                height = (int) (DrawerUIUtils.getOptimalDrawerWidth(mActivity) * AccountHeader.NAVIGATION_DRAWER_ACCOUNT_ASPECT_RATIO);

                //if we are lower than api 19 (>= 19 we have a translucentStatusBar) the height should be a bit lower
                //probably even if we are non translucent on > 19 devices?
                if (Build.VERSION.SDK_INT < 19) {
                    int tempHeight = height - statusBarHeight;
                    //if we are lower than api 19 we are not able to have a translucent statusBar so we remove the height of the statusBar from the padding
                    //to prevent display issues we only reduce the height if we still fit the required minHeight of 148dp (R.dimen.material_drawer_account_header_height)
                    //we remove additional 8dp from the defaultMinHeaderHeight as there is some buffer in the header and to prevent to large spacings
                    if (tempHeight > defaultHeaderMinHeight - UIUtils.convertDpToPixel(8, mActivity)) {
                        height = tempHeight;
                    }
                }
            }
        }

        // handle everything if we have a translucent status bar which only is possible on API >= 19
        if (mTranslucentStatusBar && Build.VERSION.SDK_INT >= 21) {
            mAccountHeader.setPadding(mAccountHeader.getPaddingLeft(), mAccountHeader.getPaddingTop() + statusBarHeight, mAccountHeader.getPaddingRight(), mAccountHeader.getPaddingBottom());
            //in fact it makes no difference if we have a translucent statusBar or not. we want 9/16 just if we are not compact
            if (mCompactStyle) {
                height = height + statusBarHeight;
            } else if ((height - statusBarHeight) <= defaultHeaderMinHeight) {
                //if the height + statusBar of the header is lower than the required 148dp + statusBar we change the height to be able to display all the data
                height = defaultHeaderMinHeight + statusBarHeight;
            }
        }

        //set the height for the header
        setHeaderHeight(height);

        // get the background view
        mAccountHeaderBackground = (ImageView) mAccountHeaderContainer.findViewById(R.id.material_drawer_account_header_background);
        // set the background
        ImageHolder.applyTo(mHeaderBackground, mAccountHeaderBackground, DrawerImageLoader.Tags.ACCOUNT_HEADER.name());

        if (mHeaderBackgroundScaleType != null) {
            mAccountHeaderBackground.setScaleType(mHeaderBackgroundScaleType);
        }

        // get the text color to use for the text section
        int textColor = ColorHolder.color(mTextColor, mActivity, R.attr.material_drawer_header_selection_text, R.color.material_drawer_header_selection_text);

        // set the background for the section
        if (mCompactStyle) {
            mAccountHeaderTextSection = mAccountHeader;
        } else {
            mAccountHeaderTextSection = mAccountHeaderContainer.findViewById(R.id.material_drawer_account_header_text_section);
        }

        mAccountHeaderTextSectionBackgroundResource = UIUtils.getSelectableBackgroundRes(mActivity);
        handleSelectionView(mCurrentProfile, true);

        // set the arrow :D
        mAccountSwitcherArrow = (ImageView) mAccountHeaderContainer.findViewById(R.id.material_drawer_account_header_text_switcher);
        mAccountSwitcherArrow.setImageDrawable(new IconicsDrawable(mActivity, MaterialDrawerFont.Icon.mdf_arrow_drop_down).sizeRes(R.dimen.material_drawer_account_header_dropdown).paddingRes(R.dimen.material_drawer_account_header_dropdown_padding).color(textColor));

        //get the fields for the name
        mCurrentProfileView = (BezelImageView) mAccountHeader.findViewById(R.id.material_drawer_account_header_current);
        mCurrentProfileName = (TextView) mAccountHeader.findViewById(R.id.material_drawer_account_header_name);
        mCurrentProfileEmail = (TextView) mAccountHeader.findViewById(R.id.material_drawer_account_header_email);

        //set the typeface for the AccountHeader
        if (mNameTypeface != null) {
            mCurrentProfileName.setTypeface(mNameTypeface);
        } else if (mTypeface != null) {
            mCurrentProfileName.setTypeface(mTypeface);
        }

        if (mEmailTypeface != null) {
            mCurrentProfileEmail.setTypeface(mEmailTypeface);
        } else if (mTypeface != null) {
            mCurrentProfileEmail.setTypeface(mTypeface);
        }

        mCurrentProfileName.setTextColor(textColor);
        mCurrentProfileEmail.setTextColor(textColor);

        mProfileFirstView = (BezelImageView) mAccountHeader.findViewById(R.id.material_drawer_account_header_small_first);
        mProfileSecondView = (BezelImageView) mAccountHeader.findViewById(R.id.material_drawer_account_header_small_second);
        mProfileThirdView = (BezelImageView) mAccountHeader.findViewById(R.id.material_drawer_account_header_small_third);

这是build的一部分方法 想看就看 不想看不看也行 就是一些高度的设置等。 字体什么。

最有用图的就是后半段

     calculateProfiles();

        //process and build the profiles
        buildProfiles();

        // try to restore all saved values again
        if (mSavedInstance != null) {
            int selection = mSavedInstance.getInt(AccountHeader.BUNDLE_SELECTION_HEADER, -1);
            if (selection != -1) {
                //predefine selection (should be the first element
                if (mProfiles != null && (selection) > -1 && selection < mProfiles.size()) {
                    switchProfiles(mProfiles.get(selection));
                }
            }
        }

        //everything created. now set the header
        if (mDrawer != null) {
            mDrawer.setHeader(mAccountHeaderContainer, mPaddingBelowHeader, mDividerBelowHeader);
        }

        //forget the reference to the activity
        mActivity = null;

        return new AccountHeader(this);

这段主要就是设置那些emil 等那些 但是个人觉得这些没必要。策划菜单还没见过那么设计的 上面设计这些主要就是Head头部分。 并且设置一些监听。 剩下的 我们来看重点的部分。
result = new DrawerBuilder() 
设计模式同上。 咱们主要就其中的方法进行分析
 前300行代码不解释 绑定页面。 toolbar  等一些

开始分析方法。
1 如何改变下面内容的颜色

.withSliderBackgroundColor(getResources().getColor(R.color.colorPrimaryDark))
.withSliderBackgroundColorRes(R.color.colorPrimaryDark) 两个方法一样

image.png

.withSliderBackgroundDrawableRes(R.drawable.header)
withSliderBackgroundDrawable设置背景图片的

image.png

withDrawerWidthPx 设置侧滑出来的宽度 一般不需要设置

 .withDrawerGravity(GravityCompat.END) 设置右滑或者左滑
image.png

.withAccountHeader(headerResult) //set the AccountHeader we created earlier for the header
这个很简单就是把我们刚才设置HEad 显示出来

.withActionBarDrawerToggleAnimated(true) 一個动态效果左侧按钮

image.png image.png

按钮有一个平滑的过渡

.withScrollToTopAfterClick(true) 这个方法也很好理解。 在你点击一个item之后 在打开这个侧滑的时候 你想要是直接拉到底的还是在最上面、

withSelectedItemByPosition 一开始选择的默认选择的

addDrawerItems 增加内容的各种内容

好了 到这里就差不多了 大部分的样式都是可以满足的。 如果有不同需要的。可以查看源码

相关文章

网友评论

    本文标题:materialdrawer抽屉详解 ~~就是有你需要的 ~~

    本文链接:https://www.haomeiwen.com/subject/wcvwbxtx.html