Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is choiceMode compatible with ExpandableListView?

Tags:

android

Is it possible to make a multiple choice list with android:choiceMode="multipleChoice" or setChoiceMode(ListView.CHOICE_MODE_MULTIPLE) on an ExpandableListView? I am able to do this with CheckBoxes on a plain ListView, but it doesn't seem to be working with ExpandableListView. In the latter, clicking the list item (either parent or child) does not affect the checkbox as it does in the former.

I have noticed that it is possible to click exactly on the checkbox to make it toggle, but this is a very small target.

Here is a relevant unanswered forum post.

like image 294
kostmo Avatar asked May 12 '10 01:05

kostmo


People also ask

What is expandable list view in android?

Android ExpandableListView is a view that shows items in a vertically scrolling two-level list. It differs from a ListView by allowing two levels which are groups that can be easily expanded and collapsed by touching to view and their respective children items.


2 Answers

By default not. 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 the sample code for Multiple choice:

https://github.com/jiahaoliuliu/ExpandableListViewMultipleChoice

like image 134
jiahao Avatar answered Oct 21 '22 08:10

jiahao


After some study, the answer is No. "Checked" status seems to be tracked internally by "position", so that a "parent" (aka "group") item a ways down the list that is checked may transfer its check to a child above it if the previous parent in the list is expanded.

I ended up tracking checkmarks in my own data structure.

like image 42
kostmo Avatar answered Oct 21 '22 07:10

kostmo