Repository files navigation

BottomDialog

心水纯纯写作很久的底部对话框样式,同时在Google Play Store 也见到过此样式,不过经过多次问询,没得到想要的结果。只好自己动手实现。

使用BottomSheet,支持滚动布局,同时底部布局不会因BottomSheet未显示全部内容而隐藏。

目录

特性

  • 高度自定义

    支持自定义头部布局(Toolbar...)、内容布局(列表、文字)和底部布局(按钮、BottomAppBar)

  • 底部布局自适应导航栏

    只在类原生机器测试过,不保证支持国产定制系统

  • 支持Activity形式的Dialog(BottomDialogActivity)

    有Context即可显示的对话框

  • 列表可操作list进行更新View

    支持监听List

DEMO

简单标题文字

BottomDialog.builder(this) {
title("Hello")
message(
buildString {
for (i in0..30) {
for (j in0..i *5) append(j)
appendln()
}
}, true
)
oneButton("OK", autoDismiss =true) {
//长按,更新内容布局
onLongClick { dialog ->
dialog.updateContent<MessageContentBuilder> {
text =Random().nextDouble().toString()
}
}
}
}

简单列表

val list =ObservableList.build<String?> {
for (i in0..50) add("item $i")
add("到底了")
}
BottomDialog.builder(this) {
this.title("Hello")
mutableList(list) { _, position, s, l ->
toast("clicked $s at $position longClick: $l")
}
buttons {
negativeButton()
neutralButton("removeAt(0)") {
if (list.isNotEmpty()) list.removeAt(0)
}
positiveButton("add(0, '...')") {
list.add(0, "...")
}
}
}

自定义列表

加载应用列表,AppListBuilder 见下文自定义布局构造器

BottomDialog.builder(this) {
title("应用列表")
content(AppListBuilder(this@MainActivity) { _, p, i, l ->
toast("$p\n$i\n$l")
})
oneButton("取消")
}

扩展

实现的更多的ContentBuilder

详见模块Extension

  • 仿一加系统分享对话框效果 [AwesomeHeader]

引入BottomDialog

  1. 在工程build.gradle添加
allprojects {
repositories {
//...
maven { url 'https://jitpack.io' }
}
}
  1. 添加依赖

最新版本 [AndroidX]:

  • BottomDialog
dependencies {
implementation 'com.github.Vove7.BottomDialog:bottomdialog:2.2.5'
}
  • 扩展包(可选)
dependencies {
implementation 'com.github.Vove7.BottomDialog:extension:2.2.5'
}

自定义布局构造器

1. 定义三层布局构造器

三层布局均可继承ContentBuilder

ToolbarHeader,其中title属性被listenToUpdate委托,在修改时,会通知updateContent进行更新布局。

classToolbarHeader(title:CharSequence?) : ContentBuilder() {
/** * 指定更新type = 1*/var title by listenToUpdate(title, this, type =1)
/** * 导航栏图标 type = 2*/var navIconId:Int? by listenToUpdate(null, this, type =2)
/** * 导航图标点击事件 type = 3*/var onIconClick:OnClick? by listenToUpdate(null, this, type =3)
overrideval layoutRes:Int=R.layout.header_toolbar
lateinitvar toolBar:Toolbar/** * 初始化View * @param view View*/overridefuninit(view:View) {
toolBar = view.tool_bar
}
/** * 进行视图更新 * @param type Int listenToUpdate中指定的type,初始化时type值为-1 * 可根据type值来选择更新视图,而不是全部更新 * @param data Any? 传递值*/overridefunupdateContent(type:Int, data:Any?) {
//type 为1 时,属性 title 被修改if (type ==-1|| type ==1) toolBar.title = title
if (type ==-1|| type ==2)
navIconId?.also {
toolBar.setNavigationIcon(it)
} ?: toolBar.setNavigationIcon(null)
if (type ==-1|| type ==3) {
toolBar.setNavigationOnClickListener {
onIconClick?.invoke(dialog)
}
}
}
}

2. 设置扩展函数

此操作可选,目的是为了方便在builder函数中调用。

已扩展的函数有:

//设置标题fun BottomDialogBuilder.title(title:CharSequence?): BottomDialogBuilder//设置内容fun BottomDialogBuilder.message(
text:String, selectable:Boolean = false
): BottomDialogBuilder//简单列表fun BottomDialogBuilder.simpleList(
items:List<String?>, autoDismiss:Boolean = true, onItemClick:OnItemClick<String?>
): BottomDialogBuilder/** * 三个按钮布局 * buttonPositive * buttonNegative * buttonNeutral*/fun BottomDialogBuilder.buttons(block:ButtonsBuilder.() ->Unit): BottomDialogBuilder//........ 更多参考Class: [BottomDialogBuilder]

如扩展BottomDialogBuilder一个toolbar函数:

/** * 头部使用Toolbar*/fun BottomDialogBuilder.toolbar(action:ToolbarHeader.() ->Unit): BottomDialogBuilder {
headerBuilder =ToolbarHeader().apply(action)
returnthis
}

使用:

BottomDialog.builder(this, show =true) {
toolbar {
title ="Hello"
navIconId =R.mipmap.ic_launcher
onIconClick = {
dialog.dismiss()
}
}
}

除了设置扩展函数,还可直接指定header(其他两种布局亦可,content, footer):

BottomDialog.builder(this) {
header(ToolbarHeader()) {
//...
}
}

3. 自定义列表内容布局

可继承ListAdapterBuilder快速实现。

可指定layoutManager

泛型T 可实现Typeable 区分元素类别,以构建不同样式

如 应用列表内容构造器 AppListBuilder

classAppListBuilder(
context:Context,
autoDismiss:Boolean = true,
privatevalappList:ObservableList<AppInfo> = ObservableList(),
onItemClick:OnItemClick<AppInfo>
) : ListAdapterBuilder<AppInfo>(applist, autoDismiss, onItemClick) {
init {
loading =true//加载视图
thread {
sleep(1500)
loadAppList(context)
}
}
//type 为元素类型,若items 未继承 Typeable: type = 0overrideval itemView: (type:Int) ->Int= { R.layout.item_app_list }
//item 绑定到视图overrideval bindView:BindView<AppInfo> = { view, item ->
view.text_1.text = item.name
view.text_2.text = item.pkg
}
privatefunloadAppList(context:Context) {
val pm = context.packageManager
appList.addAll(ObservableList.build {
pm.getInstalledPackages(0)?.forEach {
add(AppInfo(it.packageName, it.applicationInfo.loadLabel(pm)))
}
})
//停止加载
loading =false
}
}
data classAppInfo(
valpkg:String,
valname:CharSequence
)

使用:

BottomDialog.builder(this) {
title("应用列表")
//指定内容布局Builder
content(AppListBuilder(this@MainActivity) { _, p, i, l ->
toast("$p\n$i\n$l")
})
oneButton("取消")
}

适配主题

目前有两个自定义属性: ?attr/bd_bg_color 背景色 ?android:attr/textColorPrimary 文字颜色

在使用自定义主题时,需要指定上面两个属性:

<stylename="BottomDialog.Dark"parent="BottomDialog">
<itemname="bd_bg_color">#212121</item>
<itemname="android:textColorPrimary">#fff</item>
</style>

使用主题:

BottomDialog.builder(this) {
themeId =R.style.BottomDialog_Dark//...
}

注意自定义的 ContentBuilder 也需使用动态属性:

<androidx.appcompat.widget.Toolbar
android:background="?attr/bd_bg_color"app:titleTextColor="?android:attr/textColorPrimary" />

Faqs

1. show()过后如何更新布局?

此时比如更新message内容(内容布局类型为MessageContentBuilder)

dialog.updateContent<MessageContentBuilder> {
//text 属性被委托,才可通知布局刷新,见[MessageContentBuilder]
text ="new message"
}

2. 属性委托相关

当属性被委托后,改变值即可通知ContentBuilderupdateContent(type: Int, data: Any?)

如:

var title by listenToUpdate(title, this, 2)

当title值被修改后,会执行updateContent(2)

实现原理

  1. 列表类型为ObservableList,可监听内容变化,来通知Adapter更新布局

  2. 对话框底部布局,能够悬浮,由于:其中BottomSheet布局bs_rootfooter_lay同级。

布局文件dialog_content.xml。(已去除不重要属性)

<CoordinatorLayout>
<!--BottomSheet-->
<LinearLayoutandroid:id="@+id/bs_root"app:layout_behavior="android.support.design.widget.BottomSheetBehavior">
<!--头部布局-->
<FrameLayoutandroid:id="@+id/header_container" />
<NestedScrollViewandroid:id="@+id/container">
<!--内容布局-->
<FrameLayoutandroid:id="@+id/content"/>
</NestedScrollView>
</LinearLayout>
<LinearLayoutandroid:id="@+id/footer_lay"android:layout_alignParentBottom="true"android:layout_gravity="bottom" >
<!--底部布局-->
<FrameLayoutandroid:id="@+id/footer_contains" />
<!--用于撑起底部布局于导航栏之上-->
<Viewandroid:id="@+id/fill_nav" />
</LinearLayout>
</CoordinatorLayout>

详细内容请参考源码

About

可高度自定义的底部对话框,使用BottomSheet,支持滚动布局,同时底部布局不会因BottomSheet未显示全部内容而隐藏。

Topics

Resources

Stars

87 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages

, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Add copy buttons to all
 blocks\n(function() {\n function addCopyButtons() {\n document.querySelectorAll('pre code').forEach(function(codeBlock) {\n if (codeBlock.parentElement.hasAttribute('data-copy-added')) return;\n codeBlock.parentElement.setAttribute('data-copy-added', 'true');\n \n var btn = document.createElement('button');\n btn.textContent = 'Copy';\n btn.style.cssText = 'position:absolute;top:4px;right:4px;padding:2px 8px;font-size:11px;background:#4ecdc4;border:none;border-radius:4px;color:#1a1a2e;cursor:pointer;opacity:0.7;transition:opacity 0.2s;';\n btn.onmouseover = function() { this.style.opacity = '1'; };\n btn.onmouseout = function() { this.style.opacity = '0.7'; };\n btn.onclick = function() {\n navigator.clipboard.writeText(codeBlock.textContent).then(function() {\n btn.textContent = 'Copied!';\n setTimeout(function() { btn.textContent = 'Copy'; }, 1500);\n });\n };\n codeBlock.parentElement.style.position = 'relative';\n codeBlock.parentElement.appendChild(btn);\n });\n }\n \n addCopyButtons();\n \n // Re-run on dynamic content\n var observer = new MutationObserver(addCopyButtons);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Add Copy Buttons to Code Blocks");
}
} catch(__e) { console.warn('[Userscript:Add Copy Buttons to Code Blocks]', __e); }
})();
(function(){
try {
var __m = "github.com";
var __re = new RegExp('^' + "github\\.com" + '
Skip to content

Repository files navigation

BottomDialog

心水纯纯写作很久的底部对话框样式,同时在Google Play Store 也见到过此样式,不过经过多次问询,没得到想要的结果。只好自己动手实现。

使用BottomSheet,支持滚动布局,同时底部布局不会因BottomSheet未显示全部内容而隐藏。

目录

特性

  • 高度自定义

    支持自定义头部布局(Toolbar...)、内容布局(列表、文字)和底部布局(按钮、BottomAppBar)

  • 底部布局自适应导航栏

    只在类原生机器测试过,不保证支持国产定制系统

  • 支持Activity形式的Dialog(BottomDialogActivity)

    有Context即可显示的对话框

  • 列表可操作list进行更新View

    支持监听List

DEMO

简单标题文字

BottomDialog.builder(this) {
title("Hello")
message(
buildString {
for (i in0..30) {
for (j in0..i *5) append(j)
appendln()
}
}, true
)
oneButton("OK", autoDismiss =true) {
//长按,更新内容布局
onLongClick { dialog ->
dialog.updateContent<MessageContentBuilder> {
text =Random().nextDouble().toString()
}
}
}
}

简单列表

val list =ObservableList.build<String?> {
for (i in0..50) add("item $i")
add("到底了")
}
BottomDialog.builder(this) {
this.title("Hello")
mutableList(list) { _, position, s, l ->
toast("clicked $s at $position longClick: $l")
}
buttons {
negativeButton()
neutralButton("removeAt(0)") {
if (list.isNotEmpty()) list.removeAt(0)
}
positiveButton("add(0, '...')") {
list.add(0, "...")
}
}
}

自定义列表

加载应用列表,AppListBuilder 见下文自定义布局构造器

BottomDialog.builder(this) {
title("应用列表")
content(AppListBuilder(this@MainActivity) { _, p, i, l ->
toast("$p\n$i\n$l")
})
oneButton("取消")
}

扩展

实现的更多的ContentBuilder

详见模块Extension

  • 仿一加系统分享对话框效果 [AwesomeHeader]

引入BottomDialog

  1. 在工程build.gradle添加
allprojects {
repositories {
//...
maven { url 'https://jitpack.io' }
}
}
  1. 添加依赖

最新版本 [AndroidX]:

  • BottomDialog
dependencies {
implementation 'com.github.Vove7.BottomDialog:bottomdialog:2.2.5'
}
  • 扩展包(可选)
dependencies {
implementation 'com.github.Vove7.BottomDialog:extension:2.2.5'
}

自定义布局构造器

1. 定义三层布局构造器

三层布局均可继承ContentBuilder

ToolbarHeader,其中title属性被listenToUpdate委托,在修改时,会通知updateContent进行更新布局。

classToolbarHeader(title:CharSequence?) : ContentBuilder() {
/** * 指定更新type = 1*/var title by listenToUpdate(title, this, type =1)
/** * 导航栏图标 type = 2*/var navIconId:Int? by listenToUpdate(null, this, type =2)
/** * 导航图标点击事件 type = 3*/var onIconClick:OnClick? by listenToUpdate(null, this, type =3)
overrideval layoutRes:Int=R.layout.header_toolbar
lateinitvar toolBar:Toolbar/** * 初始化View * @param view View*/overridefuninit(view:View) {
toolBar = view.tool_bar
}
/** * 进行视图更新 * @param type Int listenToUpdate中指定的type,初始化时type值为-1 * 可根据type值来选择更新视图,而不是全部更新 * @param data Any? 传递值*/overridefunupdateContent(type:Int, data:Any?) {
//type 为1 时,属性 title 被修改if (type ==-1|| type ==1) toolBar.title = title
if (type ==-1|| type ==2)
navIconId?.also {
toolBar.setNavigationIcon(it)
} ?: toolBar.setNavigationIcon(null)
if (type ==-1|| type ==3) {
toolBar.setNavigationOnClickListener {
onIconClick?.invoke(dialog)
}
}
}
}

2. 设置扩展函数

此操作可选,目的是为了方便在builder函数中调用。

已扩展的函数有:

//设置标题fun BottomDialogBuilder.title(title:CharSequence?): BottomDialogBuilder//设置内容fun BottomDialogBuilder.message(
text:String, selectable:Boolean = false
): BottomDialogBuilder//简单列表fun BottomDialogBuilder.simpleList(
items:List<String?>, autoDismiss:Boolean = true, onItemClick:OnItemClick<String?>
): BottomDialogBuilder/** * 三个按钮布局 * buttonPositive * buttonNegative * buttonNeutral*/fun BottomDialogBuilder.buttons(block:ButtonsBuilder.() ->Unit): BottomDialogBuilder//........ 更多参考Class: [BottomDialogBuilder]

如扩展BottomDialogBuilder一个toolbar函数:

/** * 头部使用Toolbar*/fun BottomDialogBuilder.toolbar(action:ToolbarHeader.() ->Unit): BottomDialogBuilder {
headerBuilder =ToolbarHeader().apply(action)
returnthis
}

使用:

BottomDialog.builder(this, show =true) {
toolbar {
title ="Hello"
navIconId =R.mipmap.ic_launcher
onIconClick = {
dialog.dismiss()
}
}
}

除了设置扩展函数,还可直接指定header(其他两种布局亦可,content, footer):

BottomDialog.builder(this) {
header(ToolbarHeader()) {
//...
}
}

3. 自定义列表内容布局

可继承ListAdapterBuilder快速实现。

可指定layoutManager

泛型T 可实现Typeable 区分元素类别,以构建不同样式

如 应用列表内容构造器 AppListBuilder

classAppListBuilder(
context:Context,
autoDismiss:Boolean = true,
privatevalappList:ObservableList<AppInfo> = ObservableList(),
onItemClick:OnItemClick<AppInfo>
) : ListAdapterBuilder<AppInfo>(applist, autoDismiss, onItemClick) {
init {
loading =true//加载视图
thread {
sleep(1500)
loadAppList(context)
}
}
//type 为元素类型,若items 未继承 Typeable: type = 0overrideval itemView: (type:Int) ->Int= { R.layout.item_app_list }
//item 绑定到视图overrideval bindView:BindView<AppInfo> = { view, item ->
view.text_1.text = item.name
view.text_2.text = item.pkg
}
privatefunloadAppList(context:Context) {
val pm = context.packageManager
appList.addAll(ObservableList.build {
pm.getInstalledPackages(0)?.forEach {
add(AppInfo(it.packageName, it.applicationInfo.loadLabel(pm)))
}
})
//停止加载
loading =false
}
}
data classAppInfo(
valpkg:String,
valname:CharSequence
)

使用:

BottomDialog.builder(this) {
title("应用列表")
//指定内容布局Builder
content(AppListBuilder(this@MainActivity) { _, p, i, l ->
toast("$p\n$i\n$l")
})
oneButton("取消")
}

适配主题

目前有两个自定义属性: ?attr/bd_bg_color 背景色 ?android:attr/textColorPrimary 文字颜色

在使用自定义主题时,需要指定上面两个属性:

<stylename="BottomDialog.Dark"parent="BottomDialog">
<itemname="bd_bg_color">#212121</item>
<itemname="android:textColorPrimary">#fff</item>
</style>

使用主题:

BottomDialog.builder(this) {
themeId =R.style.BottomDialog_Dark//...
}

注意自定义的 ContentBuilder 也需使用动态属性:

<androidx.appcompat.widget.Toolbar
android:background="?attr/bd_bg_color"app:titleTextColor="?android:attr/textColorPrimary" />

Faqs

1. show()过后如何更新布局?

此时比如更新message内容(内容布局类型为MessageContentBuilder)

dialog.updateContent<MessageContentBuilder> {
//text 属性被委托,才可通知布局刷新,见[MessageContentBuilder]
text ="new message"
}

2. 属性委托相关

当属性被委托后,改变值即可通知ContentBuilderupdateContent(type: Int, data: Any?)

如:

var title by listenToUpdate(title, this, 2)

当title值被修改后,会执行updateContent(2)

实现原理

  1. 列表类型为ObservableList,可监听内容变化,来通知Adapter更新布局

  2. 对话框底部布局,能够悬浮,由于:其中BottomSheet布局bs_rootfooter_lay同级。

布局文件dialog_content.xml。(已去除不重要属性)

<CoordinatorLayout>
<!--BottomSheet-->
<LinearLayoutandroid:id="@+id/bs_root"app:layout_behavior="android.support.design.widget.BottomSheetBehavior">
<!--头部布局-->
<FrameLayoutandroid:id="@+id/header_container" />
<NestedScrollViewandroid:id="@+id/container">
<!--内容布局-->
<FrameLayoutandroid:id="@+id/content"/>
</NestedScrollView>
</LinearLayout>
<LinearLayoutandroid:id="@+id/footer_lay"android:layout_alignParentBottom="true"android:layout_gravity="bottom" >
<!--底部布局-->
<FrameLayoutandroid:id="@+id/footer_contains" />
<!--用于撑起底部布局于导航栏之上-->
<Viewandroid:id="@+id/fill_nav" />
</LinearLayout>
</CoordinatorLayout>

详细内容请参考源码

About

可高度自定义的底部对话框,使用BottomSheet,支持滚动布局,同时底部布局不会因BottomSheet未显示全部内容而隐藏。

Topics

Resources

Stars

87 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages

, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Force GitHub README to respect dark mode\n(function() {\n var style = document.createElement('style');\n style.textContent = '\n .markdown-body {\n color-scheme: dark light;\n }\n .markdown-body pre { background: #161b22 !important; }\n .markdown-body code { background: rgba(110, 118, 129, 0.4) !important; }\n .markdown-body table th, .markdown-body table td { border-color: #30363d !important; }\n .markdown-body img { background: #0d1117; }\n .markdown-body blockquote { border-left-color: #8b949e; }\n .markdown-body hr { border-color: #30363d; }\n ';\n document.head.appendChild(style);\n})();", "GitHub Dark Mode README Fix"); } } catch(__e) { console.warn('[Userscript:GitHub Dark Mode README Fix]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Repository files navigation

BottomDialog

心水纯纯写作很久的底部对话框样式,同时在Google Play Store 也见到过此样式,不过经过多次问询,没得到想要的结果。只好自己动手实现。

使用BottomSheet,支持滚动布局,同时底部布局不会因BottomSheet未显示全部内容而隐藏。

目录

特性

  • 高度自定义

    支持自定义头部布局(Toolbar...)、内容布局(列表、文字)和底部布局(按钮、BottomAppBar)

  • 底部布局自适应导航栏

    只在类原生机器测试过,不保证支持国产定制系统

  • 支持Activity形式的Dialog(BottomDialogActivity)

    有Context即可显示的对话框

  • 列表可操作list进行更新View

    支持监听List

DEMO

简单标题文字

BottomDialog.builder(this) {
title("Hello")
message(
buildString {
for (i in0..30) {
for (j in0..i *5) append(j)
appendln()
}
}, true
)
oneButton("OK", autoDismiss =true) {
//长按,更新内容布局
onLongClick { dialog ->
dialog.updateContent<MessageContentBuilder> {
text =Random().nextDouble().toString()
}
}
}
}

简单列表

val list =ObservableList.build<String?> {
for (i in0..50) add("item $i")
add("到底了")
}
BottomDialog.builder(this) {
this.title("Hello")
mutableList(list) { _, position, s, l ->
toast("clicked $s at $position longClick: $l")
}
buttons {
negativeButton()
neutralButton("removeAt(0)") {
if (list.isNotEmpty()) list.removeAt(0)
}
positiveButton("add(0, '...')") {
list.add(0, "...")
}
}
}

自定义列表

加载应用列表,AppListBuilder 见下文自定义布局构造器

BottomDialog.builder(this) {
title("应用列表")
content(AppListBuilder(this@MainActivity) { _, p, i, l ->
toast("$p\n$i\n$l")
})
oneButton("取消")
}

扩展

实现的更多的ContentBuilder

详见模块Extension

  • 仿一加系统分享对话框效果 [AwesomeHeader]

引入BottomDialog

  1. 在工程build.gradle添加
allprojects {
repositories {
//...
maven { url 'https://jitpack.io' }
}
}
  1. 添加依赖

最新版本 [AndroidX]:

  • BottomDialog
dependencies {
implementation 'com.github.Vove7.BottomDialog:bottomdialog:2.2.5'
}
  • 扩展包(可选)
dependencies {
implementation 'com.github.Vove7.BottomDialog:extension:2.2.5'
}

自定义布局构造器

1. 定义三层布局构造器

三层布局均可继承ContentBuilder

ToolbarHeader,其中title属性被listenToUpdate委托,在修改时,会通知updateContent进行更新布局。

classToolbarHeader(title:CharSequence?) : ContentBuilder() {
/** * 指定更新type = 1*/var title by listenToUpdate(title, this, type =1)
/** * 导航栏图标 type = 2*/var navIconId:Int? by listenToUpdate(null, this, type =2)
/** * 导航图标点击事件 type = 3*/var onIconClick:OnClick? by listenToUpdate(null, this, type =3)
overrideval layoutRes:Int=R.layout.header_toolbar
lateinitvar toolBar:Toolbar/** * 初始化View * @param view View*/overridefuninit(view:View) {
toolBar = view.tool_bar
}
/** * 进行视图更新 * @param type Int listenToUpdate中指定的type,初始化时type值为-1 * 可根据type值来选择更新视图,而不是全部更新 * @param data Any? 传递值*/overridefunupdateContent(type:Int, data:Any?) {
//type 为1 时,属性 title 被修改if (type ==-1|| type ==1) toolBar.title = title
if (type ==-1|| type ==2)
navIconId?.also {
toolBar.setNavigationIcon(it)
} ?: toolBar.setNavigationIcon(null)
if (type ==-1|| type ==3) {
toolBar.setNavigationOnClickListener {
onIconClick?.invoke(dialog)
}
}
}
}

2. 设置扩展函数

此操作可选,目的是为了方便在builder函数中调用。

已扩展的函数有:

//设置标题fun BottomDialogBuilder.title(title:CharSequence?): BottomDialogBuilder//设置内容fun BottomDialogBuilder.message(
text:String, selectable:Boolean = false
): BottomDialogBuilder//简单列表fun BottomDialogBuilder.simpleList(
items:List<String?>, autoDismiss:Boolean = true, onItemClick:OnItemClick<String?>
): BottomDialogBuilder/** * 三个按钮布局 * buttonPositive * buttonNegative * buttonNeutral*/fun BottomDialogBuilder.buttons(block:ButtonsBuilder.() ->Unit): BottomDialogBuilder//........ 更多参考Class: [BottomDialogBuilder]

如扩展BottomDialogBuilder一个toolbar函数:

/** * 头部使用Toolbar*/fun BottomDialogBuilder.toolbar(action:ToolbarHeader.() ->Unit): BottomDialogBuilder {
headerBuilder =ToolbarHeader().apply(action)
returnthis
}

使用:

BottomDialog.builder(this, show =true) {
toolbar {
title ="Hello"
navIconId =R.mipmap.ic_launcher
onIconClick = {
dialog.dismiss()
}
}
}

除了设置扩展函数,还可直接指定header(其他两种布局亦可,content, footer):

BottomDialog.builder(this) {
header(ToolbarHeader()) {
//...
}
}

3. 自定义列表内容布局

可继承ListAdapterBuilder快速实现。

可指定layoutManager

泛型T 可实现Typeable 区分元素类别,以构建不同样式

如 应用列表内容构造器 AppListBuilder

classAppListBuilder(
context:Context,
autoDismiss:Boolean = true,
privatevalappList:ObservableList<AppInfo> = ObservableList(),
onItemClick:OnItemClick<AppInfo>
) : ListAdapterBuilder<AppInfo>(applist, autoDismiss, onItemClick) {
init {
loading =true//加载视图
thread {
sleep(1500)
loadAppList(context)
}
}
//type 为元素类型,若items 未继承 Typeable: type = 0overrideval itemView: (type:Int) ->Int= { R.layout.item_app_list }
//item 绑定到视图overrideval bindView:BindView<AppInfo> = { view, item ->
view.text_1.text = item.name
view.text_2.text = item.pkg
}
privatefunloadAppList(context:Context) {
val pm = context.packageManager
appList.addAll(ObservableList.build {
pm.getInstalledPackages(0)?.forEach {
add(AppInfo(it.packageName, it.applicationInfo.loadLabel(pm)))
}
})
//停止加载
loading =false
}
}
data classAppInfo(
valpkg:String,
valname:CharSequence
)

使用:

BottomDialog.builder(this) {
title("应用列表")
//指定内容布局Builder
content(AppListBuilder(this@MainActivity) { _, p, i, l ->
toast("$p\n$i\n$l")
})
oneButton("取消")
}

适配主题

目前有两个自定义属性: ?attr/bd_bg_color 背景色 ?android:attr/textColorPrimary 文字颜色

在使用自定义主题时,需要指定上面两个属性:

<stylename="BottomDialog.Dark"parent="BottomDialog">
<itemname="bd_bg_color">#212121</item>
<itemname="android:textColorPrimary">#fff</item>
</style>

使用主题:

BottomDialog.builder(this) {
themeId =R.style.BottomDialog_Dark//...
}

注意自定义的 ContentBuilder 也需使用动态属性:

<androidx.appcompat.widget.Toolbar
android:background="?attr/bd_bg_color"app:titleTextColor="?android:attr/textColorPrimary" />

Faqs

1. show()过后如何更新布局?

此时比如更新message内容(内容布局类型为MessageContentBuilder)

dialog.updateContent<MessageContentBuilder> {
//text 属性被委托,才可通知布局刷新,见[MessageContentBuilder]
text ="new message"
}

2. 属性委托相关

当属性被委托后,改变值即可通知ContentBuilderupdateContent(type: Int, data: Any?)

如:

var title by listenToUpdate(title, this, 2)

当title值被修改后,会执行updateContent(2)

实现原理

  1. 列表类型为ObservableList,可监听内容变化,来通知Adapter更新布局

  2. 对话框底部布局,能够悬浮,由于:其中BottomSheet布局bs_rootfooter_lay同级。

布局文件dialog_content.xml。(已去除不重要属性)

<CoordinatorLayout>
<!--BottomSheet-->
<LinearLayoutandroid:id="@+id/bs_root"app:layout_behavior="android.support.design.widget.BottomSheetBehavior">
<!--头部布局-->
<FrameLayoutandroid:id="@+id/header_container" />
<NestedScrollViewandroid:id="@+id/container">
<!--内容布局-->
<FrameLayoutandroid:id="@+id/content"/>
</NestedScrollView>
</LinearLayout>
<LinearLayoutandroid:id="@+id/footer_lay"android:layout_alignParentBottom="true"android:layout_gravity="bottom" >
<!--底部布局-->
<FrameLayoutandroid:id="@+id/footer_contains" />
<!--用于撑起底部布局于导航栏之上-->
<Viewandroid:id="@+id/fill_nav" />
</LinearLayout>
</CoordinatorLayout>

详细内容请参考源码

About

可高度自定义的底部对话框,使用BottomSheet,支持滚动布局,同时底部布局不会因BottomSheet未显示全部内容而隐藏。

Topics

Resources

Stars

87 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages

, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Highlight search terms from Google/DuckDuckGo/Bing referrer\n(function() {\n var ref = document.referrer;\n var terms = [];\n \n if (ref.includes('google.com') || ref.includes('duckduckgo.com') || ref.includes('bing.com')) {\n var url = new URL(ref);\n var q = url.searchParams.get('q') || url.searchParams.get('p');\n if (q) {\n terms = q.split(/\\s+/).filter(function(t) { return t.length > 2; });\n }\n }\n \n if (terms.length === 0) return;\n \n var style = document.createElement('style');\n style.textContent = '.userscript-highlight { background: #fbbf24; color: #1a1a2e; padding: 1px 3px; border-radius: 2px; }';\n document.head.appendChild(style);\n \n function highlight(node) {\n if (node.nodeType === 3) { // text node\n var text = node.textContent;\n var found = false;\n terms.forEach(function(term) {\n var regex = new RegExp('(' + term.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\') + ')', 'gi');\n if (regex.test(text)) {\n found = true;\n var frag = document.createDocumentFragment();\n var parts = text.split(regex);\n parts.forEach(function(part, i) {\n if (i % 2 === 0) {\n frag.appendChild(document.createTextNode(part));\n } else {\n var span = document.createElement('span');\n span.className = 'userscript-highlight';\n span.textContent = part;\n frag.appendChild(span);\n }\n });\n node.parentNode.replaceChild(frag, node);\n }\n });\n } else if (node.nodeType === 1 && node.childNodes) { // element\n var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT'];\n if (!skipTags.includes(node.tagName)) {\n Array.from(node.childNodes).forEach(highlight);\n }\n }\n }\n \n highlight(document.body);\n \n // Re-highlight on dynamic content\n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1 || node.nodeType === 3) highlight(node);\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Highlight Search Terms"); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Repository files navigation

BottomDialog

心水纯纯写作很久的底部对话框样式,同时在Google Play Store 也见到过此样式,不过经过多次问询,没得到想要的结果。只好自己动手实现。

使用BottomSheet,支持滚动布局,同时底部布局不会因BottomSheet未显示全部内容而隐藏。

目录

特性

  • 高度自定义

    支持自定义头部布局(Toolbar...)、内容布局(列表、文字)和底部布局(按钮、BottomAppBar)

  • 底部布局自适应导航栏

    只在类原生机器测试过,不保证支持国产定制系统

  • 支持Activity形式的Dialog(BottomDialogActivity)

    有Context即可显示的对话框

  • 列表可操作list进行更新View

    支持监听List

DEMO

简单标题文字

BottomDialog.builder(this) {
title("Hello")
message(
buildString {
for (i in0..30) {
for (j in0..i *5) append(j)
appendln()
}
}, true
)
oneButton("OK", autoDismiss =true) {
//长按,更新内容布局
onLongClick { dialog ->
dialog.updateContent<MessageContentBuilder> {
text =Random().nextDouble().toString()
}
}
}
}

简单列表

val list =ObservableList.build<String?> {
for (i in0..50) add("item $i")
add("到底了")
}
BottomDialog.builder(this) {
this.title("Hello")
mutableList(list) { _, position, s, l ->
toast("clicked $s at $position longClick: $l")
}
buttons {
negativeButton()
neutralButton("removeAt(0)") {
if (list.isNotEmpty()) list.removeAt(0)
}
positiveButton("add(0, '...')") {
list.add(0, "...")
}
}
}

自定义列表

加载应用列表,AppListBuilder 见下文自定义布局构造器

BottomDialog.builder(this) {
title("应用列表")
content(AppListBuilder(this@MainActivity) { _, p, i, l ->
toast("$p\n$i\n$l")
})
oneButton("取消")
}

扩展

实现的更多的ContentBuilder

详见模块Extension

  • 仿一加系统分享对话框效果 [AwesomeHeader]

引入BottomDialog

  1. 在工程build.gradle添加
allprojects {
repositories {
//...
maven { url 'https://jitpack.io' }
}
}
  1. 添加依赖

最新版本 [AndroidX]:

  • BottomDialog
dependencies {
implementation 'com.github.Vove7.BottomDialog:bottomdialog:2.2.5'
}
  • 扩展包(可选)
dependencies {
implementation 'com.github.Vove7.BottomDialog:extension:2.2.5'
}

自定义布局构造器

1. 定义三层布局构造器

三层布局均可继承ContentBuilder

ToolbarHeader,其中title属性被listenToUpdate委托,在修改时,会通知updateContent进行更新布局。

classToolbarHeader(title:CharSequence?) : ContentBuilder() {
/** * 指定更新type = 1*/var title by listenToUpdate(title, this, type =1)
/** * 导航栏图标 type = 2*/var navIconId:Int? by listenToUpdate(null, this, type =2)
/** * 导航图标点击事件 type = 3*/var onIconClick:OnClick? by listenToUpdate(null, this, type =3)
overrideval layoutRes:Int=R.layout.header_toolbar
lateinitvar toolBar:Toolbar/** * 初始化View * @param view View*/overridefuninit(view:View) {
toolBar = view.tool_bar
}
/** * 进行视图更新 * @param type Int listenToUpdate中指定的type,初始化时type值为-1 * 可根据type值来选择更新视图,而不是全部更新 * @param data Any? 传递值*/overridefunupdateContent(type:Int, data:Any?) {
//type 为1 时,属性 title 被修改if (type ==-1|| type ==1) toolBar.title = title
if (type ==-1|| type ==2)
navIconId?.also {
toolBar.setNavigationIcon(it)
} ?: toolBar.setNavigationIcon(null)
if (type ==-1|| type ==3) {
toolBar.setNavigationOnClickListener {
onIconClick?.invoke(dialog)
}
}
}
}

2. 设置扩展函数

此操作可选,目的是为了方便在builder函数中调用。

已扩展的函数有:

//设置标题fun BottomDialogBuilder.title(title:CharSequence?): BottomDialogBuilder//设置内容fun BottomDialogBuilder.message(
text:String, selectable:Boolean = false
): BottomDialogBuilder//简单列表fun BottomDialogBuilder.simpleList(
items:List<String?>, autoDismiss:Boolean = true, onItemClick:OnItemClick<String?>
): BottomDialogBuilder/** * 三个按钮布局 * buttonPositive * buttonNegative * buttonNeutral*/fun BottomDialogBuilder.buttons(block:ButtonsBuilder.() ->Unit): BottomDialogBuilder//........ 更多参考Class: [BottomDialogBuilder]

如扩展BottomDialogBuilder一个toolbar函数:

/** * 头部使用Toolbar*/fun BottomDialogBuilder.toolbar(action:ToolbarHeader.() ->Unit): BottomDialogBuilder {
headerBuilder =ToolbarHeader().apply(action)
returnthis
}

使用:

BottomDialog.builder(this, show =true) {
toolbar {
title ="Hello"
navIconId =R.mipmap.ic_launcher
onIconClick = {
dialog.dismiss()
}
}
}

除了设置扩展函数,还可直接指定header(其他两种布局亦可,content, footer):

BottomDialog.builder(this) {
header(ToolbarHeader()) {
//...
}
}

3. 自定义列表内容布局

可继承ListAdapterBuilder快速实现。

可指定layoutManager

泛型T 可实现Typeable 区分元素类别,以构建不同样式

如 应用列表内容构造器 AppListBuilder

classAppListBuilder(
context:Context,
autoDismiss:Boolean = true,
privatevalappList:ObservableList<AppInfo> = ObservableList(),
onItemClick:OnItemClick<AppInfo>
) : ListAdapterBuilder<AppInfo>(applist, autoDismiss, onItemClick) {
init {
loading =true//加载视图
thread {
sleep(1500)
loadAppList(context)
}
}
//type 为元素类型,若items 未继承 Typeable: type = 0overrideval itemView: (type:Int) ->Int= { R.layout.item_app_list }
//item 绑定到视图overrideval bindView:BindView<AppInfo> = { view, item ->
view.text_1.text = item.name
view.text_2.text = item.pkg
}
privatefunloadAppList(context:Context) {
val pm = context.packageManager
appList.addAll(ObservableList.build {
pm.getInstalledPackages(0)?.forEach {
add(AppInfo(it.packageName, it.applicationInfo.loadLabel(pm)))
}
})
//停止加载
loading =false
}
}
data classAppInfo(
valpkg:String,
valname:CharSequence
)

使用:

BottomDialog.builder(this) {
title("应用列表")
//指定内容布局Builder
content(AppListBuilder(this@MainActivity) { _, p, i, l ->
toast("$p\n$i\n$l")
})
oneButton("取消")
}

适配主题

目前有两个自定义属性: ?attr/bd_bg_color 背景色 ?android:attr/textColorPrimary 文字颜色

在使用自定义主题时,需要指定上面两个属性:

<stylename="BottomDialog.Dark"parent="BottomDialog">
<itemname="bd_bg_color">#212121</item>
<itemname="android:textColorPrimary">#fff</item>
</style>

使用主题:

BottomDialog.builder(this) {
themeId =R.style.BottomDialog_Dark//...
}

注意自定义的 ContentBuilder 也需使用动态属性:

<androidx.appcompat.widget.Toolbar
android:background="?attr/bd_bg_color"app:titleTextColor="?android:attr/textColorPrimary" />

Faqs

1. show()过后如何更新布局?

此时比如更新message内容(内容布局类型为MessageContentBuilder)

dialog.updateContent<MessageContentBuilder> {
//text 属性被委托,才可通知布局刷新,见[MessageContentBuilder]
text ="new message"
}

2. 属性委托相关

当属性被委托后,改变值即可通知ContentBuilderupdateContent(type: Int, data: Any?)

如:

var title by listenToUpdate(title, this, 2)

当title值被修改后,会执行updateContent(2)

实现原理

  1. 列表类型为ObservableList,可监听内容变化,来通知Adapter更新布局

  2. 对话框底部布局,能够悬浮,由于:其中BottomSheet布局bs_rootfooter_lay同级。

布局文件dialog_content.xml。(已去除不重要属性)

<CoordinatorLayout>
<!--BottomSheet-->
<LinearLayoutandroid:id="@+id/bs_root"app:layout_behavior="android.support.design.widget.BottomSheetBehavior">
<!--头部布局-->
<FrameLayoutandroid:id="@+id/header_container" />
<NestedScrollViewandroid:id="@+id/container">
<!--内容布局-->
<FrameLayoutandroid:id="@+id/content"/>
</NestedScrollView>
</LinearLayout>
<LinearLayoutandroid:id="@+id/footer_lay"android:layout_alignParentBottom="true"android:layout_gravity="bottom" >
<!--底部布局-->
<FrameLayoutandroid:id="@+id/footer_contains" />
<!--用于撑起底部布局于导航栏之上-->
<Viewandroid:id="@+id/fill_nav" />
</LinearLayout>
</CoordinatorLayout>

详细内容请参考源码

About

可高度自定义的底部对话框,使用BottomSheet,支持滚动布局,同时底部布局不会因BottomSheet未显示全部内容而隐藏。

Topics

Resources

Stars

87 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages

, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Strip utm_, fbclid, gclid, etc. from all links on page\n(function() {\n var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content',\n 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid',\n 'ref', 'ref_src', 'source', 'medium', 'campaign'];\n \n function cleanUrl(url) {\n try {\n var u = new URL(url, window.location.origin);\n var changed = false;\n trackingParams.forEach(function(p) {\n if (u.searchParams.has(p)) {\n u.searchParams.delete(p);\n changed = true;\n }\n });\n return changed ? u.toString() : url;\n } catch (e) {\n return url;\n }\n }\n \n function cleanLinks() {\n document.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n \n cleanLinks();\n \n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1) {\n if (node.tagName === 'A') cleanLinks();\n node.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Remove Tracking Parameters from Links"); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + '
Skip to content

Repository files navigation

BottomDialog

心水纯纯写作很久的底部对话框样式,同时在Google Play Store 也见到过此样式,不过经过多次问询,没得到想要的结果。只好自己动手实现。

使用BottomSheet,支持滚动布局,同时底部布局不会因BottomSheet未显示全部内容而隐藏。

目录

特性

  • 高度自定义

    支持自定义头部布局(Toolbar...)、内容布局(列表、文字)和底部布局(按钮、BottomAppBar)

  • 底部布局自适应导航栏

    只在类原生机器测试过,不保证支持国产定制系统

  • 支持Activity形式的Dialog(BottomDialogActivity)

    有Context即可显示的对话框

  • 列表可操作list进行更新View

    支持监听List

DEMO

简单标题文字

BottomDialog.builder(this) {
title("Hello")
message(
buildString {
for (i in0..30) {
for (j in0..i *5) append(j)
appendln()
}
}, true
)
oneButton("OK", autoDismiss =true) {
//长按,更新内容布局
onLongClick { dialog ->
dialog.updateContent<MessageContentBuilder> {
text =Random().nextDouble().toString()
}
}
}
}

简单列表

val list =ObservableList.build<String?> {
for (i in0..50) add("item $i")
add("到底了")
}
BottomDialog.builder(this) {
this.title("Hello")
mutableList(list) { _, position, s, l ->
toast("clicked $s at $position longClick: $l")
}
buttons {
negativeButton()
neutralButton("removeAt(0)") {
if (list.isNotEmpty()) list.removeAt(0)
}
positiveButton("add(0, '...')") {
list.add(0, "...")
}
}
}

自定义列表

加载应用列表,AppListBuilder 见下文自定义布局构造器

BottomDialog.builder(this) {
title("应用列表")
content(AppListBuilder(this@MainActivity) { _, p, i, l ->
toast("$p\n$i\n$l")
})
oneButton("取消")
}

扩展

实现的更多的ContentBuilder

详见模块Extension

  • 仿一加系统分享对话框效果 [AwesomeHeader]

引入BottomDialog

  1. 在工程build.gradle添加
allprojects {
repositories {
//...
maven { url 'https://jitpack.io' }
}
}
  1. 添加依赖

最新版本 [AndroidX]:

  • BottomDialog
dependencies {
implementation 'com.github.Vove7.BottomDialog:bottomdialog:2.2.5'
}
  • 扩展包(可选)
dependencies {
implementation 'com.github.Vove7.BottomDialog:extension:2.2.5'
}

自定义布局构造器

1. 定义三层布局构造器

三层布局均可继承ContentBuilder

ToolbarHeader,其中title属性被listenToUpdate委托,在修改时,会通知updateContent进行更新布局。

classToolbarHeader(title:CharSequence?) : ContentBuilder() {
/** * 指定更新type = 1*/var title by listenToUpdate(title, this, type =1)
/** * 导航栏图标 type = 2*/var navIconId:Int? by listenToUpdate(null, this, type =2)
/** * 导航图标点击事件 type = 3*/var onIconClick:OnClick? by listenToUpdate(null, this, type =3)
overrideval layoutRes:Int=R.layout.header_toolbar
lateinitvar toolBar:Toolbar/** * 初始化View * @param view View*/overridefuninit(view:View) {
toolBar = view.tool_bar
}
/** * 进行视图更新 * @param type Int listenToUpdate中指定的type,初始化时type值为-1 * 可根据type值来选择更新视图,而不是全部更新 * @param data Any? 传递值*/overridefunupdateContent(type:Int, data:Any?) {
//type 为1 时,属性 title 被修改if (type ==-1|| type ==1) toolBar.title = title
if (type ==-1|| type ==2)
navIconId?.also {
toolBar.setNavigationIcon(it)
} ?: toolBar.setNavigationIcon(null)
if (type ==-1|| type ==3) {
toolBar.setNavigationOnClickListener {
onIconClick?.invoke(dialog)
}
}
}
}

2. 设置扩展函数

此操作可选,目的是为了方便在builder函数中调用。

已扩展的函数有:

//设置标题fun BottomDialogBuilder.title(title:CharSequence?): BottomDialogBuilder//设置内容fun BottomDialogBuilder.message(
text:String, selectable:Boolean = false
): BottomDialogBuilder//简单列表fun BottomDialogBuilder.simpleList(
items:List<String?>, autoDismiss:Boolean = true, onItemClick:OnItemClick<String?>
): BottomDialogBuilder/** * 三个按钮布局 * buttonPositive * buttonNegative * buttonNeutral*/fun BottomDialogBuilder.buttons(block:ButtonsBuilder.() ->Unit): BottomDialogBuilder//........ 更多参考Class: [BottomDialogBuilder]

如扩展BottomDialogBuilder一个toolbar函数:

/** * 头部使用Toolbar*/fun BottomDialogBuilder.toolbar(action:ToolbarHeader.() ->Unit): BottomDialogBuilder {
headerBuilder =ToolbarHeader().apply(action)
returnthis
}

使用:

BottomDialog.builder(this, show =true) {
toolbar {
title ="Hello"
navIconId =R.mipmap.ic_launcher
onIconClick = {
dialog.dismiss()
}
}
}

除了设置扩展函数,还可直接指定header(其他两种布局亦可,content, footer):

BottomDialog.builder(this) {
header(ToolbarHeader()) {
//...
}
}

3. 自定义列表内容布局

可继承ListAdapterBuilder快速实现。

可指定layoutManager

泛型T 可实现Typeable 区分元素类别,以构建不同样式

如 应用列表内容构造器 AppListBuilder

classAppListBuilder(
context:Context,
autoDismiss:Boolean = true,
privatevalappList:ObservableList<AppInfo> = ObservableList(),
onItemClick:OnItemClick<AppInfo>
) : ListAdapterBuilder<AppInfo>(applist, autoDismiss, onItemClick) {
init {
loading =true//加载视图
thread {
sleep(1500)
loadAppList(context)
}
}
//type 为元素类型,若items 未继承 Typeable: type = 0overrideval itemView: (type:Int) ->Int= { R.layout.item_app_list }
//item 绑定到视图overrideval bindView:BindView<AppInfo> = { view, item ->
view.text_1.text = item.name
view.text_2.text = item.pkg
}
privatefunloadAppList(context:Context) {
val pm = context.packageManager
appList.addAll(ObservableList.build {
pm.getInstalledPackages(0)?.forEach {
add(AppInfo(it.packageName, it.applicationInfo.loadLabel(pm)))
}
})
//停止加载
loading =false
}
}
data classAppInfo(
valpkg:String,
valname:CharSequence
)

