What's the deal with ExpandableListViews?

2019-08-29 05:10发布

问题:

So I want to create an Expandable List View and every place I look, it seems like they are programming in their own functionality for the Expandable List View themselves.

Do you really have to do that or is there a base functionality that doesn't require any programming?

回答1:

So I have used this library to achieve the expansion and collapse behavior and it works like a charm.

<com.github.florent37.expansionpanel.ExpansionHeader
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    app:expansion_headerIndicator="@id/headerIndicator"
    app:expansion_layout="@id/expansionLayout"
    app:expansion_toggleOnClick="true">

    <!-- HEADER -->

    ...
    <!-- HEADER INDICATOR -->
    <ImageView
           android:adjustViewBounds="true"
           android:id="@+id/headerIndicator"
           android:layout_gravity="center_vertical|right"
           android:layout_height="wrap_content"
           android:layout_marginLeft="16dp"
           android:layout_width="wrap_content"
           app:srcCompat="@drawable/ic_expansion_header_indicator_grey_24dp" />


</com.github.florent37.expansionpanel.ExpansionHeader>

<com.github.florent37.expansionpanel.ExpansionLayout
    android:id="@+id/expansionLayout"
    android:layout_height="wrap_content"
    android:layout_width="match_parent">

    <!-- CONTENT -->

</com.github.florent37.expansionpanel.ExpansionLayout>

Attach a listener in Activity like this

ExpansionLayout expansionLayout = findViewById(...);
expansionLayout.addListener(new ExpansionLayout.Listener() {
    @Override
    public void onExpansionChanged(ExpansionLayout expansionLayout, boolean expanded) {

    }
});