我正在使用带复选框和textview的项目的自定义适配器。这是项目的布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="@dimen/standard_margin"
>
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:focusable="false"
android:focusableInTouchMode="false"
/>
<TextView
android:id="@+id/checkBoxDesc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:layout_marginLeft="@dimen/new_small_margin"
android:layout_alignBaseline="@+id/isEbank"
android:layout_alignBottom="@+id/isEbank"
android:layout_toRightOf="@+id/isEbank"
android:layout_alignTop="@+id/isEbank"
android:gravity="left"
android:focusable="false"
android:focusableInTouchMode="false"
/>
列表如下所示:
我喜欢当用户点击列表项时,或者选中复选框来执行相同的操作。但是,当用户单击项目时,onItemClick不会触发。
以下是我的视图代码:
@Override
public View onCreateView(LayoutInflater inflater, final ViewGroup container, Bundle savedInstanceState) {
View mRoot = inflater.inflate(R.layout.fragment_user, container, false);
ArrayList<Option> listOptions=new ArrayList<Option>();
Option optionYes=new Option(true,getResources().getString(R.string.Yes));
listOptions.add(optionYes);
Option optionNo=new Option(false,getResources().getString(R.string.No));
listOptions.add(optionNo);
listView=(ListView)mRoot.findViewById(R.id.lst);
adapter=new OptionAdapter(getActivity(),R.layout.item_option,listOptions);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
CheckBox box = (CheckBox) view.findViewById(R.id.checkBox1);
box.setChecked(true);
}
});
return mRoot;
}
以下是我的适配器代码:
@Override
public View getView(int position, View convertView, final ViewGroup parent) {
final Option selectedOption = optionsList.get(position);
ViewHolder holder = null;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.item_option, null);
holder = new ViewHolder();
holder.optionDesc = (TextView) convertView.findViewById(R.id.optionDesc);
holder.optionDesc.setText(selectedOption.getDesc());
holder.optionCheckBox = (CheckBox) convertView.findViewById(R.id.isEbank);
holder.optionCheckBox.setFocusable(false);
holder.optionCheckBox.setFocusableInTouchMode(false);
convertView.setTag(holder);
holder.optionCheckBox.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
});
} else {
holder = (ViewHolder) convertView.getTag();
}
return convertView;
}
然而它不起作用。
答案 0 :(得分:0)
你应该禁用用户交互将此添加到xml
android:clickable="false"
答案 1 :(得分:0)
我建议您在getView
这样的
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater vi = (LayoutInflater)getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.item_option, null);
holder = new ViewHolder();
holder.name = (CheckBox) convertView.findViewById(R.id.checkBox1);
convertView.setTag(holder);
holder.name.setOnClickListener( new View.OnClickListener() {
public void onClick(View v) {
CheckBox cb = (CheckBox) v ;
//Check if cb is checked or not here
cb.isChecked();
});
}
else {
holder = (ViewHolder) convertView.getTag();
}
}