[我正在尝试制作一张幻灯片底图,如下图所示(第一个显示了Im想要做的事情,第二个显示了我现在要做的事情)。我尝试了不同的方式,环顾了SO和Web,以查看是否有任何文档,而且似乎没有太多。我下面的代码是我可以使其最接近的代码,但似乎不正确。任何有关创建此代码或任何有用材料的代码的帮助。
MainActivity.kt
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val sheet = DemoBottomSheetFragment()
sheet.show(supportFragmentManager, "DemoBottomSheetFragment")
}
}
class DemoBottomSheetFragment : SuperBottomSheetFragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
super.onCreateView(inflater, container, savedInstanceState)
return inflater.inflate(R.layout.fragment_demo_sheet, container, false)
}
override fun getCornerRadius() = context!!.resources.getDimension(R.dimen.demo_sheet_rounded_corner)
}
activity_main
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
</androidx.constraintlayout.widget.ConstraintLayout>
fragment_demo_sheet
<?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="wrap_content">
<TextView
android:id="@+id/show_sheet"
android:layout_width="wrap_content"
android:layout_height="1000dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
您应该使用Persistent Bottom Sheet,而不是底层对话框
以下链接可能对您有帮助
https://www.androidhive.info/2017/12/android-working-with-bottom-sheet/
使用所需的任何约束自定义布局并传递布局行为
app:layout_behavior="android.support.design.widget.BottomSheetBehavior"
在类文件中使用
使用全局变量
private var mBottomSheetBehavior: BottomSheetBehavior<*>? = null
在视图中创建
mBottomSheetBehavior = BottomSheetBehavior.from(view);
mBottomSheetBehavior?.peekHeight = 0
setBottomSheetAndCallBackBottomSheetBehaviour();
bottomSheetCollapsed();
bottomSheet?.visibility = View.VISIBLE
并且在创建名为method的视图并传递布局ID时,窥视高度首次用于隐藏视图。
/**
* set bottom sheet behavior and state
*/
private fun setBottomSheetAndCallBackBottomSheetBehaviour() {
mBottomSheetBehavior?.state = BottomSheetBehavior.STATE_HIDDEN
//callback
mBottomSheetBehavior?.setBottomSheetCallback(object :
BottomSheetBehavior.BottomSheetCallback() {
override fun onStateChanged(bottomSheet: View, newState: Int) {
if (newState == BottomSheetBehavior.STATE_COLLAPSED) {
bottomSheetCollapsed()
}
}
override fun onSlide(bottomSheet: View, slideOffset: Float) {}
})
}
并且使用以下方法进行扩展和折叠。
private fun bottomSheetExpand() {
mBottomSheetBehavior?.state = BottomSheetBehavior.STATE_EXPANDED
}
private fun bottomSheetCollapsed() {
mBottomSheetBehavior?.state = BottomSheetBehavior.STATE_COLLAPSED
}
并单击查看使用
fun isExpendCollapse(){
if (mBottomSheetBehavior?.state == BottomSheetBehavior.STATE_COLLAPSED) {
bottomSheetExpand()
} else {
bottomSheetCollapsed()
}
}
检查xml文件的CoordinatorLayout对于底表行为是必须的
<android.support.design.widget.CoordinatorLayout
android:id="@+id/bottomSheet"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:clipToPadding="true"
android:visibility="gone"
app:behavior_hideable="true"
app:behavior_peekHeight="0dp"
android:layout_alignParentBottom="true"
>
<View
android:id="@+id/view"
android:layout_width="match_parent"
android:layout_height="300dp"
android:background="@color/colorAccent"
app:layout_behavior="android.support.design.widget.BottomSheetBehavior"
/>
</android.support.design.widget.CoordinatorLayout>
您可以约束布局,线性视图或任何视图,而不是视图。并且我已经将坐标布局设置为相对布局(父布局),您可以根据需要使用。