亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

如果寫一個點擊view帶動畫的下滑展開顯示隱藏內容的控件

標簽:
Android

效果图:

https://img1.sycdn.imooc.com//5c0dd88f000106e806401114.jpg


原理是在onMeasure中得到隐藏内容的高度,点击这个view的时候对隐藏的view startAnimation,让它的高度从0增长到onMeasure得到的这个View的measureHeight

代码是:

public class ExpandableLayout extends LinearLayout {

   private Context mContext;
   private LinearLayout mHandleView;
   private RelativeLayout mContentView;
   private ImageView mIconExpand;
   int mContentHeight = 0;
   int mTitleHeight = 0;
   private boolean isExpand;
   private Animation animationDown;
   private Animation animationUp;

   public ExpandableLayout(Context context, AttributeSet attrs) {
      super(context, attrs);
      this.mContext = context;
   }

   @Override
   protected 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);
   }

   @Override
   protected 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 {
      @Override
      public final void onClick(View paramView) {

         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 {
      private int targetHeight;
      private View view;
      private boolean down;


      public DropDownAnim(View targetview, int vieweight, boolean isdown) {
         this.view = targetview;
         this.targetHeight = vieweight;
         this.down = isdown;
      }

      @Override
      protected 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);
         }
      }

      @Override
      public void initialize(int width, int height, int parentWidth,
            int parentHeight) {
         super.initialize(width, height, parentWidth, parentHeight);
      }

      @Override
      public boolean willChangeBounds() {
         return true;
      }
   }
}

xml文件为:

<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/app_detail_safety_info"
        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="点击我会显示隐藏的内容"
                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:class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="@drawable/update_detail_down" />
            </LinearLayout>
        </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:class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="@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="隐藏的内容"
                android:textColor="#000000"
                android:textSize="18sp" >
            </TextView>
        </RelativeLayout>
    </com.example.view.ExpandableLayout>

</RelativeLayout>

Activity中引用:

public class TestDropdownHideActivity extends Activity {

   public static void startActivity(Context context) {
      Intent intent = new Intent();
      intent.setClass(context,TestDropdownHideActivity.class);
      context.startActivity(intent);
   }

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.test_layout);
   }

   
}

代码在https://github.com/nickgao1986/StepSport

點擊查看更多內容
TA 點贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優質文章

正在加載中
全棧工程師
手記
粉絲
6509
獲贊與收藏
303

關注作者,訂閱最新文章

閱讀免費教程

  • 推薦
  • 評論
  • 收藏
  • 共同學習,寫下你的評論
感謝您的支持,我會繼續努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦
今天注冊有機會得

100積分直接送

付費專欄免費學

大額優惠券免費領

立即參與 放棄機會
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號

舉報

0/150
提交
取消