点击查看详情显示更多布局

布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="#eeff00"
        android:text="这是原先显示的一部分布局"
        android:gravity="center"
        />

    <LinearLayout
        android:id="@+id/layer2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:background="#60ff0000">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="66dp"
            android:gravity="center"
            android:text="详情内容"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="right"
        android:orientation="horizontal">
        <TextView
            android:id="@+id/look_detail"
            android:layout_width="wrap_content"
            android:layout_height="30dp"
            android:gravity="center"
            android:textColor="#0083ce"
            android:clickable="true"
            android:textSize="12sp"
            android:text="查看详情"/>
        <ImageView
            android:id="@+id/icon1"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:src="@mipmap/ic_launcher"/>
    </LinearLayout>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:background="#ff0000"
        />
</LinearLayout>

在Activity中

首先获得控件
private void initView() {
    lookDetail = (TextView) findViewById(R.id.look_detail);  //这是查看详情文字
    layer2 = (LinearLayout) findViewById(R.id.layer2);       //这是隐藏的大布局
    icon1 = (ImageView) findViewById(R.id.icon1);            //这是查看详情后面的三角图片,会给它设置旋转动画
}

//主要实现代码

/**
 * 展开收起 执行动画
 */
private void initShowHide() {
    //布局完成   使控件高度为0,相当于隐藏
    layer2.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            //移除所有监听
            layer2.getViewTreeObserver().removeGlobalOnLayoutListener(this);
            mLayoutHeight = layer2.getHeight();
            System.out.println("得到的高度:" + mLayoutHeight);
            //隐藏当前控件
            layer2.setPadding(0,-mLayoutHeight,0,0);
        }
    });

    //点击,开始执行动画
    lookDetail.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            ValueAnimator valueAnimator = new ValueAnimator();
            if (isOpen){
                valueAnimator.setIntValues(0, -mLayoutHeight);
                lookDetail.setText("查看详情");
            }else {
                valueAnimator.setIntValues(-mLayoutHeight, 0);
                lookDetail.setText("收起");
            }
            //设置监听的值
            valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animator) {
                    int value = (int) animator.getAnimatedValue();
                    layer2.setPadding(0,value,0,0);
                }
            });
            //动画执行中监听
            valueAnimator.addListener(new Animator.AnimatorListener() {
                @Override
                public void onAnimationStart(Animator animator) {
                    //动画开始,不能点击
                    lookDetail.setClickable(false);
                }

                @Override
                public void onAnimationEnd(Animator animator) {
                    lookDetail.setClickable(true);
                }

                @Override
                public void onAnimationCancel(Animator animator) {

                }

                @Override
                public void onAnimationRepeat(Animator animator) {

                }
            });

            valueAnimator.setDuration(500);
            valueAnimator.start();
            //状态更改
            isOpen = !isOpen;

            //进行旋转
            ViewCompat.animate(icon1).rotationBy(180f).setDuration(500).start();
        }
    });
}

这就实现好了,就是改变控件的高度来实现显示和隐藏的动画

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值