简体   繁体   English

Android AutoCompleteTextView作为ListView标头

[英]Android AutoCompleteTextView as ListView header

I'm trying to set an AutoCompleteTextView as a ListView header, but if I do so the autocomplete box never appears. 我试图将AutoCompleteTextView设置为ListView标头,但是如果这样做,则自动完成框将永远不会出现。 The code for creating the auto complete view comes directly from the Hello, AutoComplete tutorial in googles docs. 创建自动完成视图的代码直接来自googles文档中的Hello,AutoComplete教程。 The COUNTRIES array also comes from there. COUNTRIES数组也来自那里。

protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

ListView myList = (ListView) findViewById(R.id.ResultList);

LayoutInflater layoutInflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
TableLayout searchHeader = (TableLayout) layoutInflater.inflate(R.layout.search_header, null); 
myList.addHeaderView(searchHeader, null, false);

final AutoCompleteTextView textView = (AutoCompleteTextView) myList.findViewById(R.id.edit);
ArrayAdapter<String> searchAdapter = new ArrayAdapter<String>(this,
        android.R.layout.simple_dropdown_item_1line, COUNTRIES);
textView.setAdapter(searchAdapter);
textView.setThreshold(1);

        //Dummy data for listview.

String[] listContent = {
        "test", "test", "test", "test",
        "test", "test", "test", "test"
};
ArrayAdapter<String> adapter = new SearchResultAdapter(this, listContent);
myList.setAdapter(adapter);

}

As a test, I added a TextChangedListener to try and force show the dialog 作为测试,我添加了TextChangedListener尝试强制显示对话框

    textView.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void afterTextChanged(Editable s) {
            textView.showDropDown();
        }
    });

The dialog appears but is closed almost instantly. 对话框出现,但几乎立即关闭。 I wonder if some kind of event bubbling from the list view is causing this? 我想知道是否从列表视图冒泡的某种原因导致了吗?

This question regarding focus of EditText views within a ListView & some reading of the AutoCompleteTextView source helped me find the answer. 这个问题有关的一个ListView与该AutoCompleteTextView源的一些阅读中的EditText意见重点帮助我找到答案。 Setting the order of focus on the ListView to afterDescendants allowed the dialog to be shown normally. 将ListView的焦点顺序设置为afterDescendants,可以正常显示对话框。

<ListView
    android:id="@+id/ResultList"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_marginBottom="50dip"
    android:descendantFocusability="afterDescendants"
    android:fadingEdge="none" >
</ListView>

在您的XML文件中使用autocompletetextview可能会有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM