禁用ListView中CheckBox的交互性

时间:2022-04-03 09:19:50

I'm trying to have a ListView where each row has some text and a checkbox. I know that this is a solved problem, but I tried to do it myself anyways and am very close to getting it working.

我正在尝试使用ListView,其中每行都有一些文本和一个复选框。我知道这是一个已经解决的问题,但无论如何我都试图自己做,并且非常接近让它正常工作。

Here's my demo app on github.

这是我在github上的演示应用程序。

Clicking on a ListView item anywhere other than the checkbox correctly registers that the box is checked. Clicking on the box doesn't cause an OnItemClick event and thus the fact that the box is checked isn't registered.

单击复选框以外的任何位置的ListView项正确注册该复选框。单击该框不会导致OnItemClick事件,因此未选中已选中该框的事实。

I could add a listener to each checkbox and maintain state inside of the adapter which would be nice, but I need to have some way of letting the HomeActivity know when the list has applications checked and when it doesn't (to enable/disable some buttons). I'm considering using a Callback for that, but I've never done it in Java.

我可以为每个复选框添加一个监听器并维护适配器内部的状态,这很好,但我需要有一些方法让HomeActivity知道列表何时检查应用程序以及何时不检查(启用/禁用某些应用程序)纽扣)。我正在考虑使用Callback,但我从来没有用Java做过。

To address some common topics in this problem:

要解决此问题中的一些常见主题:

  • Checkbox has android:focusable and android:focusableInTouchMode both set to false
  • Checkbox有android:focusable和android:focusableInTouchMode都设置为false
  • I tried android:clickable="false" on the checkbox but then the OnItemClick method for the list was never called.
  • 我在复选框上尝试了android:clickable =“false”,但是从未调用过该列表的OnItemClick方法。
  • I maintain the state of the checkboxes in a HashMap so they can be set correctly in the adapter when views are reused.
  • 我维护HashMap中复选框的状态,以便在重用视图时可以在适配器中正确设置它们。

3 个解决方案

#1


1  

Disabling checkbox clickable inside the getView() method as

在getView()方法中禁用复选框可单击

((CheckBox) result.findViewById(R.id.checkBox)).setClickable(false);

works for me.

适合我。

#2


0  

Since you have set checkbox focusable and focusableInTouchMode as false, it will not get any event. See either click listener of list will work or that of checkbox, you cannot have both of them working at the same time.

由于您已将checkbox focusable和focusableInTouchMode设置为false,因此不会获得任何事件。请参阅列表的单击侦听器或复选框的单击侦听器,您不能同时使它们同时工作。

What you can do is that, attach the listener in bindView on the view, and also on the checkbox. In this way you can get the listener call in the HomeActivity when either row or checkbox is clicked...

你可以做的是,在视图上的bindView中附加监听器,也在复选框上。这样,当单击行或复选框时,您可以在HomeActivity中获取侦听器调用...

You can create interface for it..........

你可以为它创建接口..........

Public Interface MyClickListener{
    public void onClick();

}

}

Create object MyClickListener object;
object.onClick();

创建对象MyClickListener对象; object.onClick();

#3


0  

This will get all the views in your listview and will get the checkbox in each view and adds it to a list if it is checked:

这将获取列表视图中的所有视图,并将获得每个视图中的复选框,并将其添加到列表中(如果已选中):

List<CheckBox> listOfCheckboxes = new ArrayList<CheckBox>();
List<View> listOfViews = new ArrayList<View>();
listView1.reclaimViews(listOfViews);
for (View v : listOfViews)
{
    CheckBox cb = (CheckBox)v.findViewById(R.id.checkbox);
    if (cb.isChecked())
    {
        listOfCheckboxes.add(cb);
    }
}

#1


1  

Disabling checkbox clickable inside the getView() method as

在getView()方法中禁用复选框可单击

((CheckBox) result.findViewById(R.id.checkBox)).setClickable(false);

works for me.

适合我。

#2


0  

Since you have set checkbox focusable and focusableInTouchMode as false, it will not get any event. See either click listener of list will work or that of checkbox, you cannot have both of them working at the same time.

由于您已将checkbox focusable和focusableInTouchMode设置为false,因此不会获得任何事件。请参阅列表的单击侦听器或复选框的单击侦听器,您不能同时使它们同时工作。

What you can do is that, attach the listener in bindView on the view, and also on the checkbox. In this way you can get the listener call in the HomeActivity when either row or checkbox is clicked...

你可以做的是,在视图上的bindView中附加监听器,也在复选框上。这样,当单击行或复选框时,您可以在HomeActivity中获取侦听器调用...

You can create interface for it..........

你可以为它创建接口..........

Public Interface MyClickListener{
    public void onClick();

}

}

Create object MyClickListener object;
object.onClick();

创建对象MyClickListener对象; object.onClick();

#3


0  

This will get all the views in your listview and will get the checkbox in each view and adds it to a list if it is checked:

这将获取列表视图中的所有视图,并将获得每个视图中的复选框,并将其添加到列表中(如果已选中):

List<CheckBox> listOfCheckboxes = new ArrayList<CheckBox>();
List<View> listOfViews = new ArrayList<View>();
listView1.reclaimViews(listOfViews);
for (View v : listOfViews)
{
    CheckBox cb = (CheckBox)v.findViewById(R.id.checkbox);
    if (cb.isChecked())
    {
        listOfCheckboxes.add(cb);
    }
}