【问题标题】:Child elements in ListViewAnimations expandable list don't expand to fit contentListViewAnimations 可扩展列表中的子元素不会扩展以适应内容
【发布时间】:2013-10-25 03:01:22
【问题描述】:

我正在使用 nhaarman 的 ListViewAnimations (http://nhaarman.github.io/ListViewAnimations/) 来实现一个可展开的列表视图,其中每一行都有一个父视图和一个子视图,当用户单击父视图时会展开。子行有几个高度可变的文本视图。但是,当列表视图呈现并且我单击以展开子视图时,它的展开不足以显示所有内容。

这是我的子视图布局的样子:

<?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>

我最初将文本视图的 layout_height 设置为 fill_parent,但将它们更改为 wrap_content。这似乎也没有解决它。有没有人遇到过类似的问题。

【问题讨论】:

    标签: android android-listview expandablelistview


    【解决方案1】:

    你可以试试这个,你必须修改 ListViewAnimations 库中的 ExpandableListItemAdapter.java。

    用 this 替换 animateExpanding() 方法,使其包裹整个扩展布局:

        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);
                }
            });
    
        }
    

    问题最初在于带有环绕文本的 textViews 被计算为好像它是单行一样。如果你的布局没有环绕文本,那么库就可以了。

    检查 https://github.com/nhaarman/ListViewAnimations/issues/61 以防 Niek Haarman 将修复更新为不同或更优雅的解决方案。

    【讨论】:

    • 这行得通。但是这个解决方案让我的动画闪烁。
    • 如果您从标签 2.6.0 签出,则该错误已得到修复。所以你将不再有这个问题。
    猜你喜欢
    • 2015-09-19
    • 1970-01-01
    • 1970-01-01
    • 2020-02-12
    • 2017-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多