OnChildClickListener消失

时间:2012-05-21 14:15:39

标签: android android-layout listener

我有一个可扩展的列表视图,当我单击一行时,它会触发 onChildClick onGroupClick ,具体取决于我是否点击了某个子项或组。

enter image description here

如果我在xml布局文件中添加可点击的内容(例如CheckBox),则 onChildClick 不会再触发。 (如果我添加一个简单的textView它也可以工作)

the child_layout

任何人都知道为什么?该项是否覆盖了监听器?

主要代码:

     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;
             }
         });

适配器的代码:

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;
    }

2 个答案:

答案 0 :(得分:2)

问题是因为你的孩子有可聚焦的元素。例如,考虑一个按钮,默认情况下它将处于聚焦状态。因此将其设置为false将执行此操作。

在您的情况下,可能为Checkbox添加以下属性

  android:focusable="false"

这应该可以解决问题。

答案 1 :(得分:2)

为可聚焦元素设置android:focusable="false",例如复选框