代码之家  ›  专栏  ›  技术社区  ›  Inn0vative1

Android AutocompleteTextView下拉列表添加填充

  •  0
  • Inn0vative1  · 技术社区  · 7 年前

    我有一个 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 回复  |  直到 7 年前
    推荐文章