Home | History | Annotate | Download | only in expandingcells
      1 /*
      2  * Copyright (C) 2013 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.example.android.expandingcells;
     18 
     19 import android.content.Context;
     20 import android.util.AttributeSet;
     21 import android.widget.RelativeLayout;
     22 
     23 /**
     24  * This layout is used to contain the extra information that will be displayed
     25  * when a certain cell is expanded. The custom relative layout is created in
     26  * order to achieve a fading affect of this layout's contents as it is being
     27  * expanded or collapsed as opposed to just fading the content in(out) after(before)
     28  * the cell expands(collapses).
     29  *
     30  * During expansion, layout takes place so the full contents of this layout can
     31  * be displayed. When the size changes to display the full contents of the layout,
     32  * its height is stored. When the view is collapsing, this layout's height becomes 0
     33  * since it is no longer in the visible part of the cell.By overriding onMeasure, and
     34  * setting the height back to its max height, it is still visible during the collapse
     35  * animation, and so, a fade out effect can be achieved.
     36  */
     37 public class ExpandingLayout extends RelativeLayout {
     38 
     39 
     40     private OnSizeChangedListener mSizeChangedListener;
     41     private int mExpandedHeight = -1;
     42 
     43     public ExpandingLayout(Context context) {
     44         super(context);
     45     }
     46 
     47     public ExpandingLayout(Context context, AttributeSet attrs) {
     48         super(context, attrs);
     49     }
     50 
     51     public ExpandingLayout(Context context, AttributeSet attrs, int defStyle) {
     52         super(context, attrs, defStyle);
     53     }
     54 
     55     protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec) {
     56         if (mExpandedHeight > 0) {
     57             heightMeasureSpec = MeasureSpec.makeMeasureSpec(mExpandedHeight, MeasureSpec.AT_MOST);
     58         }
     59         super.onMeasure(widthMeasureSpec, heightMeasureSpec);
     60     }
     61 
     62     protected void onSizeChanged (int w, int h, int oldw, int oldh) {
     63         mExpandedHeight = h;
     64         //Notifies the list data object corresponding to this layout that its size has changed.
     65         mSizeChangedListener.onSizeChanged(h);
     66     }
     67 
     68     public int getExpandedHeight() {
     69         return mExpandedHeight;
     70     }
     71 
     72     public void setExpandedHeight(int expandedHeight) {
     73         mExpandedHeight = expandedHeight;
     74     }
     75 
     76     public void setSizeChangedListener(OnSizeChangedListener listener) {
     77         mSizeChangedListener = listener;
     78     }
     79 }
     80