当前位置:网站首页>Material design component - use bottomsheet to show extended content (II)
Material design component - use bottomsheet to show extended content (II)
2022-07-05 20:48:00 【Kakar】
Material Design Components - Use BottomSheet Show extended content ( Two )
In the last article We use XML Middle configuration bottom_sheet_behavior The way to achieve a simple BottomSheet, And explains that in XML Some parameters that can be configured in are described in detail , In this article, we will explain how to dynamically set in the code BottomSheet Some behaviors of
Dynamic settings in code BottomSheet
Let's think about it first , If we want to achieve the beginning BottomSheet This is the state of expansion , Instead of being folded , So what are we going to do ? We can implement this requirement in code , The code is simple , as follows
class OcrResultActivity : AppCompatActivity(R.layout.ocr_result_layout) {
val mBottomSheetBehavior: BottomSheetBehavior<View> by lazy {
BottomSheetBehavior.from(bottom_sheet)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
mBottomSheetBehavior.state = BottomSheetBehavior.STATE_COLLAPSED
mBottomSheetBehavior.peekHeight = 200
}
}
We use BottomSheetBehavior Class from Methods get our BottomSheetBehavior Instance object , Then we can make various settings , The settings here include our previous xml All parameters that can be configured in the way , And you can also set BottomSheet Of state, So this state And what is it ? Simply speaking state It means that at present BottomSheet A state of , Whether it is expanded or folded belongs to its state , There are several States
- STATE_COLLAPSED: Folded state , In this state BottomSheet Will always show , If it is high, it will be peek height Height
- STATE_EXPANDED: Unfold state , In this state ,BottomSheet Will show its maximum height
- STATE_HALF_EXPANDED: Semi expanded state , The official website indicates that this status is only set fitToContents by false When does it apply , But in fact, even if it is set to true, Use code to modify BottomSheet The status of is STATE_HALF_EXPANDED When , It will also trigger this state
- STATE_HIDDEN: Hidden state , In this state BottomSheet invisible , And it can only be visible again in the way of code settings , After testing , The following points need to be noted :
- Set up hidden by true, take BottomSheet Slide down and become hidden , Set it dynamically again hidden by false Will immediately BottomSheet Show again , There is no need to reset its state
- Set up hidden by true, take BottomSheet Slide down and become hidden , Its status can be set to STATE_COLLAPSED or STATE_HALF_EXPANDED or STATE_HALF_EXPANDED Show it again
- You can directly set its status to STATE_HIDDEN To hide directly BottomSheet
- STATE_DRAGGING: Users actively drag up or down BottomSheet When
- STATE_SETTLING: This state is after the user drags , Fingers left the screen , However BottomSheet It is in this state for a short period of time when it continues to move to the specified height , More like STATE_COLLAPSED and STATE_HALF_EXPANDED An intermediate transition state of
This way, pay attention to STATE_DRAGGING and STATE_SETTLING Cannot be set in code ,BottomSheet Handled internally , Other states can be actively set
We can give BottomSheetBehavior Set a callback method to receive the above status
mBottomSheetBehavior.addBottomSheetCallback(object :
BottomSheetBehavior.BottomSheetCallback() {
override fun onStateChanged(bottomSheet: View, newState: Int) {
Log.d("mBottomSheetBehavior", "newState = $newState")
}
override fun onSlide(bottomSheet: View, slideOffset: Float) {
}
})
onStateChanged Method will call back a new state when the state changes , We can do all kinds of processing we want here , For example, we can expand each time , Change our BottomSheet The background color , You can write like this
mBottomSheetBehavior.addBottomSheetCallback(object : BottomSheetBehavior.BottomSheetCallback() { override fun onStateChanged(bottomSheet: View, newState: Int) { when (newState) { BottomSheetBehavior.STATE_DRAGGING -> { val colorR = (0..255).random() val colorG = (0..255).random() val colorB = (0..255).random() bottomSheet.setBackgroundColor( Color.argb( 255, colorR, colorG, colorB ) ) } else -> { } } } override fun onSlide(bottomSheet: View, slideOffset: Float) { } })
The effect is as follows :
- onSlide Methods are more useful , Its slideOffset It's a ratio , Its value is from -1 To 0 To 1,0 To 1 The explanation is BottomSheet During sliding , A proportion of the intermediate process from the folded state to the fully expanded state , The folding status is 0, Sliding upward will continue from 0 Bigger , Until the fully expanded state changes to 1 until ,0 To -1 Illustrates the BottomSheet In a proportion of sliding from the folded state to the hidden state ,-1 That is to say BottomSheet It's hidden , This callback is set directly BottomSheet State enable BottomSheet In the process of unfolding or folding , There will be multiple callbacks , What is returned is a procedural proportional relationship , You can try it yourself , There is no more display here , Then there will be a real battle Demo This will be used to do some effects
Modality BottomSheet
Since then ,BottomSheet We all know the state of , Then maybe someone will have another need , That is, I want the kind of and Dialog Same , There is a mask on the back , The professional point is called modal window , The official is also very considerate , It directly provides us BottomSheetDialog and BottomSheetDialogFragment, In fact, these two will BottomSheetBehavior And Dialog A combination of products , The usage is also quite simple , We have the foundation ahead , It's much easier to look at this again
BottomSheetDialog
Let's see one first BottomSheetDialog Simple usage
class ModalBottomActivity : AppCompatActivity(R.layout.ocr_result_layout) {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val bsd = BottomSheetDialog(this)
bsd.setContentView(R.layout.modal_buttom_sheet_content)
bsd.behavior.peekHeight = 200
// bsd.behavior.isHideable = false // Be careful , here BottomSheetDialog default hideable Behavior is true, And BottomSheetBehavior Different , After all Dialog The default is that it should be able to hide
bsd.show()
button_1.setOnClickListener {
bsd.show()
}
}
}
BottomSheetDialog The interior has created BottomSheetBehavior, So we don't need to care BottomSheetBehavior, It can be used directly ,XML We only need to implement our own layout ,XML The layout is as follows
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content">
<TextView android:layout_width="match_parent" android:layout_height="200dp" android:text="aaaaaaaaaaaaaaabbbbbbbbbbbbbccccccccccccc" />
</LinearLayout>
such , our BottomSheetDialog It shows , Look at the effect
You can also inherit BottomSheetDialog And the general Dialog In the same way , Define yourself Dialog, Its behavior and Dialog It's almost the same , So this is just a simple demonstration , The rest is not detailed here
BottomSheetDialogFragment
Let's take a look at BottomSheetDialogFragment, This Fragment The interior has created BottomSheetDialog, So we can use it more conveniently , The code is as follows
class ModalBottomActivity : AppCompatActivity(R.layout.ocr_result_layout) {
val modalBottomSheet = ModalBottomSheet()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
modalBottomSheet.show(supportFragmentManager, ModalBottomSheet.TAG)
button_1.setOnClickListener {
modalBottomSheet.show(supportFragmentManager, ModalBottomSheet.TAG)
}
}
}
class ModalBottomSheet : BottomSheetDialogFragment() {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? = inflater.inflate(
R.layout.modal_buttom_sheet_content,
container,
false
)
companion object {
const val TAG = "ModalBottomSheet"
}
}
So we can achieve BottomSheetDialogFragment The exhibition of , in addition , If we want to be right BottomSheet To set it up , You can get it in this way BottomSheetBehavior object , You can set it freely
class ModalBottomSheet : BottomSheetDialogFragment() {
lateinit var behavior: BottomSheetBehavior<FrameLayout>
override fun onCreateView ...
// Note the need for onStart You can normally get BottomSheetBehavior
override fun onStart() {
super.onStart()
val bottomSheet: FrameLayout =
dialog?.findViewById(R.id.design_bottom_sheet) ?: return
behavior = BottomSheetBehavior.from(bottomSheet)
behavior.peekHeight = 400
}
companion object {
const val TAG = "ModalBottomSheet"
}
}
Okay ,BottomSheet The basic use of has been finished , Do you understand ? in addition , In the end, I recommend a very good similar BottomSheet Third party Library of effects SlidingUpPanelLayout, You can understand and try to use , See which is more suitable for your own needs , Then choose to use
Next time we will use one Demo To make the BottomSheet Nest a list to achieve better results , Hope you enjoy it
Finally, I hope all brothers and sisters will give me a compliment , Close a note , I will share more information with you in succession , Thank you. !
边栏推荐
- 教你自己训练的pytorch模型转caffe(一)
- Make Jar, Not War
- How to renew NPDP? Here comes the operation guide!
- 重上吹麻滩——段芝堂创始人翟立冬游记
- Mathematical analysis_ Notes_ Chapter 9: curve integral and surface integral
- NPDP如何续证?操作指南来了!
- Material Design组件 - 使用BottomSheet展现扩展内容(二)
- 珍爱网微服务底层框架演进从开源组件封装到自研
- Duchefa s0188 Chinese and English instructions of spectinomycin hydrochloride pentahydrate
- Abnova CD81 monoclonal antibody related parameters and Applications
猜你喜欢
Specification of protein quantitative kit for abbkine BCA method
如何让化工企业的ERP库存账目更准确
Applet page navigation
Duchefa d5124 md5a medium Chinese and English instructions
14、Transformer--VIT TNT BETR
基于AVFoundation实现视频录制的两种方式
【刷题记录】1. 两数之和
MySQL fully parses json/ arrays
Promouvoir le développement de l'industrie culturelle et touristique par la recherche, l'apprentissage et l'enseignement pratique du tourisme
Duchefa low melting point agarose PPC Chinese and English instructions
随机推荐
教你自己训练的pytorch模型转caffe(一)
科普|英语不好对NPDP考试有影响吗 ?
Abnova丨 MaxPab 小鼠源多克隆抗体解决方案
解析五育融合之下的steam教育模式
Codeforces Round #804 (Div. 2) - A, B, C
Monorepo管理方法论和依赖安全
ClickHouse 复制粘贴多行sql语句报错
中国管理科学研究院凝聚行业专家,傅强荣获智库专家“十佳青年”称号
2.<tag-哈希表, 字符串>补充: 剑指 Offer 50. 第一个只出现一次的字符 dbc
Duchefa MS medium contains vitamin instructions
haas506 2.0开发教程 - 阿里云ota - pac 固件升级(仅支持2.2以上版本)
MySQL fully parses json/ arrays
How to open an account online for futures? Is it safe?
ts 之 泛型
证券开户选择哪个证券比较好?网上开户安全么?
Applet page navigation
Informatics Orsay all in one 1339: [example 3-4] find the post order traversal | Valley p1827 [usaco3.4] American Heritage
从架构上详解技术(SLB,Redis,Mysql,Kafka,Clickhouse)的各类热点问题
【刷题记录】1. 两数之和
Duchefa丨MS培养基含维生素说明书