【问题标题】:Android - Why click the CheckBox does not trigger the ListView's onItemClick?Android - 为什么单击 CheckBox 不会触发 ListView 的 onItemClick?
【发布时间】:2016-07-02 06:47:57
【问题描述】:

问题是:当我点击复选框时,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>

【问题讨论】:

  • 请添加适配器代码。

标签: android listview


【解决方案1】:

在您的复选框上设置 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());
    }
});
.
.
. 

【讨论】:

  • 当我点击复选框时我想触发 ListView 的 onItemClickListener 是因为我想知道有多少项目被选中并且我需要知道数组的位置。
  • 创建一个新方法(函数)并将所有代码放入该方法中,然后在 onItemClickonCheckChange 中调用该方法
  • 我需要从ListView的onItemClickListener中获取位置参数,所以这种方式行不通。
【解决方案2】:

我找到了解决办法。

只需添加这两个属性:

android:clickable="false"
android:focusable="false"

【讨论】:

    猜你喜欢
    • 2012-06-23
    • 2019-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-10
    • 1970-01-01
    • 2014-03-08
    • 1970-01-01
    相关资源
    最近更新 更多