简体   繁体   中英

Android onClickListener strange behavior

i'm trying to set a child click listener on my ExpandedListView, and i'm getting some strange results:

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. probably not the most sophisticated solution, but works perfectly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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