Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 7628fbe0 authored by Gilles Debunne's avatar Gilles Debunne Committed by Android (Google) Code Review
Browse files

Merge "Fixes for ExpandableListView with headers/footers"

parents a33097d6 47ccdf37
Loading
Loading
Loading
Loading
+25 −16
Original line number Diff line number Diff line
@@ -482,18 +482,21 @@ public class ExpandableListView extends ListView {
        return mAdapter;
    }
    
    private boolean isHeaderOrFooterPosition(int position) {
        final int footerViewsStart = mItemCount - getFooterViewsCount();
        return (position < getHeaderViewsCount() || position >= footerViewsStart);
    }

    @Override
    public boolean performItemClick(View v, int position, long id) {
        // Ignore clicks in header/footers
        final int headerViewsCount = getHeaderViewsCount();
        final int footerViewsStart = mItemCount - getFooterViewsCount();

        if (position < headerViewsCount || position >= footerViewsStart) {
        if (isHeaderOrFooterPosition(position)) {
            // Clicked on a header/footer, so ignore pass it on to super
            return super.performItemClick(v, position, id);
        }
        
        // Internally handle the item click
        final int headerViewsCount = getHeaderViewsCount();
        return handleItemClick(v, position - headerViewsCount, id);
    }
    
@@ -689,8 +692,8 @@ public class ExpandableListView extends ListView {
    }
    
    /**
     * Converts a flat list position (the raw position of an item (child or
     * group) in the list) to an group and/or child position (represented in a
     * Converts a flat list position (the raw position of an item (child or group)
     * in the list) to an group and/or child position (represented in a
     * packed position). This is useful in situations where the caller needs to
     * use the underlying {@link ListView}'s methods. Use
     * {@link ExpandableListView#getPackedPositionType} ,
@@ -699,10 +702,16 @@ public class ExpandableListView extends ListView {
     * 
     * @param flatListPosition The flat list position to be converted.
     * @return The group and/or child position for the given flat list position
     *         in packed position representation.
     *         in packed position representation. #PACKED_POSITION_VALUE_NULL if
     *         the position corresponds to a header or a footer item.
     */
    public long getExpandableListPosition(int flatListPosition) {
        PositionMetadata pm = mConnector.getUnflattenedPos(flatListPosition);
        if (isHeaderOrFooterPosition(flatListPosition)) {
            return PACKED_POSITION_VALUE_NULL;
        }

        final int shiftedPosition = flatListPosition - getHeaderViewsCount();
        PositionMetadata pm = mConnector.getUnflattenedPos(shiftedPosition);
        long packedPos = pm.position.getPackedPosition();
        pm.recycle();
        return packedPos;
@@ -724,7 +733,7 @@ public class ExpandableListView extends ListView {
                .obtainPosition(packedPosition));
        int retValue = pm.position.flatListPos;
        pm.recycle();
        return retValue;
        return retValue + getHeaderViewsCount();
    }

    /**
@@ -732,12 +741,13 @@ public class ExpandableListView extends ListView {
     * its type). Can return {@link #PACKED_POSITION_VALUE_NULL} if no selection.
     * 
     * @return A packed position containing the currently selected group or
     *         child's position and type. #PACKED_POSITION_VALUE_NULL if no selection.
     *         child's position and type. #PACKED_POSITION_VALUE_NULL if no selection
     *         or if selection is on a header or a footer item.
     */
    public long getSelectedPosition() {
        final int selectedPos = getSelectedItemPosition();
        if (selectedPos == -1) return PACKED_POSITION_VALUE_NULL;

        // The case where there is no selection (selectedPos == -1) is also handled here.
        return getExpandableListPosition(selectedPos);
    }
    
@@ -921,13 +931,12 @@ public class ExpandableListView extends ListView {

    @Override
    ContextMenuInfo createContextMenuInfo(View view, int flatListPosition, long id) {
        // Adjust for and handle for header views
        final int adjustedPosition = flatListPosition - getHeaderViewsCount();
        if (adjustedPosition < 0) {
            // Return normal info for header view context menus
        if (isHeaderOrFooterPosition(flatListPosition)) {
            // Return normal info for header/footer view context menus
            return new AdapterContextMenuInfo(view, flatListPosition, id);
        }

        final int adjustedPosition = flatListPosition - getHeaderViewsCount();
        PositionMetadata pm = mConnector.getUnflattenedPos(adjustedPosition);
        ExpandableListPosition pos = pm.position;
        pm.recycle();
+2 −2
Original line number Diff line number Diff line
@@ -123,9 +123,9 @@ public class ExpandableListBasicTest extends ActivityInstrumentationTestCase2<Ex
    }

    @MediumTest
    public void testGroupChildPositions() {
    public void testContextMenus() {
        ExpandableListTester tester = new ExpandableListTester(mExpandableListView, this);
        tester.testGroupAndChildPositions();
        tester.testContextMenus();
    }

    @MediumTest
+9 −9
Original line number Diff line number Diff line
@@ -56,11 +56,11 @@ public class ExpandableListTester {
        mInstrumentation.waitForIdleSync();
        mActivityInstrumentation.sendKeys(KeyEvent.KEYCODE_DPAD_CENTER);
        mActivityInstrumentation.getInstrumentation().waitForIdleSync();
        Assert.assertTrue("Group did not expand " + groupIndex, mExpandableListView
                .isGroupExpanded(groupIndex));
        Assert.assertTrue("Group did not expand " + groupIndex, 
                mExpandableListView.isGroupExpanded(groupIndex));
    }

    void testGroupAndChildPositions() {
    void testContextMenus() {
        // Add a position tester ContextMenu listener to the ExpandableListView
        PositionTesterContextMenuListener menuListener = new PositionTesterContextMenuListener();
        mExpandableListView.setOnCreateContextMenuListener(menuListener);
@@ -109,7 +109,7 @@ public class ExpandableListTester {
        // Scrolling on footer elements should trigger an AdapterContextMenu
        for (int i=0; i<mExpandableListView.getFooterViewsCount(); i++) {
            // Check group index in context menu
            menuListener.expectAdapterContextMenu(i+1);
            menuListener.expectAdapterContextMenu(index);
            // Make sure the group is visible so that getChild finds it
            mListUtil.arrowScrollToSelectedPosition(index);
            View footerChild = mExpandableListView.getChildAt(index
@@ -136,8 +136,8 @@ public class ExpandableListTester {

        for (int i=0; i<headerCount; i++) {
            Assert.assertEquals("Non NULL position for header item",
                    ExpandableListView.PACKED_POSITION_TYPE_NULL, mExpandableListView
                    .getExpandableListPosition(i));
                    ExpandableListView.PACKED_POSITION_VALUE_NULL,
                    mExpandableListView.getExpandableListPosition(i));
        }

        // Test all (non expanded) groups
@@ -156,7 +156,7 @@ public class ExpandableListTester {

        for (int i=0; i<mExpandableListView.getFooterViewsCount(); i++) {
            Assert.assertEquals("Non NULL position for header item",
                    ExpandableListView.PACKED_POSITION_TYPE_NULL,
                    ExpandableListView.PACKED_POSITION_VALUE_NULL,
                    mExpandableListView.getExpandableListPosition(headerCount + groupCount + i));
        }
    }
@@ -190,7 +190,7 @@ public class ExpandableListTester {
        for (int i=0; i<mExpandableListView.getHeaderViewsCount(); i++) {
            mListUtil.arrowScrollToSelectedPosition(index);
            Assert.assertEquals("Header item is selected",
                    ExpandableListView.PACKED_POSITION_TYPE_NULL,
                    ExpandableListView.PACKED_POSITION_VALUE_NULL,
                    mExpandableListView.getSelectedPosition());
            index++;
        }
@@ -209,7 +209,7 @@ public class ExpandableListTester {
        for (int i=0; i<mExpandableListView.getFooterViewsCount(); i++) {
            mListUtil.arrowScrollToSelectedPosition(index);
            Assert.assertEquals("Footer item is selected",
                    ExpandableListView.PACKED_POSITION_TYPE_NULL,
                    ExpandableListView.PACKED_POSITION_VALUE_NULL,
                    mExpandableListView.getSelectedPosition());
            index++;
        }
+2 −2
Original line number Diff line number Diff line
@@ -63,9 +63,9 @@ public class ExpandableListWithHeadersTest extends
    }

    @MediumTest
    public void testGroupChildPositions() {
    public void testContextMenus() {
        ExpandableListTester tester = new ExpandableListTester(mExpandableListView, this);
        tester.testGroupAndChildPositions();
        tester.testContextMenus();
    }

    @MediumTest