【发布时间】:2014-09-09 05:17:52
【问题描述】:
我已经在 android listview 的单选模式下实现了一个带有复选框的自定义列表视图。我想要一个功能,当我点击复选框时,必须添加与该复选框对应的行(或者说“人“该行中显示的对象应该被添加到另一个列表并从该列表中删除)并且在该列表视图上的 itemclick 应该将我带到 android 中的另一个屏幕。我尝试了其他方式,但他们指定复选框需要是可聚焦的:错误。另外我希望点击侦听器仅在复选框上工作。请对此提出任何建议或帮助。提前致谢
这个 id 是 xml 代码。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<CheckBox
android:id="@+id/projects_check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginStart="5dp"
android:layout_marginTop="5dp"
android:focusable="false" />
<TextView
android:id="@+id/project_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/projects_check"
android:layout_alignBottom="@+id/projects_check"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:layout_toEndOf="@+id/projects_check"
android:layout_toRightOf="@+id/projects_check"
android:text="TextView" />
</RelativeLayout>
这是我的 onitem 点击监听代码..
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long arg3) {
Projects info = (Projects) parent.getItemAtPosition(position);
info.toggleChecked();
ProjectsHolder viewHolder = (ProjectsHolder) view.getTag();
viewHolder.getmChoiceSelect().setChecked(info.isChecked());
if (viewHolder.getmChoiceSelect().isChecked()) {
completed_projects.add(info);
mCompProjAdapter.notifyDataSetChanged();
projects_list.remove(info);
mProjAdapter.notifyDataSetChanged();
}
}
【问题讨论】:
-
建议使用自定义适配器并在那里处理复选框和文本视图的单击事件,而不是
setOnItemClickListener用于列表视图 -
但是由于复选框是在自定义适配器中实现的,因此如何才能将复选框单击到片段中
-
您必须在自定义适配器中编写复选框和文本框的点击事件。
标签: android android-listview onitemclicklistener