создание выпадающего списка в виде вида в макете, например, относительного вида или другого вида - PullRequest
1 голос
/ 16 марта 2020

У меня есть AutoCompleteTextView, и он хорошо работает с выпадающим списком. Я хочу показать выпадающий список как часть макета в AutoCompleteTextView, а не поверх макета. Вот макет

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:animateLayoutChanges="true">
    <com.google.android.material.tabs.TabLayout
        android:id="@+id/tabLayout11"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabIndicatorColor="@color/year_tablayout"
        android:layout_below="@+id/top_menu"
        android:background="@color/year_tablayout"
        app:tabIndicator="@null"
        app:tabMode="scrollable">
    </com.google.android.material.tabs.TabLayout>
    <AutoCompleteTextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/autocomplete_city"
        android:layout_width="match_parent"
        android:background="@null"
        android:hint="Search Here ..."
        android:completionThreshold="0"
        android:textColorHint="#aaa"
        android:paddingLeft="@dimen/_10sdp"
        android:textCursorDrawable="@drawable/cursor"
        android:layout_height="wrap_content"
        android:drawableRight="@drawable/ic_search_black_24dp"
        android:paddingRight="10dp"/>
    <com.proj.www.ui.mainpage.NonSwipeableViewPager
        android:id="@+id/viewPager_city"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_below="@+id/top_menu"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:background="@color/tumblr_white"
        />
</LinearLayout>

, а вот фрагмент кода

  public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.city_fragment, container, false);
        AutoCompleteTextView autoCity = view.findViewById(R.id.autocomplete_city);
        String[] city = getResources().getStringArray(R.array.city_array);
        ArrayAdapter<String> autoAdapter = new ArrayAdapter<String>(getContext(), android.R.layout.simple_list_item_1, city);
        autoCity.setAdapter(autoAdapter);
}}

Я хочу сделать выпадающий список, поскольку представление принадлежит макету и отображается под AutoCompleteTextView. Заранее спасибо.

1 Ответ

0 голосов
/ 17 марта 2020

Я надеюсь понять, что вы хотите:

activity_main. xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:orientation="vertical">
    <androidx.appcompat.widget.AppCompatAutoCompleteTextView
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

MainActivity.kt

import android.os.Bundle
import android.view.View
import android.widget.ArrayAdapter
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val savedemail = listOf("red", "blue", "green", "yellow")
        val adapter = ArrayAdapter<String>(
            this, // Context
            android.R.layout.simple_dropdown_item_1line, // Layout
            savedemail // Array
        )
        text.setAdapter(adapter)
        text.threshold = 1
        text.onFocusChangeListener = View.OnFocusChangeListener { view, b ->
            if (b) {
                text.showDropDown()
            }
        }
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...