ExpandableListView CheckBox不会触发onClick事件

时间:2013-06-17 20:59:11

标签: android checkbox expandablelistview

在我的ExpandableListView中,每个子行包含一个CheckBox和一个TextView。当我点击TextView时,一切都很完美(终于!)。但是,当我点击Checkbox时,其状态会发生变化,但onClick事件永远不会触发。没有错误,但没有真正发生。

我希望textview和复选框都调用相同的行为(即用户应该能够点击行上的任何位置,并且会产生相同的行为)。我究竟做错了什么?谢谢!

public class ExpandListAdapter extends BaseExpandableListAdapter {

    public ExpandListAdapter(Context context, ArrayList<ExpandListGroup> groups) {
        this.context = context;
        this.groups = groups;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild,  View view, final ViewGroup parent) {

        view = getCategoryChildView(groupPosition, childPosition, view);

        view.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    //this works for the textview click but
                    //doesn't fire when the check box is clicked 
                    System.err.println("child clicked");
                }
            });

        return view;
    }

    private View getCategoryChildView(int groupPosition, int childPosition, View view) {

        ClientFinderCategories child = (ClientFinderCategories) getChild(groupPosition, childPosition);
        ViewHolder holder;
        if (view == null) {
            holder = new ViewHolder();
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.expandlist_child_item_category, null);
            holder.checkBox = (CheckBox) view.findViewById(R.id.check_box);
            holder.textView = (TextView) view.findViewById(R.id.expand_list_item);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }       

        try {
            holder.checkBox.setChecked(child.getIsSelected());
            holder.textView.setText(child.getCategory());   
        } catch (Exception e) {}

        return view;
    }

}

layout xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="55dip"
    android:descendantFocusability="blocksDescendants"
    android:orientation="horizontal" >

    <CheckBox
        android:id="@+id/check_box"
        android:layout_marginLeft="30dp"
        android:focusable="false"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/expand_list_item"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:clickable="false"
        android:paddingLeft="10dp"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="@dimen/smart_finder_settings_font_size"
        android:textColor="#FFFFFF" />

</LinearLayout>

1 个答案:

答案 0 :(得分:1)

如果另一个视图中包含的视图(此处:CheckBox中的LinearLayout)处理了一个点击本身(CheckBox会执行,而TextView设置为android:clickable="false")则包含该视图的OnClickListener不被称为。

setOnCheckedChangeListener()本身上调用CheckBox以安装适当的侦听器。