Android - 通过点击另一个布局实现布局动画

3
我有两个RelativeLayout,每个中都包含一个TextView。
基本上,顶部布局作为“按钮”使用。当单击时,另一个布局将播放其动画(向下展开)。
在这种情况下,我应该将setLayoutAnimationListener()添加到哪个布局中,以便在onAnimationStart()方法中无法单击顶部布局,并且可以在onAnimationEnd()方法中单击它?
以下是我所指的布局:
<RelativeLayout
            android:id="@+id/activity_signup_step_one_dropdownTitleWrapper"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/activity_signup_step_one_orangEmailWrapper"
            android:layout_marginTop="20dp"
            android:layout_marginBottom="20dp">

            <TextView
                android:id="@+id/activity_signup_step_one_dropdownTitle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/stepOneTitle"/>

        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/activity_signup_step_one_dropdownTextWrapper"
            android:gravity="center_vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/activity_signup_step_one_dropdownTitleWrapper">

            <TextView
                android:id="@+id/activity_signup_step_one_dropdownText"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/stepOneMessage"/>


        </RelativeLayout>

Java 类
@Override
    public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_signup_step_one);
...

RelativeLayout dropDownTitle = (RelativeLayout)findViewById(R.id.activity_signup_step_one_dropdownTitleWrapper);
        RelativeLayout dropDownMessage = (RelativeLayout)findViewById(R.id.activity_signup_step_one_dropdownTextWrapper);
dropDownMessage.setVisibility(View.GONE);
        dropDownTitle.setOnClickListener(translateHandler);

dropDownMessage.setLayoutAnimationListener(new Animation.AnimationListener() {
            //RelativeLayout touchDisabler = (RelativeLayout) findViewById(R.id.activity_signup_step_one_dropdownTitleWrapper_filler);
            RelativeLayout dropDownTitle = (RelativeLayout) findViewById(R.id.activity_signup_step_one_dropdownTitleWrapper);

            @Override
            public void onAnimationStart(Animation animation) {
                //touchDisabler.setClickable(true);
                Log.d("onStart", "Start");
                dropDownTitle.setClickable(false);
                //getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,
                //WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                //touchDisabler.setClickable(false);
                Log.d("onEnd", "End");
                dropDownTitle.setClickable(true);
                //getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
                //touchDisabler.setClickable(true);
            }
        });

int height;

    View.OnClickListener translateHandler = new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            RelativeLayout dropDownMessage = (RelativeLayout)findViewById(R.id.activity_signup_step_one_dropdownTextWrapper);
            TextView testText = (TextView)findViewById(R.id.activity_signup_step_one_dropdownText);
            if(dropDownMessage.getVisibility() == View.VISIBLE){
                MyCustomAnimation a = new MyCustomAnimation(dropDownMessage, 350, MyCustomAnimation.COLLAPSE);
                height = a.getHeight();
                testText.setText(getResources().getString(R.string.stepOneMessage));
                dropDownMessage.startAnimation(a);
            }else{
                MyCustomAnimation a = new MyCustomAnimation(dropDownMessage, 350, MyCustomAnimation.EXPAND);
                a.setHeight(height);
                testText.setText(getResources().getString(R.string.stepOneMessage));
                dropDownMessage.startAnimation(a);
            }
        }
    };

这是一个可能有用的动画类:

Animation class


public class MyCustomAnimation extends Animation {

    public final static int COLLAPSE = 1;
    public final static int EXPAND = 0;

    private View mView;
    private int mEndHeight;
    private int mType;
    private RelativeLayout.LayoutParams mLayoutParams;

    public MyCustomAnimation(View view, int duration, int type) {

        setDuration(duration);
        mView = view;
        mEndHeight = mView.getHeight();
        mLayoutParams = ((RelativeLayout.LayoutParams) view.getLayoutParams());
        mType = type;
        if(mType == EXPAND) {
            mLayoutParams.height = 0;
        } else {
            mLayoutParams.height = LayoutParams.WRAP_CONTENT;
        }
        view.setVisibility(View.VISIBLE);
    }

    public int getHeight(){
        return mView.getHeight();
    }

    public void setHeight(int height){
        mEndHeight = height;
    }

    @Override
    protected void applyTransformation(float interpolatedTime, Transformation t) {

        super.applyTransformation(interpolatedTime, t);
        if (interpolatedTime < 1.0f) {
            if(mType == EXPAND) {
                mLayoutParams.height =  (int)(mEndHeight * interpolatedTime);
            } else {
                mLayoutParams.height = (int) (mEndHeight * (1 - interpolatedTime));
            }
            mView.requestLayout();
        } else {
            if(mType == EXPAND) {
                mLayoutParams.height = LayoutParams.WRAP_CONTENT;
                mView.requestLayout();
            }else{
                mView.setVisibility(View.GONE);
            }
        }
    }
}

完成了,之前在另一个Activity上工作,抱歉。 - Kevin Murvie
是的,即使动画正在进行中,我仍然可以点击顶部的“RelativeLayout”.. 我已经在开始时将其设置为setClickable(false) - Kevin Murvie
@0X0nosugar 它被识别为布局动画事件 :( 我不能选择除布局动画事件之外的任何其他动画事件侦听器.. 我使用了一个我从Stackoverflow上找到的自定义动画.. 我将发布它 - Kevin Murvie
1个回答

0
final RelativeLayout dropDownTitle = (RelativeLayout) findViewById(R.id.activity_signup_step_one_dropdownTitleWrapper);

dropDownMessage.setLayoutAnimationListener(new Animation.AnimationListener() {
            //RelativeLayout touchDisabler = (RelativeLayout) findViewById(R.id.activity_signup_step_one_dropdownTitleWrapper_filler);

            @Override
            public void onAnimationStart(Animation animation) {
                //touchDisabler.setClickable(true);
                Log.d("onStart", "Start");
                dropDownTitle.setClickable(false);
                dropDownMessage.setVisibility(View.GONE);
                //getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,
                //WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                //touchDisabler.setClickable(false);
                Log.d("onEnd", "End");
                dropDownTitle.setClickable(true);


                //getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
                //touchDisabler.setClickable(true);
            }
        });

尝试在匿名内部类(AnimationListener)之外实例化您的RelativeLayout,并当然将其声明为final

1
仍然发生着...我仍然无法使dropDownTitlefalse,这可能是一个好消息,Android Monitor在开始和结束时都不显示Log.d - Kevin Murvie
请查看我的更新答案 - 请尝试将您的setVisibility(View.GONE)放在onAnimationStart中而不是在AnimationListener之前。 - Ludwig S
问题是我需要在动画开始之前将其“消失”:(只有当用户希望时,信息才会向下滑动,如果不是,则包含信息的RelativeLayout必须为“GONE”:( - Kevin Murvie

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接