2

Goal: I am trying to create a UI with 2 checkboxes per line, each line seperated into sections. I've included a screenshot of the UI below.

Current approach: I am using an ExpandableListView and handeling the data with an onChildClick.

Problem: When you click a checkbox, it does not trigger onChildClick or anything else. Clicking anywhere outside of the checkbox will trigger this event.

Research: There are lots of threads that suggest setting android:focusable="false", but that doesn't change anything for me. I have focusable set to false for every element in my UI.

Reproduction: I have the exact same problem running the code from this article without modification, which includes android:focusable="false". I based a lot of my code on that example, and If I can get it working using that codebase, I'm sure I can get it working in mine.

Current UI

  • Need to clarify - when a checkbox is clicked, you want it to fire both the checkbox's event AND the ELV row event? Is that correct? – SBerg413 Jun 21 '12 at 19:41
  • I don't really care what it fires as long as I can use that fire to update the data. So I guess I need it to fire something that will give me the group, child row, and the state of each checkbox. I'm pretty sure I can get those in OnChildClick. Since I use the same 2 checkboxs for each of these rows, I don't think I can do that with a listener. – Robert Kuykendall Jun 21 '12 at 19:44
  • Everything is still focusable in touch mode. You need to turn that off by adding `android:focusableInTouchMode="false"`. See my answer below. – Barak Jun 22 '12 at 06:30

3 Answers3

4

You are missing a line in your XML I bet.

You need to have these two lines in your xml (the second one being the most important):

android:focusable="false"
android:focusableInTouchMode="false"

That will allow each checkbox to be clickable separately as well as the row itself.

Then you set onCheckChangedListener listeners in your adapter getView() to deal with the checkboxes (where it will have access to the position, view and parent info). Your onChildClick can then deal with the rwo clicks.

Barak
  • 16,318
  • 9
  • 52
  • 84
3

As far as I figured it out, the checkbox does consume the event so the OnChildClickListener doesn't trigger. In the tag list item I added these lines to the checkbox:

<CheckBox
    ...
    android:focusable="false"
    android:clickable="false"
    android:focusableInTouchMode="false"/>

Then I could simply use my OnChildClickListener as if I wouldn't have used a checkbox.

bergjs
  • 125
  • 2
  • 6
0

I think it's because the checkbox consumes the event and doesn't forward it to the list item.

If you want to get the click on the boxes you can add OnClickListener to the boxes in the getView() of the list adapter.

I understand you right, the box is at least getting checked? If you don't need custom behaviour you just can read if the box is checked or not when you "submit" the list (or whatever is your next step of checking boxes).

After reading comments, here is your code example with some additions:

@Override
public View getView(final int pos, final View convView, ViewGroup parent) {

    //initialize convView, define other local variables, etc. 

    CheckBox cb = (CheckBox) convView.findViewById(R.id.mycheckbox);
    cb.setOnCheckedChangeListener(new OnCheckedChangeListener() { 

        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
            Log.d("ElistCBox2", "listitem position: " + pos); 
        } 
    });

    return convView;
}

Note that you have to use final modifier for the variables which you use in the anonymous class, otherwise you get a compiler error.

User
  • 31,811
  • 40
  • 131
  • 232
  • I can add an OnClickListener to the checkboxes, but as far as I know that wouldn't give me information on WHICH checkbox was clicked AKA group and child. As far as submission, I was hoping to just process each check change without requiring a submit. – Robert Kuykendall Jun 21 '12 at 19:52
  • If you add an OnClickListener (anonymous class) to the checkbox inside getView() you will capture the local variables there, like position, convertView, etc. So you have access to the item where you currently are. – User Jun 21 '12 at 19:55
  • Working on implementing this now using the demo application. – Robert Kuykendall Jun 21 '12 at 20:03
  • I am reasonably sure I implemented an anonymous listener, but it doesn't have access to any of the outside variables: `cb.setOnCheckedChangeListener(new OnCheckedChangeListener() { public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { Log.d("ElistCBox2", groupPosition); } });` Sorry for the formatting, these comments are pretty simplistic. – Robert Kuykendall Jun 21 '12 at 20:38
  • I added a code example containing your code in my post. I hope it helps. – User Jun 21 '12 at 20:46
  • Ah, I wasn't adding `final` to my variables in the call. It's working! I'll keep coding, and hopefully I can use this to make the saves I want. – Robert Kuykendall Jun 21 '12 at 21:04
  • Ouch. Listeners were triggered correctly, but check boxes are being changed at random. Just posting in case somebody comes upon this thread. – Robert Kuykendall Jun 25 '12 at 20:03
  • I don't think so either. I started a second question for this issue: http://stackoverflow.com/questions/11233578/android-checkboxes-randomly-checked-unchecked-in-expandable-list – Robert Kuykendall Jun 27 '12 at 19:44