android 可折叠的控件

最近新学习了一个可折叠控件,在控件中可添加布局;

ExpandView.java

public class ExpandView extends FrameLayout implements View.OnClickListener {
    private Animation mExpandAnimation;
    private Animation mCollapseAnimation;
    private boolean mIsExpand;
    private int layoutId;

    public ExpandView(Context context) {
        this(context, null);
    }

    public ExpandView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ExpandView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

    }

    public void setLayout(int layout) {
        this.layoutId = layout;
        initExpandView();
    }

    private void initExpandView() {
        LayoutInflater.from(getContext()).inflate(layoutId, this, true);
//        mExpandAnimation = AnimationUtils.loadAnimation(getContext(), R.anim.expand);
        mExpandAnimation = AnimationUtils.loadAnimation(getContext(), R.anim.fade_appear);
        mExpandAnimation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                setVisibility(View.VISIBLE);
            }
        });

        mCollapseAnimation = AnimationUtils.loadAnimation(getContext(), R.anim.fade_disappear);
        mCollapseAnimation.setAnimationListener(new Animation.AnimationListener() {

            @Override
            public void onAnimationStart(Animation animation) {
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {

                setVisibility(View.GONE);
            }
        });

    }

    public void collapse() {
        if (mIsExpand) {
            mIsExpand = false;
            clearAnimation();
            startAnimation(mCollapseAnimation);
        }
    }

    public void expand() {
        if (!mIsExpand) {
            mIsExpand = true;
            clearAnimation();
            startAnimation(mExpandAnimation);
        }
    }

    public boolean isExpand() {
        return mIsExpand;
    }

    public void setContentView() {
        View view = null;
        view = LayoutInflater.from(getContext()).inflate(R.layout.activity_field_record, null);
        removeAllViews();
        addView(view);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

        }
    }
}

调用:

    /**
     * 初始化调用
     */
    public void ExpandView() {
        expandView.setContentView();
        lin_tv.setClickable(true);
        lin_tv.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (expandView.isExpand()) {
                    expandView.collapse();
                    textview_title.setText("点击向下展开");
                    img_shrink.setImageDrawable(getResources().getDrawable(R.drawable.icon_down));
                } else {
                    expandView.expand();
                    textview_title.setText("点击向上收缩");
                    img_shrink.setImageDrawable(getResources().getDrawable(R.drawable.icon_up));
                }
            }
        });
        
    }

布局文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context="com.foldtext.MainActivity">
        <LinearLayout
            android:id="@+id/lin_tv"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <TextView
                android:id="@+id/hello1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:maxLines="2"
                android:padding="10dp"
                android:text="标题" />
            <TextView
                android:id="@+id/textview_title"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="center"
                android:text="点击向下展开" />
            <ImageView
                android:id="@+id/img_shrink"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:padding="10dp"
                android:src="@drawable/icon_down" />
        </LinearLayout>
        <com.nmpa.nmpaapp.modules.unlicensed.ExpandView
            android:id="@+id/ex_expandview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:visibility="gone" />
    </LinearLayout>

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值