首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >ExpandableListView CheckBox不触发onClick事件

ExpandableListView CheckBox不触发onClick事件
EN

Stack Overflow用户
提问于 2013-06-18 04:59:11
回答 1查看 948关注 0票数 0

在我的ExpandableListView中,每个子行都包含一个CheckBox和一个TextView。一切都运行得很完美(终于!)当我点击TextView的时候。但是,当我点击复选框时,它的状态会发生变化,但onClick事件永远不会触发。没有错误,但实际上什么也没有发生。

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

代码语言:javascript
运行
复制
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

代码语言:javascript
运行
复制
<?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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-06-18 05:23:10

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

CheckBox本身上调用setOnCheckedChangeListener()以安装适当的监听程序。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17156764

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档