user1689757
user1689757

Reputation: 475

Child elements in ListViewAnimations expandable list don't expand to fit content

I am using nhaarman's ListViewAnimations (http://nhaarman.github.io/ListViewAnimations/) to implement an expandable list view where each row has one parent and one child view that expands when a user clicks on the parent view. The child row has a couple of text views with variable height. However when the listview renders and I click to expand the child view it does not expand enough to display all the content.

This is what my child view layout looks like:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/list_item_child"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/white"
    android:paddingBottom="20dp"
    android:paddingLeft="20dp"
    android:paddingRight="20dp" >

    <ImageView
        android:id="@+id/listItemArrow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:contentDescription="@null" />

    <RelativeLayout
        android:id="@+id/listItemInfoContainer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/listItemTitle"
            style="@style/ScentTitle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp" />

        <TextView
            android:id="@+id/listItemDetails"
            style="@style/itemDetails"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/listItemTitle" />

        <Button
            android:id="@+id/listItemShopNowButton"
            style="@style/itemButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/listItemDetails"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="10dp"
            android:paddingLeft="30dp"
            android:paddingRight="30dp"
            android:text="@string/shop_now" />

    </RelativeLayout>

</RelativeLayout>

I initially had the layout_height of the text views set to fill_parent, but changed them over to wrap_content. That did not seem to fix it either. Has anyone run into similar issues.

Screenshot

Upvotes: 1

Views: 712

Answers (1)

jesobremonte
jesobremonte

Reputation: 3396

You can try this, you'll have to modify ExpandableListItemAdapter.java in the ListViewAnimations library.

Replace the animateExpanding() method with this so that it wraps the entire expanding layout:

    public static void animateExpanding(final View view) {

        /*
         * Set this view to invisible so that the size can be measured. If it's set to View.VISIBLE here
         * the maximized layout appears to flicker in before animating.
         */
        view.setVisibility(View.INVISIBLE);

        view.post(new Runnable() {

            @Override
            public void run() {

                /*
                 * Do view.getWidth in a view.post thread or else the first view.getWidth will always be 0.
                 * 
                 * The width MeasureSpec cannot be set to UNSPECIFIED here or else the height of wrapped
                 * textViews gets calculated as if it's a single infinitely long line.
                 */
                int widthSpec = View.MeasureSpec.makeMeasureSpec(view.getWidth(), View.MeasureSpec.AT_MOST);
                int heightSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
                view.measure(widthSpec, heightSpec);

                ValueAnimator animator = createHeightAnimator(view, 0, view.getMeasuredHeight());
                animator.start();
                view.setVisibility(View.VISIBLE);
            }
        });

    }

The problem originally lies with textViews with wrapping text being calculated as if it's a single line. If your layout didn't have that wrapping text the library would otherwise be fine.

Check up on https://github.com/nhaarman/ListViewAnimations/issues/61 in case Niek Haarman updates the fix to a different or more elegant solution.

Upvotes: 1

Related Questions