ExpandableListView child divider drawing process

I am trying to edit the way a child divider is drawn in the ExpandableListView class for Android. My reasons for this objective are as follows: I would like to shorten child divider length and height. As of now, the child drawable file looks like this

<?xml version="1.0" encoding="utf-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
    android:insetLeft="@dimen/main_activity_nav_drawer_child_margin_left"
    android:insetTop="2dp">

    <shape android:shape="rectangle" >

        <gradient
            android:startColor="@color/colorSecondary"
            android:endColor="@color/colorPrimary"/>

    </shape>

</inset>

I use the inset tag to shorten the top and left sides of the divider. Unfortunately, whenever I expand a parent view, the divider immediately underneath it becomes a child divider. I would like this particular divider to remain a parent divider. I have looked at the source code for ExpandableListView and found this snippet for drawing a child divider:

// If this item is a child, or it is a non-empty group that is expanded
if ((pos.position.type == ExpandableListPosition.CHILD) || (pos.isExpanded() &&
    pos.groupMetadata.lastChildFlPos != pos.groupMetadata.flPos)) {

    // These are the cases where we draw the child divider
    final Drawable divider = mChildDivider;
    divider.setBounds(bounds);
    divider.draw(canvas);
    pos.recycle();
    return;
}

It is clear that to achieve my goal, I need to remove the code inside of the if condition that is to the right of the logical or operator along with the operator itself. All of that being said, I have a few ideas of where to go from here but I am unsure how to proceed. The obvious thing to do would be to subclass ExpandableListView and override the method where this code is run, however, I am unable to do this since the method (named drawDivider) has the default visibility modifier. I don’t think that I am able to change the bounds on which this view is drawn since I also don’t have access to any of these members either. Does anyone have any pointers, tips, or solution for this problem? If any of my assumptions are incorrect, I would very much appreciate the guidance. I will answer any questions if anything is unclear about this post.

Source: Android Questions

LEAVE A COMMENT