最近在做一个评论弹框的时候遇到两个问题:
1.BottomSheetDialogFragment中的EditText有行数显示,当内容过多后,滑动内容和BottomSheetDialogFragment的上下拖动事件冲突了。解决办法:禁止BottomSheetDialogFragment的上下拖动功能
设置回掉监听:
override var mBottomSheetBehaviorCallback: BottomSheetBehavior.BottomSheetCallback? = object : BottomSheetBehavior.BottomSheetCallback() {
override fun onStateChanged(bottomSheet: View, newState: Int) {
if (newState == BottomSheetBehavior.STATE_DRAGGING || newState == BottomSheetBehavior.STATE_SETTLING) {
mBehavior?.state = BottomSheetBehavior.STATE_COLLAPSED
}
}
override fun onSlide(bottomSheet: View, slideOffset: Float) {}
}
mBehavior = BottomSheetBehavior.from(rootView?.parent as View)
// 直接展开内容
mBehavior?.state = BottomSheetBehavior.STATE_EXPANDED
mBehavior?.isHideable = true
(rootView?.parent as View).setBackgroundColor(Color.TRANSPARENT)
// 设置内容高度
rootView?.post {
mBehavior?.setPeekHeight(rootView?.height ?: 0)
}
// 设置监听
mBottomSheetBehaviorCallback?.let {
mBehavior?.addBottomSheetCallback(it)
}
然后就解决了。
2.输入框被挡住问题:
<style name="BottomSheetDialogStyle" parent="Theme.Design.Light.BottomSheetDialog">
<item name="android:windowIsFloating">false</item>
<item name="android:statusBarColor">@android:color/transparent</item>
<item name="android:windowSoftInputMode">adjustResize</item>
</style>
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setStyle(DialogFragment.STYLE_NORMAL, R.style.BottomSheetDialogStyle)
}
参考自:https://stackoverflow.com/questions/48002290/show-entire-bottom-sheet-with-edittext-above-keyboard