我遇到一个问题,键盘无法正常显示,所以底页只显示了一半(请参见第一张照片)。我想拥有它,以便当用户单击搜索栏时,底部工作表始终会扩展到页面顶部,然后出现键盘(请参见照片2)。我以为问题出在STATE_EXPANDED上,并且以某种方式禁用了键盘,但这不起作用。任何帮助将不胜感激。
class CustomBottomSheetBehavior<T : View>(private val context: Context, attrs: AttributeSet) :
BottomSheetBehavior<T>(context, attrs), GestureDetector.OnGestureListener {
private lateinit var customBottomSheetCallback: CustomBottomSheetCallback<T>
private val gestureDetector = GestureDetector(context, this)
interface BottomSheetStateChangeListener {
fun changeBottomSheetState(state: Int)
}
fun setupCallback(listener: BottomSheetStateChangeListener) {
customBottomSheetCallback = CustomBottomSheetCallback(this, listener)
this.setBottomSheetCallback(customBottomSheetCallback)
}
override fun onTouchEvent(parent: CoordinatorLayout, child: T, event: MotionEvent): Boolean {
// gestureDetector.onTouchEvent(event)
return super.onTouchEvent(parent, child, event)
}
override fun onDown(motionEvent: MotionEvent): Boolean {
return false
}
override fun onShowPress(motionEvent: MotionEvent) {
}
override fun onSingleTapUp(motionEvent: MotionEvent): Boolean {
state = STATE_EXPANDED
return false
}
override fun onScroll(
motionEvent: MotionEvent,
motionEvent1: MotionEvent,
v: Float,
v1: Float
): Boolean {
return false
}
override fun onLongPress(motionEvent: MotionEvent) {
}
override fun onFling(
motionEvent: MotionEvent,
motionEvent1: MotionEvent,
v: Float,
v1: Float
): Boolean {
if(motionEvent.y < motionEvent1.y) {
Log.d("onFling", "down swipe")
customBottomSheetCallback.directFling = DirectFling.Down
} else {
Log.d("onFling", "up swipe")
customBottomSheetCallback.directFling = DirectFling.UP
}
return false
}
enum class DirectFling {
UP, Down
}
internal inner class CustomBottomSheetCallback<T : View>(private val bottomSheetBehavior: CustomBottomSheetBehavior<T>, private val listener: BottomSheetStateChangeListener) :
BottomSheetBehavior.BottomSheetCallback() {
// 4(Collapsed) or 3(Expanded) or 6(Half_Expanded)
var preState = 4
var directFling: DirectFling? = null
private var offset: Float = 0.toFloat()
override fun onStateChanged(view: View, state: Int) {
if(state == STATE_SETTLING) {
when(preState) {
STATE_COLLAPSED -> {
if (directFling == DirectFling.UP) {
bottomSheetBehavior.state = STATE_EXPANDED
}
}
STATE_EXPANDED -> {
if (directFling == DirectFling.Down) {
bottomSheetBehavior.state = STATE_COLLAPSED
}
}
}
}
if (state == STATE_COLLAPSED) {
preState = STATE_COLLAPSED
listener.changeBottomSheetState(STATE_HIDDEN)
Log.d("state collapse", "full collapsed")
//Collapse keyboard
val imm = ContextCompat.getSystemService(view.context, InputMethodManager::class.java)
imm?.hideSoftInputFromWindow(view.windowToken, 0)
//Collapse keyboard
}
if(state == STATE_EXPANDED) {
preState = STATE_EXPANDED
listener.changeBottomSheetState(STATE_EXPANDED)
Log.d("state expandedf", "full expanded")
}
}
override fun onSlide(view: View, offset: Float) {
this.offset = offset
}
}
}
答案 0 :(得分:0)
我做到了!单击搜索按钮时,它是bottomSheetBehavior.state = BottomSheetBehavior.STATE_EXPANDED