Mark
Mark

Reputation: 1031

OnChildClickListener disappears

I have an expandable listview, when i click a row it fires the onChildClick or the onGroupClick depending if i'm clicking a child or a group.

enter image description here

If i add something clickable in the xml layout file (eg. a CheckBox) the onChildClick doesn't fire anymore. (if i add a simple textView it works aswell)

the child_layout

Anyone knows why? Is the item overwriting the listener?

The main code:

     View firstView = inflater.inflate(R.layout.main_layout, container, false);
 listView = (ExpandableListView) firstView.findViewById(R.id.expandableListView1);
listView.setOnChildClickListener(new OnChildClickListener()
         {

             @Override
             public boolean onChildClick(ExpandableListView arg0, View arg1, int arg2, int arg3, long arg4)
             {
                 Toast.makeText(getActivity().getBaseContext(), "Child clicked", Toast.LENGTH_LONG).show();
                 return false;
             }
         });

code of the adapter:

public class ExpandableListAdapter extends BaseExpandableListAdapter {

    private Context context;

    private ArrayList<String> groups;

    private ArrayList<ArrayList<FarmaciListItem>> children;

    public ExpandableListAdapter(Context context, ArrayList<String> groups,
            ArrayList<ArrayList<FarmaciListItem>> children) {
        this.context = context;
        this.groups = groups;
        this.children = children;
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return children.get(groupPosition).get(childPosition);
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    // Return a child view. You can load your custom layout here.
    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
            View convertView, ViewGroup parent) {
        FarmaciListItem farmacoItem = (FarmaciListItem) getChild(groupPosition, childPosition);
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.child_layout, null); 
        }
        TextView tv = (TextView) convertView.findViewById(R.id.tvChild);
        tv.setText("   " + farmacoItem.getName());

        // Depending upon the child type, set the imageTextView01
        tv.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
        if (farmacoItem instanceof PesoItem) {
            tv.setCompoundDrawablesWithIntrinsicBounds(R.drawable.car, 0, 0, 0);
        }
        return convertView;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return children.get(groupPosition).size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return groups.get(groupPosition);
    }

    @Override
    public int getGroupCount() {
        return groups.size();
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    // Return a group view. You can load your custom layout here.
    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
            ViewGroup parent) {
        String group = (String) getGroup(groupPosition);
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.group_layout, null);
        }
        TextView tv = (TextView) convertView.findViewById(R.id.tvGroup);
        tv.setText(group);
        return convertView;
    }

Upvotes: 1

Views: 1500

Answers (2)

Andro Selva
Andro Selva

Reputation: 54322

The problem is because you are having focusable elements in your child. For example consider a button, it will be in focused state by default. So setting it to false will do it.

In your case, maybe for the Checkbox add the below attribute

  android:focusable="false"

this should do the trick.

Upvotes: 2

2red13
2red13

Reputation: 11227

set android:focusable="false" for the focusable element eg checkbox

Upvotes: 2

Related Questions