Android AutocompleteTextView下拉列表添加了填充

时间:2019-01-16 02:25:11

标签: android android-layout android-listview dropdown autocompletetextview

我有一个AutocompleteTextView,显示了项的自定义布局。

项目正确显示,但是问题在于列表本身在顶部和底部添加了填充。参见下图。红色区域是显示两个项目的列表。顶部和底部的浅灰色是列表添加的填充。列表项布局不包含此填充。如何设置填充或将其从下拉列表中删除?

AutocompompleteTextView dropdown list

这是我的行文件:

row_user_suggestion.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="@color/sentRed"
android:orientation="horizontal">

<ImageView
    android:id="@+id/imageView"
    android:layout_width="40dp"
    android:layout_height="40dp"
    android:layout_marginStart="20dp"
    android:layout_marginTop="10dp"
    android:layout_marginEnd="20dp"
    android:layout_marginBottom="10dp"
    tools:layout_editor_absoluteX="26dp"
    tools:layout_editor_absoluteY="44dp"
    tools:srcCompat="@tools:sample/avatars" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_vertical|start"
    android:orientation="vertical">

    <TextView
        android:id="@+id/usernameTv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:text="TextView"
        android:textSize="18sp" />

    <TextView
        android:id="@+id/fullnameTv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="20dp"
        android:text="TextView"
        android:textSize="14sp" />
</LinearLayout>

加载下拉菜单的布局:

activity_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/gradient"
android:fitsSystemWindows="true">


<ScrollView
    android:id="@+id/scrollView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginStart="20dp"
    android:layout_marginEnd="20dp"
    android:layout_marginBottom="20dp">

    <LinearLayout
        android:id="@+id/layoutForm"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="10dp"
        android:layout_marginTop="20dp"
        android:layout_marginEnd="10dp"
        android:orientation="vertical">

        <TextView
            android:id="@+id/textView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginBottom="20dp"
            android:gravity="center"
            android:text="@string/title_send_transfer"
            android:textAppearance="@android:style/TextAppearance.Holo.Large"
            android:textColor="@android:color/white"
            android:textSize="18sp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent" />

        <Spinner
            android:id="@+id/spinner"
            style="@style/AppTheme.Spinner"
            android:layout_width="match_parent"
            android:layout_height="40dp" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <android.support.design.widget.TextInputLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="20dp"
                android:layout_weight="0.8"
                android:textColorHint="@color/colorVeryLightGray"
                android:theme="@style/AppTheme.WhiteColorAccent">

                <AutoCompleteTextView
                    android:id="@+id/usernameTv"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:dropDownHeight="wrap_content"
                    android:inputType="textPersonName"
                    android:maxLines="1"
                    android:singleLine="true"
                    android:textColor="@android:color/white"
                    android:textColorHighlight="@color/colorWhite"
                    android:textColorHint="@color/colorWhite"
                    android:textColorLink="@color/colorWhite"
                    android:theme="@style/AppTheme.WhiteEditText" />

            </android.support.design.widget.TextInputLayout>

            <ImageButton
                android:id="@+id/scanButton"
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:padding="5dp"
                android:scaleType="fitCenter"
                android:src="@drawable/scan_btn" />
        </LinearLayout>

        <android.support.design.widget.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_marginBottom="20dp"
            android:layout_weight="0.33"
            android:textColorHint="@color/colorVeryLightGray"
            android:theme="@style/AppTheme.WhiteColorAccent">

            <EditText
                android:id="@+id/messageTv"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:inputType="text|number|numberDecimal"
                android:maxLines="1"
                android:singleLine="true"
                android:textColor="@android:color/white"
                android:textColorHighlight="@android:color/white"
                android:textColorHint="@color/colorWhite"
                android:theme="@style/AppTheme.WhiteEditText" />

        </android.support.design.widget.TextInputLayout>
    </LinearLayout>

</ScrollView>

<Button
    android:id="@+id/nextButton"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="50dp"
    android:layout_marginTop="16dp"
    android:layout_marginRight="50dp"
    android:layout_marginBottom="50dp"
    android:text="@string/next"
    android:textStyle="bold"
    android:theme="@style/AppTheme.PrimaryButton"
    app:layout_anchor="@+id/form"
    app:layout_anchorGravity="bottom|center"
    app:layout_constraintBottom_toBottomOf="parent"
    />

我正在使用提到的自定义适配器,我认为这里不相关,因为我没有在此处设置任何填充,而是添加了完整性:

UserFilterAdapter.kt

class UserFilterAdapter: ArrayAdapter<User>, Filterable {
private var mContext: Context? = null
private var mSuggestionList: List<User> = listOf()
private var mFilteredSuggestionList: List<User> = listOf()

constructor(context: Context, suggestionList: List<User>): super(context, R.layout.row_user_suggestion) {
    mContext = context
    mSuggestionList = suggestionList
    mFilteredSuggestionList = mSuggestionList
}

override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
    var view = convertView
    val viewHolder: ViewHolder
    if (view == null) {
        val inflater = mContext?.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
        view = inflater.inflate(R.layout.row_user_suggestion, null)
        viewHolder = ViewHolder()
        viewHolder.fullnameTv = view.findViewById(R.id.fullnameTv) as TextView
        viewHolder.usernameTv = view.findViewById(R.id.usernameTv) as TextView
        viewHolder.imageView = view.findViewById(R.id.imageView) as ImageView
        view.setTag(viewHolder)
    } else {
        viewHolder = view.tag as ViewHolder
    }
    val searchSuggestion = mFilteredSuggestionList[position]
    viewHolder.usernameTv?.text = searchSuggestion.username
    viewHolder.fullnameTv?.text = searchSuggestion.name
    Picasso.get().load(R.drawable.avatar_placeholder_small).into(viewHolder.imageView)
    return view!!
}

fun setSuggestionList(list: List<User>) {
    mSuggestionList = list
    mFilteredSuggestionList = listOf()
    notifyDataSetChanged()
}

class ViewHolder {
    var imageView: ImageView? = null
    var usernameTv: TextView? = null
    var fullnameTv: TextView? = null
}

override fun getCount(): Int {
    return mFilteredSuggestionList.size
}

override fun getItem(position: Int): User {
    val user = mFilteredSuggestionList[position]
    return user
}

override fun getFilter(): Filter {
    return object : Filter() {
        override fun performFiltering(constraint: CharSequence?): FilterResults {
            val results = FilterResults()
            val constraintString = constraint.toString().toLowerCase()
            if (!constraintString.isEmpty()) {
                val tempFilteredList = mutableListOf<User>()
                for (suggestion in mSuggestionList) {
                    val name = suggestion.name
                    if (name.toLowerCase().contains(constraintString) ||
                            suggestion.username.toLowerCase().contains(constraintString)) {
                        tempFilteredList.add(suggestion)
                    }
                }
                results.values = tempFilteredList
                results.count = tempFilteredList.size
            } else {
                results.values = mSuggestionList
                results.count = mSuggestionList.size
            }
            return results
        }

        override fun publishResults(constraint: CharSequence?, results: FilterResults) {
            Log.d("FILTER", "Publishing results for $constraint: $results")
            if (results.values != null) {
                mFilteredSuggestionList = results.values as List<User>
                notifyDataSetChanged()
            }
        }
    }
}

0 个答案:

没有答案