1

我添加了一个ListPopupWindow锚定到一个TextInputLayout. 我遇到的问题是弹出窗口的动画开始于TextInputLayout

在此处输入图像描述

请注意,问题只是动画开始的地方。动画完成后,弹出窗口正确显示在TextInputLayout.

我该怎么做才能让动画在TextInputLayout? 请注意,这不是我自己的动画,而是默认动画。

布局片段:

<com.google.android.material.textfield.TextInputLayout
    android:id="@+id/textInputLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginStart="16dp"
    android:layout_marginTop="16dp"
    android:layout_marginEnd="16dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <com.google.android.material.textfield.TextInputEditText
        android:id="@+id/editText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="hint" />
</com.google.android.material.textfield.TextInputLayout>

代码ListPopupWindow

val arrAdapter = ArrayAdapter<String>(
        this,
        R.layout.dropdown_menu_popup_item,
        listOf("hello", "world")
)

val listPopupWindow = ListPopupWindow(this).apply {
    anchorView = findViewById(R.id.textInputLayout)
    setAdapter(arrAdapter)
}

findViewById<Button>(R.id.button).setOnClickListener {
    listPopupWindow.show()
}

代码也可以在 Github 上找到:https ://github.com/wondering639/stack-listpopupwindow-animation-start

提示:这只是一个简化的测试用例。我知道AutoCompleteTextView,但我确实需要手动显示ListPopupWindow.

4

1 回答 1

1

您可以使用setEpicenterBounds(Rect)并将 Rect 宽度传递给整个宽度,但在锚的底部传递一个平坦的高度。

显然锚视图需要在调用时已经布局,setEpicenterBounds(Rect)因为需要它的大小,调用的最佳位置可能就在之前show(),如果你真的不能在那里做并且锚视图没有使用固定大小,你可能会使用ViewTreeObserver.OnGlobalLayoutListener.

val anchorView = findViewById<View>(R.id.textInputLayout)
val listPopupWindow = ListPopupWindow(this).apply {       
    this.anchorView = anchorView
    setAdapter(arrAdapter)
}

findViewById<Button>(R.id.button).setOnClickListener {
    listPopupWindow.epicenterBounds = Rect(0, anchorView.height, anchorView.width, anchorView.height)
    listPopupWindow.show()
}

在此处输入图像描述

于 2020-08-14T14:03:45.360 回答