AutoCompleteTextView 不调整高度

问题描述 投票:0回答:1

我有一个带有自定义数组适配器的 autoCompleteTextView,但是当我在用户键入时过滤结果时,列表视图似乎具有固定大小。

在第一个示例中,我有 3 个匹配结果,在第二个示例中,有 1 个。正如您在屏幕截图底部所注意到的,列表视图具有固定大小。 我尝试即时计算结果大小的高度,如下所示,但它不起作用

private fun adjustListHeight() {
        // After calling adapter.notifyDataSetChanged()
        val filteredItemCount = autoCompleteAdapter.getFilteredItemCount()

        // Calculate the height of the dropdown list based on the number of items and the height of the first item
        val firstItemView = autoCompleteAdapter.getView(0, null, binding.descriptionTextInputLayout)
        val measureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)
        firstItemView.measure(measureSpec, measureSpec)
        val itemHeight = firstItemView.measuredHeight

        val verticalPadding = firstItemView.paddingTop + firstItemView.paddingBottom

        val dropdownHeight = if (filteredItemCount > 0) {
            // Calculate the total height of the dropdown list based on the number of items and item height
            val totalHeight = (filteredItemCount * itemHeight) + (2 * verticalPadding)
            totalHeight
        } else {
            // Default height when no items are available
            ViewGroup.LayoutParams.WRAP_CONTENT
        }

        // Set the calculated height to the AutoCompleteTextView's dropdown list
        binding.descriptionEditText.dropDownHeight = dropdownHeight
    }

我在适配器中的notifyDataChanged()之后立即调用上面的方法。

这也是xml中的视图

<AutoCompleteTextView
                android:id="@+id/descriptionEditText"
                style="@style/TextAppearance.FontPath.Regular"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:cursorVisible="true"
                android:ellipsize="end"
                android:inputType="textCapCharacters"
                android:maxLines="1"
                android:dropDownHeight="wrap_content"
                android:paddingTop="8dp"
                android:textColor="@color/global_gray_dark_text"
                android:textCursorDrawable="@drawable/drawable_cursor_color"
                android:textSize="20sp"
                android:theme="@style/customCursor"
                tools:text="69774839234234234234234234234"
                tools:drawableEnd="@drawable/ic_contacts_2"/>

我需要列表视图来调整过滤后的项目高度

android kotlin view adapter autocompletetextview
1个回答
0
投票

对于任何有兴趣的人来说,答案就在这里。两种解决方案都有效

AutoCompleteTextView 下拉高度未换行到内容

© www.soinside.com 2019 - 2024. All rights reserved.