【问题标题】:collapse and expand animation issue android折叠和展开动画问题android
【发布时间】:2017-03-09 04:50:33
【问题描述】:

当我第一次单击此动画时,该动画会突然折叠并自行展开,然后单击时一切正常。问题是为什么这个动画第一次扩展自己。这里有专家吗?

这是我的 xml 代码

<TextView
        android:drawableRight="@drawable/ic_arrow_down"
        android:background="#FFF12222"
        android:textColor="#060606"
        android:textSize="20sp"
        android:text="Required Field"
        android:id="@+id/section_required_field"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <LinearLayout
        android:orientation="vertical"
        android:id="@+id/layout_required_fields"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:layout_marginTop="10dp"
            android:id="@+id/tile_head"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Title"
            android:textSize="16sp"
            android:textColor="#060606"/>

        <EditText
            android:id="@+id/title"
            android:layout_width="270sp"
            android:layout_height="wrap_content"
            android:text="abcd"
            android:layout_marginTop="2dp"
            android:background="@drawable/rounded_edittext"
            android:textSize="15sp"/>

        <TextView
            android:id="@+id/description_head"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="20sp"
            android:text="Description"
            android:textSize="15sp"
            android:textColor="#060606"/>

        <EditText
            android:id="@+id/video_description"
            android:layout_width="270sp"
            android:layout_height="wrap_content"
            android:layout_marginTop="2sp"
            android:text="abcd"
            android:background="@drawable/rounded_edittext"
            android:textSize="15sp"/>

        <TextView
            android:id="@+id/category_head"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="20sp"
            android:text="Category"
            android:textSize="15sp"
            android:textColor="#060606"/>

        <TextView
            android:id="@+id/video_category"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="2sp"
            android:text="abcd"
            android:textSize="15sp"
            android:textColor="#060606"/>

        <TextView
            android:id="@+id/tags_head"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="20sp"
            android:text="Tags"
            android:textSize="15sp"
            android:textColor="#060606"/>

        <EditText
            android:id="@+id/tags"
            android:layout_width="270sp"
            android:layout_height="wrap_content"
            android:layout_marginTop="2sp"
            android:text="abcd"
            android:textSize="15sp"
            android:background="@drawable/rounded_edittext"
            android:textColor="#060606"/>

    </LinearLayout>

这是我的动画 java 类

public class AnimationUtils {

    public static void expand(final View v) {
        v.measure(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        final int targetHeight = v.getMeasuredHeight();

        // Older versions of android (pre API 21) cancel animations for views with a height of 0.
        v.getLayoutParams().height = 1;
        v.setVisibility(View.VISIBLE);
        Animation a = new Animation()
        {
            @Override
            protected void applyTransformation(float interpolatedTime, Transformation t) {
                v.getLayoutParams().height = interpolatedTime == 1
                        ? ViewGroup.LayoutParams.WRAP_CONTENT
                        : (int)(targetHeight * interpolatedTime);
                v.requestLayout();
            }

            @Override
            public boolean willChangeBounds() {
                return true;
            }
        };

        // 1dp/ms
        a.setDuration((int)(targetHeight / v.getContext().getResources().getDisplayMetrics().density));
        v.startAnimation(a);
    }

    public static void collapse(final View v) {
        final int initialHeight = v.getMeasuredHeight();

        Animation a = new Animation()
        {
            @Override
            protected void applyTransformation(float interpolatedTime, Transformation t) {
                if(interpolatedTime == 1){
                    v.setVisibility(View.GONE);
                }else{
                    v.getLayoutParams().height = initialHeight - (int)(initialHeight * interpolatedTime);
                    v.requestLayout();
                }
            }

            @Override
            public boolean willChangeBounds() {
                return true;
            }
        };

        // 1dp/ms
        a.setDuration((int)(initialHeight / v.getContext().getResources().getDisplayMetrics().density));
        v.startAnimation(a);
    }
}

这是我的 java 类代码

tvRequiredField = (TextView) findViewById(R.id.section_required_field);
        requiredFieldsLayout = (LinearLayout) findViewById(R.id.layout_required_fields);
        tvRequiredField.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (v.isSelected()) {
                    AnimationUtils.collapse(requiredFieldsLayout);
                    v.setSelected(false);
                }
                else {
                    AnimationUtils.expand(requiredFieldsLayout);
                    v.setSelected(true);
                }
            }
        });

【问题讨论】:

    标签: android animation collapse expand


    【解决方案1】:

    我错了

    tvRequiredField = (TextView) findViewById(R.id.section_required_field);
            requiredFieldsLayout = (LinearLayout) findViewById(R.id.layout_required_fields);
            tvRequiredField.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (v.isSelected()) {//this should be expand instead of collapse
                        AnimationUtils.collapse(requiredFieldsLayout);
                        v.setSelected(false);
                    }
                    else {//this should be collapse instead of expand 
                        AnimationUtils.expand(requiredFieldsLayout);
                        v.setSelected(true);
                    }
                }
            });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-03
      • 2011-06-29
      • 2017-02-21
      相关资源
      最近更新 更多