简体   繁体   English

Android onClickListener异常行为

[英]Android onClickListener strange behavior

i'm trying to set a child click listener on my ExpandedListView, and i'm getting some strange results: 我试图在我的ExpandedListView上设置一个子级单击侦听器,并且得到一些奇怪的结果:

theList.setOnChildClickListener(new OnChildClickListener() {
    @Override
    public boolean onChildClick(ExpandableListView parent, View view, int groupPosition, int childPosition, long id) {
        Log.i(LOG_TAG, "clicked");

        ExpandableListAdapter adapter = parent.getExpandableListAdapter();
        View v = (View)adapter.getChild(groupPosition, childPosition);

        Toast toast = Toast.makeText(getApplicationContext(), 
                "item: " + new Integer(groupPosition).toString() + ":" + new Integer(childPosition).toString(), 
                Toast.LENGTH_SHORT);
        toast.show();

        if (!((CheckedTextView)view.findViewById(R.id.check)).isChecked()){
            ((CheckedTextView)view.findViewById(R.id.check)).setChecked(true);
        } else {
            ((CheckedTextView)view.findViewById(R.id.check)).setChecked(false);
        }

        return true;
    }
});

it sort of works. 这样的作品。 when i check one of the items, however, another, additional item will also be selected, seemingly at random. 但是,当我检查其中一个项目时,似乎还会随机选择另一个项目。 am i not doin this correctly? 我这样做不正确吗?

Solved the issue myself. 我自己解决了这个问题。 i ended up extending the adapter class and overriding getChildView and getGroupView and added a list of booleans to the adapter to keep up with the checkmarks. 我最终扩展了适配器类,并覆盖了getChildView和getGroupView,并向适配器添加了一个布尔值列表,以跟上复选标记。 probably not the most sophisticated solution, but works perfectly. 可能不是最复杂的解决方案,但效果很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM