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>

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章