使用:

BottomDialog.builder(this) {
title("应用列表")
//指定内容布局Builder
content(AppListBuilder(this@MainActivity) { _, p, i, l ->
toast("$p\n$i\n$l")
})
oneButton("取消")
}

适配主题

目前有两个自定义属性: ?attr/bd_bg_color 背景色 ?android:attr/textColorPrimary 文字颜色

在使用自定义主题时,需要指定上面两个属性:

<stylename="BottomDialog.Dark"parent="BottomDialog">
<itemname="bd_bg_color">#212121</item>
<itemname="android:textColorPrimary">#fff</item>
</style>

使用主题:

BottomDialog.builder(this) {
themeId =R.style.BottomDialog_Dark//...
}

注意自定义的 ContentBuilder 也需使用动态属性:

<androidx.appcompat.widget.Toolbar
android:background="?attr/bd_bg_color"app:titleTextColor="?android:attr/textColorPrimary" />

Faqs

1. show()过后如何更新布局?

此时比如更新message内容(内容布局类型为MessageContentBuilder)

dialog.updateContent<MessageContentBuilder> {
//text 属性被委托,才可通知布局刷新,见[MessageContentBuilder]
text ="new message"
}

2. 属性委托相关

当属性被委托后,改变值即可通知ContentBuilderupdateContent(type: Int, data: Any?)

如:

var title by listenToUpdate(title, this, 2)

当title值被修改后,会执行updateContent(2)

实现原理

  1. 列表类型为ObservableList,可监听内容变化,来通知Adapter更新布局

  2. 对话框底部布局,能够悬浮,由于:其中BottomSheet布局bs_rootfooter_lay同级。

布局文件dialog_content.xml。(已去除不重要属性)

<CoordinatorLayout>
<!--BottomSheet-->
<LinearLayoutandroid:id="@+id/bs_root"app:layout_behavior="android.support.design.widget.BottomSheetBehavior">
<!--头部布局-->
<FrameLayoutandroid:id="@+id/header_container" />
<NestedScrollViewandroid:id="@+id/container">
<!--内容布局-->
<FrameLayoutandroid:id="@+id/content"/>
</NestedScrollView>
</LinearLayout>
<LinearLayoutandroid:id="@+id/footer_lay"android:layout_alignParentBottom="true"android:layout_gravity="bottom" >
<!--底部布局-->
<FrameLayoutandroid:id="@+id/footer_contains" />
<!--用于撑起底部布局于导航栏之上-->
<Viewandroid:id="@+id/fill_nav" />
</LinearLayout>
</CoordinatorLayout>

详细内容请参考源码

About

可高度自定义的底部对话框,使用BottomSheet,支持滚动布局,同时底部布局不会因BottomSheet未显示全部内容而隐藏。

Topics

Resources

Stars

87 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages

, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Auto-enable theater mode on YouTube\n(function() {\n function tryTheater() {\n var btn = document.querySelector('button[aria-label=\"Theater mode\"], ytd-player #player button[title=\"Theater mode\"]');\n if (btn && !btn.classList.contains('activated')) {\n btn.click();\n }\n }\n \n // Try immediately\n tryTheater();\n \n // Try after navigation (SPA)\n var lastUrl = location.href;\n setInterval(function() {\n if (location.href !== lastUrl) {\n lastUrl = location.href;\n setTimeout(tryTheater, 500);\n }\n }, 1000);\n \n // Also try on player load\n var observer = new MutationObserver(tryTheater);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "YouTube Theater Mode Default"); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Repository files navigation

BottomDialog

心水纯纯写作很久的底部对话框样式,同时在Google Play Store 也见到过此样式,不过经过多次问询,没得到想要的结果。只好自己动手实现。

使用BottomSheet,支持滚动布局,同时底部布局不会因BottomSheet未显示全部内容而隐藏。

目录

特性

  • 高度自定义

    支持自定义头部布局(Toolbar...)、内容布局(列表、文字)和底部布局(按钮、BottomAppBar)

  • 底部布局自适应导航栏

    只在类原生机器测试过,不保证支持国产定制系统

  • 支持Activity形式的Dialog(BottomDialogActivity)

    有Context即可显示的对话框

  • 列表可操作list进行更新View

    支持监听List

DEMO

简单标题文字

BottomDialog.builder(this) {
title("Hello")
message(
buildString {
for (i in0..30) {
for (j in0..i *5) append(j)
appendln()
}
}, true
)
oneButton("OK", autoDismiss =true) {
//长按,更新内容布局
onLongClick { dialog ->
dialog.updateContent<MessageContentBuilder> {
text =Random().nextDouble().toString()
}
}
}
}

简单列表

val list =ObservableList.build<String?> {
for (i in0..50) add("item $i")
add("到底了")
}
BottomDialog.builder(this) {
this.title("Hello")
mutableList(list) { _, position, s, l ->
toast("clicked $s at $position longClick: $l")
}
buttons {
negativeButton()
neutralButton("removeAt(0)") {
if (list.isNotEmpty()) list.removeAt(0)
}
positiveButton("add(0, '...')") {
list.add(0, "...")
}
}
}

自定义列表

加载应用列表,AppListBuilder 见下文自定义布局构造器

BottomDialog.builder(this) {
title("应用列表")
content(AppListBuilder(this@MainActivity) { _, p, i, l ->
toast("$p\n$i\n$l")
})
oneButton("取消")
}

扩展

实现的更多的ContentBuilder

详见模块Extension

  • 仿一加系统分享对话框效果 [AwesomeHeader]

引入BottomDialog

  1. 在工程build.gradle添加
allprojects {
repositories {
//...
maven { url 'https://jitpack.io' }
}
}
  1. 添加依赖

最新版本 [AndroidX]:

  • BottomDialog
dependencies {
implementation 'com.github.Vove7.BottomDialog:bottomdialog:2.2.5'
}
  • 扩展包(可选)
dependencies {
implementation 'com.github.Vove7.BottomDialog:extension:2.2.5'
}

自定义布局构造器

1. 定义三层布局构造器

三层布局均可继承ContentBuilder

ToolbarHeader,其中title属性被listenToUpdate委托,在修改时,会通知updateContent进行更新布局。

classToolbarHeader(title:CharSequence?) : ContentBuilder() {
/** * 指定更新type = 1*/var title by listenToUpdate(title, this, type =1)
/** * 导航栏图标 type = 2*/var navIconId:Int? by listenToUpdate(null, this, type =2)
/** * 导航图标点击事件 type = 3*/var onIconClick:OnClick? by listenToUpdate(null, this, type =3)
overrideval layoutRes:Int=R.layout.header_toolbar
lateinitvar toolBar:Toolbar/** * 初始化View * @param view View*/overridefuninit(view:View) {
toolBar = view.tool_bar
}
/** * 进行视图更新 * @param type Int listenToUpdate中指定的type,初始化时type值为-1 * 可根据type值来选择更新视图,而不是全部更新 * @param data Any? 传递值*/overridefunupdateContent(type:Int, data:Any?) {
//type 为1 时,属性 title 被修改if (type ==-1|| type ==1) toolBar.title = title
if (type ==-1|| type ==2)
navIconId?.also {
toolBar.setNavigationIcon(it)
} ?: toolBar.setNavigationIcon(null)
if (type ==-1|| type ==3) {
toolBar.setNavigationOnClickListener {
onIconClick?.invoke(dialog)
}
}
}
}

2. 设置扩展函数

此操作可选,目的是为了方便在builder函数中调用。

已扩展的函数有:

//设置标题fun BottomDialogBuilder.title(title:CharSequence?): BottomDialogBuilder//设置内容fun BottomDialogBuilder.message(
text:String, selectable:Boolean = false
): BottomDialogBuilder//简单列表fun BottomDialogBuilder.simpleList(
items:List<String?>, autoDismiss:Boolean = true, onItemClick:OnItemClick<String?>
): BottomDialogBuilder/** * 三个按钮布局 * buttonPositive * buttonNegative * buttonNeutral*/fun BottomDialogBuilder.buttons(block:ButtonsBuilder.() ->Unit): BottomDialogBuilder//........ 更多参考Class: [BottomDialogBuilder]

如扩展BottomDialogBuilder一个toolbar函数:

/** * 头部使用Toolbar*/fun BottomDialogBuilder.toolbar(action:ToolbarHeader.() ->Unit): BottomDialogBuilder {
headerBuilder =ToolbarHeader().apply(action)
returnthis
}

使用:

BottomDialog.builder(this, show =true) {
toolbar {
title ="Hello"
navIconId =R.mipmap.ic_launcher
onIconClick = {
dialog.dismiss()
}
}
}

除了设置扩展函数,还可直接指定header(其他两种布局亦可,content, footer):

BottomDialog.builder(this) {
header(ToolbarHeader()) {
//...
}
}

3. 自定义列表内容布局

可继承ListAdapterBuilder快速实现。

可指定layoutManager

泛型T 可实现Typeable 区分元素类别,以构建不同样式

如 应用列表内容构造器 AppListBuilder

classAppListBuilder(
context:Context,
autoDismiss:Boolean = true,
privatevalappList:ObservableList<AppInfo> = ObservableList(),
onItemClick:OnItemClick<AppInfo>
) : ListAdapterBuilder<AppInfo>(applist, autoDismiss, onItemClick) {
init {
loading =true//加载视图
thread {
sleep(1500)
loadAppList(context)
}
}
//type 为元素类型,若items 未继承 Typeable: type = 0overrideval itemView: (type:Int) ->Int= { R.layout.item_app_list }
//item 绑定到视图overrideval bindView:BindView<AppInfo> = { view, item ->
view.text_1.text = item.name
view.text_2.text = item.pkg
}
privatefunloadAppList(context:Context) {
val pm = context.packageManager
appList.addAll(ObservableList.build {
pm.getInstalledPackages(0)?.forEach {
add(AppInfo(it.packageName, it.applicationInfo.loadLabel(pm)))
}
})
//停止加载
loading =false
}
}
data classAppInfo(
valpkg:String,
valname:CharSequence
)

使用:

BottomDialog.builder(this) {
title("应用列表")
//指定内容布局Builder
content(AppListBuilder(this@MainActivity) { _, p, i, l ->
toast("$p\n$i\n$l")
})
oneButton("取消")
}

适配主题

目前有两个自定义属性: ?attr/bd_bg_color 背景色 ?android:attr/textColorPrimary 文字颜色

在使用自定义主题时,需要指定上面两个属性:

<stylename="BottomDialog.Dark"parent="BottomDialog">
<itemname="bd_bg_color">#212121</item>
<itemname="android:textColorPrimary">#fff</item>
</style>

使用主题:

BottomDialog.builder(this) {
themeId =R.style.BottomDialog_Dark//...
}

注意自定义的 ContentBuilder 也需使用动态属性:

<androidx.appcompat.widget.Toolbar
android:background="?attr/bd_bg_color"app:titleTextColor="?android:attr/textColorPrimary" />

Faqs

1. show()过后如何更新布局?

此时比如更新message内容(内容布局类型为MessageContentBuilder)

dialog.updateContent<MessageContentBuilder> {
//text 属性被委托,才可通知布局刷新,见[MessageContentBuilder]
text ="new message"
}

2. 属性委托相关

当属性被委托后,改变值即可通知ContentBuilderupdateContent(type: Int, data: Any?)

如:

var title by listenToUpdate(title, this, 2)

当title值被修改后,会执行updateContent(2)

实现原理

  1. 列表类型为ObservableList,可监听内容变化,来通知Adapter更新布局

  2. 对话框底部布局,能够悬浮,由于:其中BottomSheet布局bs_rootfooter_lay同级。

布局文件dialog_content.xml。(已去除不重要属性)

<CoordinatorLayout>
<!--BottomSheet-->
<LinearLayoutandroid:id="@+id/bs_root"app:layout_behavior="android.support.design.widget.BottomSheetBehavior">
<!--头部布局-->
<FrameLayoutandroid:id="@+id/header_container" />
<NestedScrollViewandroid:id="@+id/container">
<!--内容布局-->
<FrameLayoutandroid:id="@+id/content"/>
</NestedScrollView>
</LinearLayout>
<LinearLayoutandroid:id="@+id/footer_lay"android:layout_alignParentBottom="true"android:layout_gravity="bottom" >
<!--底部布局-->
<FrameLayoutandroid:id="@+id/footer_contains" />
<!--用于撑起底部布局于导航栏之上-->
<Viewandroid:id="@+id/fill_nav" />
</LinearLayout>
</CoordinatorLayout>

详细内容请参考源码

About

可高度自定义的底部对话框,使用BottomSheet,支持滚动布局,同时底部布局不会因BottomSheet未显示全部内容而隐藏。

Topics

Resources

Stars

87 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages

, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Remove or un-stick sticky/fixed headers that block content\n(function() {\n function unstick() {\n document.querySelectorAll('header, nav, [role=\"banner\"], .header, .navbar, .sticky, .fixed-top, [style*=\"position: fixed\"], [style*=\"position:sticky\"]').forEach(function(el) {\n if (el.style.position === 'fixed' || el.style.position === 'sticky' || \n getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') {\n el.style.position = 'static';\n el.style.top = 'auto';\n el.style.zIndex = 'auto';\n }\n });\n }\n \n unstick();\n \n var observer = new MutationObserver(unstick);\n observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] });\n})();", "Kill Sticky Headers"); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Repository files navigation

BottomDialog

心水纯纯写作很久的底部对话框样式,同时在Google Play Store 也见到过此样式,不过经过多次问询,没得到想要的结果。只好自己动手实现。

使用BottomSheet,支持滚动布局,同时底部布局不会因BottomSheet未显示全部内容而隐藏。

目录

特性

  • 高度自定义

    支持自定义头部布局(Toolbar...)、内容布局(列表、文字)和底部布局(按钮、BottomAppBar)

  • 底部布局自适应导航栏

    只在类原生机器测试过,不保证支持国产定制系统

  • 支持Activity形式的Dialog(BottomDialogActivity)

    有Context即可显示的对话框

  • 列表可操作list进行更新View

    支持监听List

DEMO

简单标题文字

BottomDialog.builder(this) {
title("Hello")
message(
buildString {
for (i in0..30) {
for (j in0..i *5) append(j)
appendln()
}
}, true
)
oneButton("OK", autoDismiss =true) {
//长按,更新内容布局
onLongClick { dialog ->
dialog.updateContent<MessageContentBuilder> {
text =Random().nextDouble().toString()
}
}
}
}

简单列表

val list =ObservableList.build<String?> {
for (i in0..50) add("item $i")
add("到底了")
}
BottomDialog.builder(this) {
this.title("Hello")
mutableList(list) { _, position, s, l ->
toast("clicked $s at $position longClick: $l")
}
buttons {
negativeButton()
neutralButton("removeAt(0)") {
if (list.isNotEmpty()) list.removeAt(0)
}
positiveButton("add(0, '...')") {
list.add(0, "...")
}
}
}

自定义列表

加载应用列表,AppListBuilder 见下文自定义布局构造器

BottomDialog.builder(this) {
title("应用列表")
content(AppListBuilder(this@MainActivity) { _, p, i, l ->
toast("$p\n$i\n$l")
})
oneButton("取消")
}

扩展

实现的更多的ContentBuilder

详见模块Extension

  • 仿一加系统分享对话框效果 [AwesomeHeader]

引入BottomDialog

  1. 在工程build.gradle添加
allprojects {
repositories {
//...
maven { url 'https://jitpack.io' }
}
}
  1. 添加依赖

最新版本 [AndroidX]:

  • BottomDialog
dependencies {
implementation 'com.github.Vove7.BottomDialog:bottomdialog:2.2.5'
}
  • 扩展包(可选)
dependencies {
implementation 'com.github.Vove7.BottomDialog:extension:2.2.5'
}

自定义布局构造器

1. 定义三层布局构造器

三层布局均可继承ContentBuilder

ToolbarHeader,其中title属性被listenToUpdate委托,在修改时,会通知updateContent进行更新布局。

classToolbarHeader(title:CharSequence?) : ContentBuilder() {
/** * 指定更新type = 1*/var title by listenToUpdate(title, this, type =1)
/** * 导航栏图标 type = 2*/var navIconId:Int? by listenToUpdate(null, this, type =2)
/** * 导航图标点击事件 type = 3*/var onIconClick:OnClick? by listenToUpdate(null, this, type =3)
overrideval layoutRes:Int=R.layout.header_toolbar
lateinitvar toolBar:Toolbar/** * 初始化View * @param view View*/overridefuninit(view:View) {
toolBar = view.tool_bar
}
/** * 进行视图更新 * @param type Int listenToUpdate中指定的type,初始化时type值为-1 * 可根据type值来选择更新视图,而不是全部更新 * @param data Any? 传递值*/overridefunupdateContent(type:Int, data:Any?) {
//type 为1 时,属性 title 被修改if (type ==-1|| type ==1) toolBar.title = title
if (type ==-1|| type ==2)
navIconId?.also {
toolBar.setNavigationIcon(it)
} ?: toolBar.setNavigationIcon(null)
if (type ==-1|| type ==3) {
toolBar.setNavigationOnClickListener {
onIconClick?.invoke(dialog)
}
}
}
}

2. 设置扩展函数

此操作可选,目的是为了方便在builder函数中调用。

已扩展的函数有:

//设置标题fun BottomDialogBuilder.title(title:CharSequence?): BottomDialogBuilder//设置内容fun BottomDialogBuilder.message(
text:String, selectable:Boolean = false
): BottomDialogBuilder//简单列表fun BottomDialogBuilder.simpleList(
items:List<String?>, autoDismiss:Boolean = true, onItemClick:OnItemClick<String?>
): BottomDialogBuilder/** * 三个按钮布局 * buttonPositive * buttonNegative * buttonNeutral*/fun BottomDialogBuilder.buttons(block:ButtonsBuilder.() ->Unit): BottomDialogBuilder//........ 更多参考Class: [BottomDialogBuilder]

如扩展BottomDialogBuilder一个toolbar函数:

/** * 头部使用Toolbar*/fun BottomDialogBuilder.toolbar(action:ToolbarHeader.() ->Unit): BottomDialogBuilder {
headerBuilder =ToolbarHeader().apply(action)
returnthis
}

使用:

BottomDialog.builder(this, show =true) {
toolbar {
title ="Hello"
navIconId =R.mipmap.ic_launcher
onIconClick = {
dialog.dismiss()
}
}
}

除了设置扩展函数,还可直接指定header(其他两种布局亦可,content, footer):

BottomDialog.builder(this) {
header(ToolbarHeader()) {
//...
}
}

3. 自定义列表内容布局

可继承ListAdapterBuilder快速实现。

可指定layoutManager

泛型T 可实现Typeable 区分元素类别,以构建不同样式

如 应用列表内容构造器 AppListBuilder

classAppListBuilder(
context:Context,
autoDismiss:Boolean = true,
privatevalappList:ObservableList<AppInfo> = ObservableList(),
onItemClick:OnItemClick<AppInfo>
) : ListAdapterBuilder<AppInfo>(applist, autoDismiss, onItemClick) {
init {
loading =true//加载视图
thread {
sleep(1500)
loadAppList(context)
}
}
//type 为元素类型,若items 未继承 Typeable: type = 0overrideval itemView: (type:Int) ->Int= { R.layout.item_app_list }
//item 绑定到视图overrideval bindView:BindView<AppInfo> = { view, item ->
view.text_1.text = item.name
view.text_2.text = item.pkg
}
privatefunloadAppList(context:Context) {
val pm = context.packageManager
appList.addAll(ObservableList.build {
pm.getInstalledPackages(0)?.forEach {
add(AppInfo(it.packageName, it.applicationInfo.loadLabel(pm)))
}
})
//停止加载
loading =false
}
}
data classAppInfo(
valpkg:String,
valname:CharSequence
)

使用:

BottomDialog.builder(this) {
title("应用列表")
//指定内容布局Builder
content(AppListBuilder(this@MainActivity) { _, p, i, l ->
toast("$p\n$i\n$l")
})
oneButton("取消")
}

适配主题

目前有两个自定义属性: ?attr/bd_bg_color 背景色 ?android:attr/textColorPrimary 文字颜色

在使用自定义主题时,需要指定上面两个属性:

<stylename="BottomDialog.Dark"parent="BottomDialog">
<itemname="bd_bg_color">#212121</item>
<itemname="android:textColorPrimary">#fff</item>
</style>

使用主题:

BottomDialog.builder(this) {
themeId =R.style.BottomDialog_Dark//...
}

注意自定义的 ContentBuilder 也需使用动态属性:

<androidx.appcompat.widget.Toolbar
android:background="?attr/bd_bg_color"app:titleTextColor="?android:attr/textColorPrimary" />

Faqs

1. show()过后如何更新布局?

此时比如更新message内容(内容布局类型为MessageContentBuilder)

dialog.updateContent<MessageContentBuilder> {
//text 属性被委托,才可通知布局刷新,见[MessageContentBuilder]
text ="new message"
}

2. 属性委托相关

当属性被委托后,改变值即可通知ContentBuilderupdateContent(type: Int, data: Any?)

如:

var title by listenToUpdate(title, this, 2)

当title值被修改后,会执行updateContent(2)

实现原理

  1. 列表类型为ObservableList,可监听内容变化,来通知Adapter更新布局

  2. 对话框底部布局,能够悬浮,由于:其中BottomSheet布局bs_rootfooter_lay同级。

布局文件dialog_content.xml。(已去除不重要属性)

<CoordinatorLayout>
<!--BottomSheet-->
<LinearLayoutandroid:id="@+id/bs_root"app:layout_behavior="android.support.design.widget.BottomSheetBehavior">
<!--头部布局-->
<FrameLayoutandroid:id="@+id/header_container" />
<NestedScrollViewandroid:id="@+id/container">
<!--内容布局-->
<FrameLayoutandroid:id="@+id/content"/>
</NestedScrollView>
</LinearLayout>
<LinearLayoutandroid:id="@+id/footer_lay"android:layout_alignParentBottom="true"android:layout_gravity="bottom" >
<!--底部布局-->
<FrameLayoutandroid:id="@+id/footer_contains" />
<!--用于撑起底部布局于导航栏之上-->
<Viewandroid:id="@+id/fill_nav" />
</LinearLayout>
</CoordinatorLayout>

详细内容请参考源码

About

可高度自定义的底部对话框,使用BottomSheet,支持滚动布局,同时底部布局不会因BottomSheet未显示全部内容而隐藏。

Topics

Resources

Stars

87 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages

, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Universal Dark Mode - works on any site\n(function() {\n var enabled = true;\n \n function applyDarkMode() {\n if (!enabled) return;\n \n // Create style element if it doesn't exist\n var style = document.getElementById('universal-dark-mode-style');\n if (!style) {\n style = document.createElement('style');\n style.id = 'universal-dark-mode-style';\n document.head.appendChild(style);\n }\n \n // Dark mode CSS - inverts colors but preserves images/video\n style.textContent = '\n /* Invert everything except media */\n html {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #1a1a2e !important;\n }\n \n /* Restore images, videos, iframes, canvas */\n img, video, iframe, canvas, svg, picture, [style*=\"background-image\"] {\n filter: invert(1) hue-rotate(180deg) !important;\n }\n \n /* Preserve specific elements that should not be inverted */\n .no-dark-mode, .no-dark-mode *,\n [data-theme=\"light\"], [data-theme=\"light\"],\n .ace_editor, .ace_editor *,\n .CodeMirror, .CodeMirror *,\n .monaco-editor, .monaco-editor *,\n .markdown-body pre, .markdown-body pre *,\n .highlight, .highlight *,\n pre code, pre code * {\n filter: none !important;\n }\n \n /* Fix common UI elements */\n .modal, .popup, .dropdown-menu, .tooltip, .popover {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #2d2d44 !important;\n border-color: #444 !important;\n }\n \n /* Scrollbars */\n ::-webkit-scrollbar { background: #1a1a2e !important; }\n ::-webkit-scrollbar-thumb { background: #444 !important; }\n ::-webkit-scrollbar-thumb:hover { background: #555 !important; }\n \n /* Selection */\n ::selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ::-moz-selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ';\n }\n \n function removeDarkMode() {\n var style = document.getElementById('universal-dark-mode-style');\n if (style) style.remove();\n }\n \n // Toggle with Alt+Shift+D\n document.addEventListener('keydown', function(e) {\n if (e.altKey && e.shiftKey && e.key === 'D') {\n e.preventDefault();\n enabled = !enabled;\n if (enabled) {\n applyDarkMode();\n console.log('[Universal Dark Mode] Enabled');\n } else {\n removeDarkMode();\n console.log('[Universal Dark Mode] Disabled');\n }\n }\n });\n \n // Apply on load\n applyDarkMode();\n \n // Re-apply on dynamic content\n var observer = new MutationObserver(function(mutations) {\n if (enabled && !document.getElementById('universal-dark-mode-style')) {\n applyDarkMode();\n }\n });\n observer.observe(document.head, { childList: true });\n \n console.log('[Universal Dark Mode] Loaded - Press Alt+Shift+D to toggle');\n})();", "Universal Dark Mode"); } } catch(__e) { console.warn('[Userscript:Universal Dark Mode]', __e); } })(); })();
Skip to content

Repository files navigation

BottomDialog

心水纯纯写作很久的底部对话框样式,同时在Google Play Store 也见到过此样式,不过经过多次问询,没得到想要的结果。只好自己动手实现。

使用BottomSheet,支持滚动布局,同时底部布局不会因BottomSheet未显示全部内容而隐藏。

目录

特性

  • 高度自定义

    支持自定义头部布局(Toolbar...)、内容布局(列表、文字)和底部布局(按钮、BottomAppBar)

  • 底部布局自适应导航栏

    只在类原生机器测试过,不保证支持国产定制系统

  • 支持Activity形式的Dialog(BottomDialogActivity)

    有Context即可显示的对话框

  • 列表可操作list进行更新View

    支持监听List

DEMO

简单标题文字

BottomDialog.builder(this) {
title("Hello")
message(
buildString {
for (i in0..30) {
for (j in0..i *5) append(j)
appendln()
}
}, true
)
oneButton("OK", autoDismiss =true) {
//长按,更新内容布局
onLongClick { dialog ->
dialog.updateContent<MessageContentBuilder> {
text =Random().nextDouble().toString()
}
}
}
}

简单列表

val list =ObservableList.build<String?> {
for (i in0..50) add("item $i")
add("到底了")
}
BottomDialog.builder(this) {
this.title("Hello")
mutableList(list) { _, position, s, l ->
toast("clicked $s at $position longClick: $l")
}
buttons {
negativeButton()
neutralButton("removeAt(0)") {
if (list.isNotEmpty()) list.removeAt(0)
}
positiveButton("add(0, '...')") {
list.add(0, "...")
}
}
}

自定义列表

加载应用列表,AppListBuilder 见下文自定义布局构造器

BottomDialog.builder(this) {
title("应用列表")
content(AppListBuilder(this@MainActivity) { _, p, i, l ->
toast("$p\n$i\n$l")
})
oneButton("取消")
}

扩展

实现的更多的ContentBuilder

详见模块Extension

  • 仿一加系统分享对话框效果 [AwesomeHeader]

引入BottomDialog

  1. 在工程build.gradle添加
allprojects {
repositories {
//...
maven { url 'https://jitpack.io' }
}
}
  1. 添加依赖

最新版本 [AndroidX]:

  • BottomDialog
dependencies {
implementation 'com.github.Vove7.BottomDialog:bottomdialog:2.2.5'
}
  • 扩展包(可选)
dependencies {
implementation 'com.github.Vove7.BottomDialog:extension:2.2.5'
}

自定义布局构造器

1. 定义三层布局构造器

三层布局均可继承ContentBuilder

ToolbarHeader,其中title属性被listenToUpdate委托,在修改时,会通知updateContent进行更新布局。

classToolbarHeader(title:CharSequence?) : ContentBuilder() {
/** * 指定更新type = 1*/var title by listenToUpdate(title, this, type =1)
/** * 导航栏图标 type = 2*/var navIconId:Int? by listenToUpdate(null, this, type =2)
/** * 导航图标点击事件 type = 3*/var onIconClick:OnClick? by listenToUpdate(null, this, type =3)
overrideval layoutRes:Int=R.layout.header_toolbar
lateinitvar toolBar:Toolbar/** * 初始化View * @param view View*/overridefuninit(view:View) {
toolBar = view.tool_bar
}
/** * 进行视图更新 * @param type Int listenToUpdate中指定的type,初始化时type值为-1 * 可根据type值来选择更新视图,而不是全部更新 * @param data Any? 传递值*/overridefunupdateContent(type:Int, data:Any?) {
//type 为1 时,属性 title 被修改if (type ==-1|| type ==1) toolBar.title = title
if (type ==-1|| type ==2)
navIconId?.also {
toolBar.setNavigationIcon(it)
} ?: toolBar.setNavigationIcon(null)
if (type ==-1|| type ==3) {
toolBar.setNavigationOnClickListener {
onIconClick?.invoke(dialog)
}
}
}
}

2. 设置扩展函数

此操作可选,目的是为了方便在builder函数中调用。

已扩展的函数有:

//设置标题fun BottomDialogBuilder.title(title:CharSequence?): BottomDialogBuilder//设置内容fun BottomDialogBuilder.message(
text:String, selectable:Boolean = false
): BottomDialogBuilder//简单列表fun BottomDialogBuilder.simpleList(
items:List<String?>, autoDismiss:Boolean = true, onItemClick:OnItemClick<String?>
): BottomDialogBuilder/** * 三个按钮布局 * buttonPositive * buttonNegative * buttonNeutral*/fun BottomDialogBuilder.buttons(block:ButtonsBuilder.() ->Unit): BottomDialogBuilder//........ 更多参考Class: [BottomDialogBuilder]

如扩展BottomDialogBuilder一个toolbar函数:

/** * 头部使用Toolbar*/fun BottomDialogBuilder.toolbar(action:ToolbarHeader.() ->Unit): BottomDialogBuilder {
headerBuilder =ToolbarHeader().apply(action)
returnthis
}

使用:

BottomDialog.builder(this, show =true) {
toolbar {
title ="Hello"
navIconId =R.mipmap.ic_launcher
onIconClick = {
dialog.dismiss()
}
}
}

除了设置扩展函数,还可直接指定header(其他两种布局亦可,content, footer):

BottomDialog.builder(this) {
header(ToolbarHeader()) {
//...
}
}

3. 自定义列表内容布局

可继承ListAdapterBuilder快速实现。

可指定layoutManager

泛型T 可实现Typeable 区分元素类别,以构建不同样式

如 应用列表内容构造器 AppListBuilder

classAppListBuilder(
context:Context,
autoDismiss:Boolean = true,
privatevalappList:ObservableList<AppInfo> = ObservableList(),
onItemClick:OnItemClick<AppInfo>
) : ListAdapterBuilder<AppInfo>(applist, autoDismiss, onItemClick) {
init {
loading =true//加载视图
thread {
sleep(1500)
loadAppList(context)
}
}
//type 为元素类型,若items 未继承 Typeable: type = 0overrideval itemView: (type:Int) ->Int= { R.layout.item_app_list }
//item 绑定到视图overrideval bindView:BindView<AppInfo> = { view, item ->
view.text_1.text = item.name
view.text_2.text = item.pkg
}
privatefunloadAppList(context:Context) {
val pm = context.packageManager
appList.addAll(ObservableList.build {
pm.getInstalledPackages(0)?.forEach {
add(AppInfo(it.packageName, it.applicationInfo.loadLabel(pm)))
}
})
//停止加载
loading =false
}
}
data classAppInfo(
valpkg:String,
valname:CharSequence
)

使用:

BottomDialog.builder(this) {
title("应用列表")
//指定内容布局Builder
content(AppListBuilder(this@MainActivity) { _, p, i, l ->
toast("$p\n$i\n$l")
})
oneButton("取消")
}

适配主题

目前有两个自定义属性: ?attr/bd_bg_color 背景色 ?android:attr/textColorPrimary 文字颜色

在使用自定义主题时,需要指定上面两个属性:

<stylename="BottomDialog.Dark"parent="BottomDialog">
<itemname="bd_bg_color">#212121</item>
<itemname="android:textColorPrimary">#fff</item>
</style>

使用主题:

BottomDialog.builder(this) {
themeId =R.style.BottomDialog_Dark//...
}

注意自定义的 ContentBuilder 也需使用动态属性:

<androidx.appcompat.widget.Toolbar
android:background="?attr/bd_bg_color"app:titleTextColor="?android:attr/textColorPrimary" />

Faqs

1. show()过后如何更新布局?

此时比如更新message内容(内容布局类型为MessageContentBuilder)

dialog.updateContent<MessageContentBuilder> {
//text 属性被委托,才可通知布局刷新,见[MessageContentBuilder]
text ="new message"
}

2. 属性委托相关

当属性被委托后,改变值即可通知ContentBuilderupdateContent(type: Int, data: Any?)

如:

var title by listenToUpdate(title, this, 2)

当title值被修改后,会执行updateContent(2)

实现原理

  1. 列表类型为ObservableList,可监听内容变化,来通知Adapter更新布局

  2. 对话框底部布局,能够悬浮,由于:其中BottomSheet布局bs_rootfooter_lay同级。

布局文件dialog_content.xml。(已去除不重要属性)

<CoordinatorLayout>
<!--BottomSheet-->
<LinearLayoutandroid:id="@+id/bs_root"app:layout_behavior="android.support.design.widget.BottomSheetBehavior">
<!--头部布局-->
<FrameLayoutandroid:id="@+id/header_container" />
<NestedScrollViewandroid:id="@+id/container">
<!--内容布局-->
<FrameLayoutandroid:id="@+id/content"/>
</NestedScrollView>
</LinearLayout>
<LinearLayoutandroid:id="@+id/footer_lay"android:layout_alignParentBottom="true"android:layout_gravity="bottom" >
<!--底部布局-->
<FrameLayoutandroid:id="@+id/footer_contains" />
<!--用于撑起底部布局于导航栏之上-->
<Viewandroid:id="@+id/fill_nav" />
</LinearLayout>
</CoordinatorLayout>

详细内容请参考源码

About

可高度自定义的底部对话框,使用BottomSheet,支持滚动布局,同时底部布局不会因BottomSheet未显示全部内容而隐藏。

Topics

Resources

Stars

87 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages