If you write a widget that shows hidden content when you click view with animation sliding down, view hidden content

Source: Internet
Author: User
Tags checkstyle

If you write a widget that shows hidden content when you click view with animation sliding down, view hidden content

The principle is to get the height of the hidden content in onMeasure. When you click this view, you can increase the height of the hidden view from 0 to the measureHeight of the View obtained by onMeasure.


Write as follows:

Public class extends LinearLayout {private Context mContext; private LinearLayout mHandleView; private RelativeLayout mContentView; private ImageView succeeded; int updated = 0; int mTitleHeight = 0; private boolean isExpand; private Animation animationDown; private Animation animationUp; public ExpandableLayout (Context context, AttributeSet attrs) {super (context, attrs); this. mCont Ext = context ;}@ Overrideprotected void onMeasure (int widthMeasureSpec, int heightMeasureSpec) {if (this. mContentHeight = 0) {this. mContentView. measure (widthMeasureSpec, 0); this. mContentHeight = this. mContentView. getMeasuredHeight ();} if (this. mTitleHeight = 0) {this. mHandleView. measure (widthMeasureSpec, 0); this. mTitleHeight = this. mHandleView. getMeasuredHeight ();} super. onMeasure (widthMeasureSpec, HeightMeasureSpec);} @ Overrideprotected void onFinishInflate () {super. onFinishInflate (); this. mHandleView = (LinearLayout) this. findViewById (R. id. collapse_value); this. mContentView = (RelativeLayout) this. findViewById (R. id. expand_value); this. mIconExpand = (ImageView) this. findViewById (R. id. icon_value); this. mHandleView. setOnClickListener (new ExpandListener (); this. mContentView. setOnClickListener (new ExpandListener (); mContentView. setVisibility (View. GONE);} private class ExpandListener implements View. onClickListener {@ Overridepublic final void onClick (View paramView) {// clearAnimation is the view method clearAnimation (); if (! IsExpand) {if (animationDown = null) {animationDown = new DropDownAnim (mContentView, mContentHeight, true); animationDown. setDuration (200); // suppress checkstyle} startAnimation (animationDown); mContentView. startAnimation (AnimationUtils. loadAnimation (mContext, R. anim. animalpha); mIconExpand. setImageResource (R. drawable. update_detail_up); isExpand = true;} else {isExpand = false; if (animationUp = null) {animationUp = new DropDownAnim (mContentView, mContentHeight, false); animationUp. setDuration (200); // suppress checkstyle} startAnimation (animationUp); mIconExpand. setImageResource (R. drawable. update_detail_down) ;}}} class DropDownAnim extends Animation {/** target height */private int targetHeight;/** target view */private View view; /** whether to expand down */private boolean down; /*** constructor ** @ param targetview * view to be displayed * @ param vieweight * destination height * @ param isdown * true: expand down, false: hide */public DropDownAnim (View targetview, int vieweight, boolean isdown) {this. view = targetviewdomainthis.tar getHeight = vieweight; this. down = isdown;} // when down, interpolatedTime increases from 0 to 1, so that newHeight also increases from 0 to targetHeight @ Overrideprotected void applyTransformation (float interpolatedTime, Transformation t) {int newHeight; if (down) {newHeight = (int) (targetHeight * interpolatedTime);} else {newHeight = (int) (targetHeight * (1-interpolatedTime ));} view. getLayoutParams (). height = newHeight; view. requestLayout (); if (view. getVisibility () = View. GONE) {view. setVisibility (View. VISIBLE) ;}@ Overridepublic void initialize (int width, int height, int parentWidth, int parentHeight) {super. initialize (width, height, parentWidth, parentHeight) ;}@ Overridepublic boolean willChangeBounds () {return true ;}}}

Layout:

<RelativeLayout 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: background = "# FFFFFF"> <com. example. view. expandableLayout android: id = "@ + id/resize" android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: orientation = "vertical"> <LinearLayout android: id = "@ + id/collapse_value" android: layout_width = "fill_parent" android: layout_height = "34dip" android: layout_marginLeft = "14dip" android: gravity = "center_vertical" android: orientation = "horizontal"> <TextView android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_marginLeft = "30dp" android: text = "click I will show the hidden content" android: textColor = "#000000" android: textSize = "18sp"> </TextView> <LinearLayout android: layout_width = "fill_parent" android: layout_height = "fill_parent" android: gravity = "right | center_vertical" android: orientation = "horizontal"> <ImageView android: id = "@ + id/icon_value" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_marginRight = "22dip" android: src = "@ drawable/update_detail_down"/> </LinearLayout> <RelativeLayout android: id = "@ + id/expand_value" android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: background = "@ drawable/app_detail_safety_info_bg" android: orientation = "horizontal" android: paddingBottom = "8dip" android: paddingTop = "8dip"> <ImageButton android: id = "@ + id/btn" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_centerVertical = "true" android: layout_marginLeft = "30dp" android: background = "@ null" android: src = "@ drawable/btn_icon" android: textColor = "# 0A84BD" android: textSize = "16sp"/> <TextView android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_marginLeft = "30dp" android: layout_toRightOf = "@ id/btn" android: text = "hidden content" android: textColor = "#000000" android: textSize = "18sp"> </TextView> </RelativeLayout> </com. example. view. expandableLayout> </RelativeLayout>

You only need to setContentView layout in this activity and click view to execute the expanded animation.

Code: http://download.csdn.net/detail/baidu_nod/7812705

:



For android, I have hidden a control, controlled by a button, and clicked to display the control

Two ideas are provided: 1. Code layout. Note that the Code layout is not in XML. Click the event and re-layout.
2. It is a bit opportunistic, that is, to create an Activity and set the layout as the style you want after clicking it. it is OK to jump to this Activity after clicking Intent. The first type requires knowledge and is relatively complicated.

Click a button in a row in the gridview to display a hidden control of the row.

The RowCommand event cannot directly obtain the current row. You can use other events. For example, you can use e. newEditIndex gets the current row. If you do not want to change the previous field to edit status and set it to read-only
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.