0

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

我希望 textview 和 checkbox 都调用相同的行为(即用户应该能够单击行上的任意位置并且将产生相同的行为)。我究竟做错了什么?谢谢!

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

}

布局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>
4

1 回答 1

1

如果包含在另一个视图中的视图(此处:CheckBoxin LinearLayout)处理单击本身(CheckBox 执行而 TextView 被设置android:clickable="false"),则不调用包含视图的 OnClickListener 。

调用自身来安装适当的监听器setOnCheckedChangeListener()CheckBox

于 2013-06-17T21:23:10.120 回答