使用自定义项目布局时,AutoCompleteTextView 项目不可点击

AutoCompleteTextView items are not clickable when using custom item layout

我正在开发一个使用 AutoCompleteTextView 和自定义项目布局的应用程序,但我无法使项目点击操作起作用。

这是自定义项目布局:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
   <data>
       <variable name="viewModel" type="blog.liderc.familybudget.vms.DepartmentVM"/>
       <variable name="parentViewModel" type="blog.liderc.familybudget.AddSubjectViewModel"/>
   </data>
   <LinearLayout
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:orientation="horizontal">
       <TextView
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:gravity="start"
               android:text="@{viewModel.title}"
               android:textSize="20sp"/>
       <Button
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:gravity="end"
               android:text="x"
               android:textSize="20sp"
               android:onClick="@{() -> parentViewModel.deleteDepartmentAsync(viewModel)}"/>
   </LinearLayout>
</layout>

以下是我的配置方式:

override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
   return activity?.let {
       val builder = AlertDialog.Builder(it)
       val view = LayoutInflater
           .from(it)
           .inflate(R.layout.dialog_add_subject, null)

       viewModel = ViewModelProviders.of(it).get(AddSubjectViewModel::class.java)
       viewModel.subject = SubjectVM()
       viewModel.departmentsAutoComplete = departments
       val app = it.application as FamilyBudgetApp
       viewModel.depsRepo = app.depsRepository.value

       val adapter = ObserverListAdapter(
           departments,
           R.layout.department_autocomplete,
           viewModel) { view.department.refreshAutoCompleteResults() }
       view.department.setAdapter(adapter)

       val binding: ViewDataBinding? = DataBindingUtil.bind(view)
       binding?.run {
           setVariable(BR.viewModel, viewModel)
           executePendingBindings()
       }
       builder.setView(view)
           .setPositiveButton("Remember", positive)
           .setNegativeButton("Nevermind") { dialog, _ -> dialog.cancel() }
       builder.create()
   } ?: throw IllegalStateException("Activity cannot be null")
}

R.layout.department_autocomplete - 是自定义项目布局。

当我输入相应的 AutoCompleteTextView 字段时,建议会被过滤并正确显示,但是当我点击其中任何一个时都没有任何反应。

外观如下: Autocomplete menu

我遇到了和你一样的问题。您必须创建自己的 onItemClick 事件。在此 tutorial 之后,您需要设置自己的 onItemClickListener.

它应该是这样的:

    autoCompleteTextView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                // click event (such as filling the autocomplete section)
            }
        });

编辑:我没有看到你的评论。我必须做的另一件事是在 xml 文件的 <LinearLayout> 中设置 android:descendantFocusability="blocksDescendants"Source