我已经在android listview的单选模式中实现了一个带有复选框的自定义列表视图。我想要一个功能,当我点击复选框时,必须添加与该复选框对应的行(或者说是&# 34; Person"该行中显示的对象应该被添加到另一个列表并从该列表中删除)并且在listview上的listview上应该将我带到android中的另一个屏幕。我尝试了其他方式,但是他们指定了复选框需要是可伪装的:false。我也希望点击监听器只在复选框上工作。请提出任何建议或帮助。谢谢提前
这是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();
}
}
答案 0 :(得分:0)
要管理点击行本身,您只需为listView本身编写侦听器即可。 这就是我管理复选框点击的方式。
class CustomAdapter extends ArrayAdapter<BeanClass> {
private ArrayList<BeanClass> items;
public FieldPlannedAdapter(Context context,
ArrayList<BeanClass> items) {
super(context, R.layout.custom_list_row, items);
this.items = items;
}
public class ViewHolder {
protected CheckBox checkbox;
protected TextView name;
}
@Override
public View getView(final int position, View convertView,
ViewGroup parent) {
final ViewHolder viewHolder;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) getActivity()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.custom_list_row,
null);
viewHolder = new ViewHolder();
viewHolder.checkbox = (CheckBox) convertView
.findViewById(R.id.checkIsMissed);
viewHolder.name = (TextView) convertView
.findViewById(R.id.txtViewMTPname);
convertView.setTag(viewHolder);
viewHolder.checkbox
.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
final CheckBox cb = (CheckBox) v;
final BeanClass item = (BeanClass) cb
.getTag();
item.setSelected(cb.isChecked());
if (cb.isChecked()) {
// DO SOMETHING
} else {
}
}
});
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
BeanClass item = items.get(position);
viewHolder.name.setTag(item);
viewHolder.checkbox.setTag(item);
viewHolder.name.setText(item.getName());
viewHolder.checkbox.setChecked(item.isSelected());
if (viewHolder.checkbox.isChecked()) {
// DO SOMETHING
} else {
// DO SOMETHING
}
return convertView;
}
}
自定义行示例:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp" >
<LinearLayout
android:id="@+id/row"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/card_ui"
android:descendantFocusability="blocksDescendants"
android:orientation="vertical"
android:paddingBottom="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="10dp" >
<TextView
android:id="@+id/txtViewMTPname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Customer Name"
android:textColor="@drawable/text_selector"
android:textSize="14sp"
android:textStyle="bold" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:id="@+id/txtViewMtpDarCity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="dgdgdgdg"
android:textColor="@drawable/text_selector"
android:textSize="16sp"
android:textStyle="italic" />
<CheckBox
android:id="@+id/checkIsMissed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:gravity="center"
android:text="Missed" />
</LinearLayout>
</LinearLayout>
</FrameLayout>