问题是:当我点击复选框时,ListView的OnItemClickListener没有被触发。
我谷歌寻求答案,但无法找到解决方案。
任何人都可以帮助我吗?我想知道为什么这不起作用。
在VacantHouseActivityFragment的onCreateView方法中:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_vacant_house, container, false);
ListView vacantHouseList = (ListView) rootView.findViewById(R.id.vacant_house_list);
vancantHouseAdapter = new VacantHouseListAdapter(
getActivity(),
R.layout.vacant_house_list_item,
new ArrayList<VacantHouse>()
);
vacantHouseList.setAdapter(vancantHouseAdapter);
vacantHouseList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
CheckBox vacantHouseCheckbox = (CheckBox) view.findViewById(R.id.vacant_house_checkbox);
Log.v("TAG", (String) vacantHouseCheckbox.getText());
}
});
return rootView;
}
fragment_vacant_house.xml的内容:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".VacantHouseActivityFragment"
tools:showIn="@layout/activity_vacant_house">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/vacant_house_list" />
</FrameLayout>
vacant_house_list_item.xml的内容:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New CheckBox"
android:id="@+id/vacant_house_checkbox"
android:focusable="false" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Small Text"
android:id="@+id/vacant_house_rent"
android:textColor="@color/abc_btn_colored_borderless_text_material"
android:textSize="@dimen/notification_subtext_size"
android:focusable="false" />
</LinearLayout>
答案 0 :(得分:0)
在复选框上设置onCheckChangeListener
.
.
.
vacantHouseList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
CheckBox vacantHouseCheckbox = (CheckBox) view.findViewById(R.id.vacant_house_checkbox);
vacantHouseCheckbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
// checkbox is checked now
//your logic code here
} else {
// checkbox is unchecked now
//your logic code here
}
}
});
Log.v("TAG", (String) vacantHouseCheckbox.getText());
}
});
.
.
.
答案 1 :(得分:0)
我找到了解决方案。
只需添加以下两个属性:
android:clickable="false"
android:focusable="false"