在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):zsmb13/MaterialDrawerKt开源软件地址(OpenSource Url):https://github.com/zsmb13/MaterialDrawerKt开源编程语言(OpenSource Language):Kotlin 96.5%开源软件介绍(OpenSource Introduction):MaterialDrawerKtCreate navigation drawers in your Activities and Fragments without having to write any XML, in pure Kotlin code, with access to all the features of the original library: all sorts of menu items, badges, account headers, and more. This library is a Kotlin DSL wrapper around the mikepenz/MaterialDrawer library, and features:
Sample appYou can find the sample app in the Play Store, and its source code in the app module of the project. SetupThe library is hosted on implementation 'co.zsmb:materialdrawer-kt:3.0.0'
// required support lib modules
implementation "androidx.appcompat:appcompat:${versions.appcompat}"
implementation "androidx.recyclerview:recyclerview:${versions.recyclerView}"
implementation "androidx.annotation:annotation:${versions.annotation}"
implementation "com.google.android.material:material:${versions.material}"
implementation "androidx.constraintlayout:constraintlayout:${versions.constraintLayout}" In general, you don't have to include the original library separately. (See the note in the FAQ.) If you want to use the pre-Kotlin version of the base library for some reason, you can use the last 2.x release of the library found here. If you're not using AndroidX yet, you can use the last 1.x release of the library found here. Basic usageJust as a general note, try using the in-IDE documentation when you're in doubt about anything. It's much more detailed than the original library's docs, this was one of the design goals of the library. The IntelliJ/Android Studio shortcut for bringing up the docs about a function is To add a navigation drawer, you just have to add the following to your Activity's drawer {} This will give you an empty sheet that you can drag in from the left side of the screen. You can add menu items to it like this: drawer {
primaryItem("Home") {}
divider {}
primaryItem("Users") {}
secondaryItem("Settings") {}
} For all the available types of menu items, see the "Drawer item types" in the sample app. You can modify properties of the drawer inside the drawer {
headerViewRes = R.layout.header
closeOnClick = false
primaryItem("Home") {
icon = R.drawable.ic_home
}
divider {}
primaryItem("Users") {
icon = R.drawable.ic_user
}
secondaryItem("Settings") {
icon = R.drawable.ic_settings
selectable = false
}
} Note that most of these properties are non-readable, and can only be used for setting these values. This is why these properties are marked as deprecated, and will cause build errors. The rest should be safe to use to read back any values you've set, if you had to do that for whatever reason. For a complete reference of the wrapper methods and properties, see the list in the wiki. Advanced featuresAccount headersCreating an account header with profile entries can be done like so: drawer {
accountHeader {
profile("Samantha", "[email protected]") {
icon = "http://some.site/samantha.png"
}
profile("Laura", "[email protected]") {
icon = R.drawable.profile_laura
}
}
primaryItem("Home")
} Note that loading images from URLs requires additional setup, see the Image loading section of this document or the FootersYou can add items to an always visible, sticky footer in by nesting them inside a drawer {
footer {
primaryItem("Primary item")
secondaryItem("Secondary item")
}
} ListenersListeners can be added to both individual drawer items and the entire drawer. Some examples: drawer {
primaryItem("Item 1")
primaryItem("Item 2") {
// Called only when this item is clicked
onClick { _ ->
Log.d("DRAWER", "Click.")
false
}
}
// Called when any drawer item is clicked
onItemClick { _, position, _ ->
Log.d("DRAWER", "Item $position clicked")
false
}
onOpened {
Log.d("DRAWER", "Navigation drawer opened")
}
} More examples in the "Listeners" section of the sample app. BadgesAdd badges to drawer items, and customize them with this syntax: drawer {
primaryItem {
badge("111") {
cornersDp = 0
color = 0xFF0099FF
colorPressed = 0xFFCC99FF
}
}
} You can see more examples in the "Badges" section of the sample app. Conversion from originalThis is a rough guide to how the original API's features are converted to the DSL, for those who are already familiar with the original library. BuildersBuilders are replaced by functions that are named without the "Builder" suffix. DrawerBuilder()
.withActivity(this)
.build() ... is replaced with ... drawer {
} DrawerItemsCalls to XyzDrawerItem classes are replaced with functions as well. The "Drawer" word is omitted from the function's name. Note that properties like names and descriptions of the drawer items become parameters of these functions. For example: PrimaryDrawerItem().withName("Item name") ... is replaced with ... primaryItem("Item name") {
}
|
Parameter type | Property name |
---|---|
Int | headerBackground headerBackgroundRes |
Drawable | headerBackgroundDrawable |
ImageHolder | headerBackgroundImage |
There may be defaults without suffixes for what's assumed to be the most popular use case.
Adding simple listeners to drawers (or individual drawer items) are done with onXyz function calls, which take lambdas as parameters. For example:
DrawerBuilder()
.withActivity(this)
.withOnDrawerItemClickListener(object : Drawer.OnDrawerItemClickListener {
override fun onItemClick(view: View, position: Int, drawerItem: IDrawerItem<out Any?, out RecyclerView.ViewHolder>?): Boolean {
Log.d("DRAWER", "Clicked!")
return true
}
})
.build()
... is replaced with ...
drawer {
onItemClick { view, position, drawerItem ->
Log.d("DRAWER", "Clicked!")
true
}
}
Listeners that originally have multiple callbacks have been broken up into individual functions:
DrawerBuilder()
.withActivity(this)
.withOnDrawerListener(object : Drawer.OnDrawerListener {
override fun onDrawerSlide(drawerView: View?, slideOffset: Float) {
Log.d("DRAWER", "Sliding")
}
override fun onDrawerClosed(drawerView: View?) {
Log.d("DRAWER", "Closed")
}
override fun onDrawerOpened(drawerView: View?) {
Log.d("DRAWER", "Opened")
}
})
.build()
... is replaced with ...
drawer {
onSlide { _, _ ->
Log.d("DRAWER", "Sliding")
}
onClosed {
Log.d("DRAWER", "Closed")
}
onOpened {
Log.d("DRAWER", "Opened")
}
}
Since the MaterialDrawer library doesn't include its own image loading solution, you have to set one up yourself. You have to do this before the first time MaterialDrawer has to load an image, for example, in your Application's onCreate
method.
With the original library, this setup looks like this (Picasso is just used as an example):
DrawerImageLoader.init(object: AbstractDrawerImageLoader() {
override fun placeholder(ctx: Context, tag: String?): Drawable {
return DrawerUIUtils.getPlaceHolder(ctx)
}
override fun set(imageView: ImageView, uri: Uri, placeholder: Drawable?, tag: String?) {
Picasso.with(imageView.context)
.load(uri)
.placeholder(placeholder)
.into(imageView)
}
override fun cancel(imageView: ImageView) {
Picasso.with(imageView.context)
.cancelRequest(imageView)
}
})
This can be replaced by the following:
drawerImageLoader {
placeholder { ctx, tag ->
DrawerUIUtils.getPlaceHolder(ctx)
}
set { imageView, uri, placeholder, tag ->
Picasso.with(imageView.context)
.load(uri)
.placeholder(placeholder)
.into(imageView)
}
cancel { imageView ->
Picasso.with(imageView.context)
.cancelRequest(imageView)
}
}
If the base library gets features and they aren't ported to this wrapper yet, you can include that as a dependency in addition to this one, and use the two together. For these purposes, the internal DrawerBuilder
that this library uses is exposed through a property, and you can access it like so:
drawer {
builder.withKeepStickyItemsVisible(true)
}
The internal AccountHeaderBuilder
is exposed in the same way:
accountHeader {
builder.withHeightDp(20)
}
As for drawer items, you can just use the original API's calls on the items that are returned:
val item = primaryItem("Hello") {
icon = R.drawable.profile
}
item.withBadge("10")
primaryItem("Hello") {
icon = R.drawable.profile
}.withBadge("10")
Copyright 2020 Marton Braun
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论