1、bottomsheet(NestedScrollView)
activity代码:
public class BottomSheetActivity extends AppCompatActivity {
BottomSheetBehavior behavior;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bottom_sheet);
View bottomSheet = findViewById(R.id.bottom_sheet);
behavior = BottomSheetBehavior.from(bottomSheet);
behavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
@Override
public void onStateChanged(@NonNull View bottomSheet, int newState) {
//这里是bottomSheet 状态的改变
}
@Override
public void onSlide(@NonNull View bottomSheet, float slideOffset) {
//这里是拖拽中的回调,根据slideOffset可以做一些动画
}
});
}
public void doclick(View v)
{
switch (v.getId()) {
case R.id.button0:
if(behavior.getState() == BottomSheetBehavior.STATE_EXPANDED) {
behavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
}else {
behavior.setState(BottomSheetBehavior.STATE_EXPANDED);
}
break;
}
}
}
activity_bottom_sheet.xml 代码
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:id="@+id/button0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="doclick"
android:text="展示BottomSheet"/>
</LinearLayout>
<androidx.core.widget.NestedScrollView
android:id="@+id/bottom_sheet"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:behavior_hideable="true"
app:behavior_peekHeight="0dp"
app:layout_behavior="@string/bottom_sheet_behavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@color/purple_200"
android:gravity="center"
android:textColor="@android:color/white"
android:text="这是BottomSheets,拖动试试"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:scaleType="centerCrop"
android:src="@mipmap/ic_launcher"/>
</LinearLayout>
</androidx.core.widget.NestedScrollView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
上面的代码转换为kotlin的代码为:
class MainActivity : AppCompatActivity() {
var behavior: BottomSheetBehavior<*>? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val btShow = findViewById<Button>(R.id.bt_show)
val bottomSheet = findViewById<NestedScrollView>(R.id.bottom_sheet);
behavior = BottomSheetBehavior.from(bottomSheet)
behavior!!.addBottomSheetCallback(object : BottomSheetBehavior.BottomSheetCallback(){ //如果不需要可以省略
override fun onStateChanged(bottomSheet: View, newState: Int) {
//这里是bottomSheet 状态的改变
}
override fun onSlide(bottomSheet: View, slideOffset: Float) {
//这里是拖拽中的回调,根据slideOffset可以做一些动画
}
})
btShow.setOnClickListener { showBS() }
}
private fun showBS() {
if(behavior!!.getState() == BottomSheetBehavior.STATE_EXPANDED) {
behavior!!.setState(BottomSheetBehavior.STATE_COLLAPSED);
}else {
behavior!!.setState(BottomSheetBehavior.STATE_EXPANDED);
}
}
}
二、BottomSheetDialog
1、显示
val bottomSheetDialog = BottomSheetDialog(this)
bottomSheetDialog.setContentView(R.layout.dialog_bottom_sheet)
bottomSheetDialog.show()
2、layout.dialog_bottom_sheet.xml代码
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingTop="80dp"
android:paddingBottom="80dp"
android:text="BottomSheetDialog"
android:textSize="30sp"
android:textStyle="bold" />
三、BottomSheetDialogFragment
1、显示
val fragmentManager: FragmentManager = supportFragmentManager
MyBottomSheetDialog.newInstance("参数1", "参数2").show(fragmentManager, "标识")
2、BottomSheetDialogFragment代码
class MyBottomSheetDialog : BottomSheetDialogFragment() {
private lateinit var binding: DialogMyBottomSheetBinding
/**
* setStyle 圆角效果
*/
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setStyle(STYLE_NORMAL, R.style.BottomSheetDialog)
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
binding = DialogMyBottomSheetBinding.inflate(inflater)
initView()
return binding.root
}
private fun initView() {
//do something
}
/**
* 设置固定高度
*/
override fun onStart() {
super.onStart()
// //拿到系统的 bottom_sheet
// val view: FrameLayout = dialog?.findViewById(R.id.design_bottom_sheet)!!
// //获取behavior
// val behavior = BottomSheetBehavior.from(view)
// //设置弹出高度
// behavior.peekHeight = 350
}
companion object {
@JvmStatic
fun newInstance(
params1: String,
params2: String
) =
MyBottomSheetDialog().apply {
// 这里可以作为参数赋值
// mCountryBean = params1
// this.phoneNum = params2
}
}
}
3、MyBottomSheetDialog 的xml代码
<?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"
android:orientation="vertical"
android:padding="10dp">
<!-- android:background="@drawable/shape_sheet_dialog_bg"-->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:padding="10dp"
android:text="标题:我是***"
android:textColor="@android:color/black"
android:textSize="20sp"
android:textStyle="bold" />
</LinearLayout>
them.xml里面
<!--实现BottomSheetDialog圆角效果-->
<style name="BottomSheetDialog" parent="Theme.Design.Light.BottomSheetDialog">
<item name="bottomSheetStyle">@style/bottomSheetStyleWrapper</item>
</style>
<style name="bottomSheetStyleWrapper" parent="Widget.Design.BottomSheet.Modal">
<item name="android:background">@android:color/transparent</item>
</style>
三个类型的底部弹窗代码都已经整理好了,下载地址为: