【问题标题】:Slide Up/Down animation not working correct in second Time向上/向下滑动动画第二次无法正常工作
【发布时间】:2017-11-25 21:11:48
【问题描述】:

我在顶部位置有一个布局,这个视图是第一次隐藏。我会以编程方式进行上下滑动动画。我编写了代码,这个代码只有第一次才能完美运行 在这里

public void cutomTabDropDownAnimation(LinearLayout view, boolean isSlideDown) {
    TranslateAnimation animate;
    if (isSlideDown) {
        animate = new TranslateAnimation(
                0,
                0,
                0,
                view.getHeight()); // toYDelta
    } else {
        animate = new TranslateAnimation(
                0,
                0,
                view.getHeight(),                 
                0); // toYDelta
    }


    animate.setDuration(300);
    animate.setFillAfter(true);
    animate.setInterpolator(new BounceInterpolator());
    view.setAnimation(animate);
    if (isSlideDown)
        view.setVisibility(View.VISIBLE);
    else
        view.setVisibility(View.GONE);


}

第二次显示第一个视图,然后开始动画,但不正确,视图隐藏。 这里是一个xml文件源

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"

android:id="@+id/myCoordinator"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f2f2f2">



<LinearLayout
    android:id="@+id/customTabLayout"
    android:layout_width="match_parent"
    android:layout_height="49dp"
    android:background="#ffffff"
    android:orientation="vertical"
    android:visibility="gone">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="48dp">


        <View
            android:id="@+id/view"
            android:layout_width="1dp"
            android:layout_height="match_parent"
            android:layout_centerInParent="true"
            android:background="@color/colorPrimary"

            />

        <TextView
            android:id="@+id/firstTab"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignEnd="@+id/view"
            android:layout_alignParentTop="true"
            android:layout_alignRight="@+id/view"
            android:gravity="center"
            android:text="@string/u_four_tab_1"
            android:textColor="#405a97"
            android:textSize="16dp" />

        <TextView
            android:id="@+id/secondTab"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignLeft="@+id/view"
            android:layout_alignParentTop="true"
            android:layout_alignStart="@+id/view"
            android:gravity="center"
            android:text="@string/u_four_tab_2"
            android:textColor="#d4d4d4"
            android:textSize="16dp"

            />

    </RelativeLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#801d7aed" />
</LinearLayout>

<android.support.v4.widget.NestedScrollView
    android:id="@+id/nestedScrollview"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/customTabLayout">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clipToPadding="false" />
</android.support.v4.widget.NestedScrollView>

<LinearLayout
    android:id="@+id/empty_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    android:layout_marginTop="40dp"
    android:gravity="top|center"
    android:orientation="vertical"
    android:visibility="gone">

    <TextView
        android:id="@+id/empty_layout_txt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="@string/u_empty_package"
        android:textColor="#808080"
        android:textSize="16dp" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="46dp"
        android:background="@mipmap/package_grey" />
</LinearLayout>

我的意思是xml文件中的customTabLayout线性布局。我该如何解决这个问题?

【问题讨论】:

    标签: android android-animation android-linearlayout android-relativelayout


    【解决方案1】:

    我认为导致奇怪行为的原因是您的这部分代码:

    if (isSlideDown)
        view.setVisibility(View.VISIBLE);
    else
        view.setVisibility(View.GONE);
    

    您需要在动画结束之后或开始之前设置视图的可见性(取决于上下文)

        animate.setAnimationListener(new AnimationListener() {    
        @Override
        public void onAnimationStart(Animation animation) {  
            // TODO Auto-generated method stub
    if (isSlideDown){
            view.setVisibility(View.VISIBLE);
    }
        }
    
        @Override
        public void onAnimationRepeat(Animation animation) {
            // TODO Auto-generated method stub
        }
    
        @Override
        public void onAnimationEnd(Animation animation) {
    
        if (!isSlideDown){
            view.setVisibility(View.GONE);
    }
    
        }
    });
    

    【讨论】:

    • 我改变了,但没有别的@Jaja
    【解决方案2】:

    更改为 INVISIBLE 而不是 GONE。

    试试这个:

     if (isSlideDown)
        view.setVisibility(View.VISIBLE);
    else
        view.setVisibility(View.INVISIBLE);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-24
      • 1970-01-01
      • 2013-10-31
      • 1970-01-01
      相关资源
      最近更新 更多