使用自定义项目布局时,无法单击自动完成文本视图项目



我正在开发一个使用自动完成文本视图和自定义项目布局的应用程序,但我无法使项目单击操作起作用。

以下是自定义项目布局:

<?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 - 是自定义项目布局。

当我输入相应的自动完成文本视图字段时,建议被过滤并正确显示,但当我点击其中任何一个时没有任何反应。

下面是它的外观: 自动完成菜单

我和你有同样的问题。您必须创建自己的onItemClick事件。按照本教程,您需要设置自己的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"。源

最新更新