如何在Android中为其子项的可扩展列表设置动画?

时间:2017-08-17 11:00:04

标签: android animation

我希望在列表上下列表显示良好的外观并且不会非常快速地向上和向下列出时,为我的可扩展列表设置动画。

2 个答案:

答案 0 :(得分:0)

只需将此方法用于子视图。

private void animateView(final View target, final int type) {
        Animation anim = new ExpandCollapseAnimation(target,type);
        anim.setDuration(getAnimationDuration());
        anim.setAnimationListener(new AnimationListener() {

        @Override
        public void onAnimationStart(Animation animation) {}

        @Override
        public void onAnimationRepeat(Animation animation) {}

        @Override
        public void onAnimationEnd(Animation animation) {
            if (type == ExpandCollapseAnimation.EXPAND) {
                if (parent instanceof ListView) {
                    ListView listView = (ListView) parent;
                    int movement = target.getBottom();

                    Rect r = new Rect();
                    boolean visible = target.getGlobalVisibleRect(r);
                    Rect r2 = new Rect();
                    listView.getGlobalVisibleRect(r2);

                    if (!visible) {
                        listView.smoothScrollBy(movement, getAnimationDuration());
                    } else {
                        if (r2.bottom == r.bottom) {
                            listView.smoothScrollBy(movement,getAnimationDuration());
                        }
                      }
                }
            }
        }
    });
    target.startAnimation(anim);
}

答案 1 :(得分:0)

在build.gradle中添加依赖项

{"message":"Validation failed because [{reason=INVALID_VALUE_FOR_FIELD, field=reference, batchIndex=0, type=INVALID_VALUE, message=/reference cannot be set to urn:li:share:6316476667095322625, parameters={field=/reference, value=urn:li:share:6316476667095322625, key=}}]","status":400}

创建xml文件

buildscript {
    repositories {
        jcenter()
    }
}

dependencies {
    compile 'com.github.aakira:expandable-layout:1.6.0@aar'
}

添加java代码

<com.github.aakira.expandablelayout.ExpandableRelativeLayout
    android:id="@+id/expandableLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:ael_expanded="false"
    app:ael_duration="500"
    app:ael_interpolator="bounce"
    app:ael_orientation="vertical">

    <TextView
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="sample" />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/text"
        android:text="sample2" />
</com.github.aakira.expandablelayout.ExpandableRelativeLayout>