我正在开发一个使用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
答案 0 :(得分:0)
我和您有同样的问题。您将必须创建自己的 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)
}
});
编辑:我没有看到您的评论。我要做的另一件事是在android:descendantFocusability="blocksDescendants"
文件的<LinearLayout>
中设置xml
。 Source