Wednesday, 4 September 2013

Using choice mode in ExpandableListView

The choice modes on ListView, like Multiple choice or Single choice, are not used on ExpandableListView, although it could be set.

Checking the source code you can see the follow:

512     @Override
513     public boolean performItemClick(View v, int position, long id) {
514         // Ignore clicks in header/footers
515         if (isHeaderOrFooterPosition(position)) {
516             // Clicked on a header/footer, so ignore pass it on to super
517             return super.performItemClick(v, position, id);
518         }
519         
520         // Internally handle the item click
521         final int adjustedPosition = getFlatPositionForConnector(position);
522         return handleItemClick(v, adjustedPosition, id);
523     }

533         boolean handleItemClick(View v, int position, long id) {
534         final PositionMetadata posMetadata = mConnector.getUnflattenedPos(position);
535         
536         id = getChildOrGroupId(posMetadata.position);
537         
538         boolean returnValue;
539         if (posMetadata.position.type == ExpandableListPosition.GROUP) {
540             /* It's a group, so handle collapsing/expanding */
                ...
579         } else {
580             /* It's a child, so pass on event */
581             if (mOnChildClickListener != null) {
582                 playSoundEffect(SoundEffectConstants.CLICK);
583                 return mOnChildClickListener.onChildClick(this, v, posMetadata.position.groupPos,
584                         posMetadata.position.childPos, id);
585             }
586 
587             returnValue = false;
588         }
589 
590         posMetadata.recycle();
591 
592         return returnValue;
593     }

Source:
http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.2.2_r1/android/widget/ExpandableListView.java#ExpandableListView.handleItemClick%28android.view.View%2Cint%2Clong%29

The problem here is if the row is not header neither footer, the method performItemClick of super class (android.widget.AbsListView) won't be called, which is the one who takes account the choice mode:
http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.2.2_r1/android/widget/AbsListView.java#AbsListView.performItemClick%28android.view.View%2Cint%2Clong%29

So, the unique solution is implement the choice logic in onChildItemClickListener ourself. Here is my sample code for Multiple choice:
https://github.com/jiahaoliuliu/ExpandableListViewMultipleChoice

No comments:

Post a Comment