功能简介
已实现的功能点:
1. 子视图显示时,点击父控件,子视图向上折叠消失,箭头向下;
2.子视图不显示时,点击父控件,子视图向下折叠展开,箭头向上;
效果图
外部调用
外部调用代码
elType = (ExpandableLayout)findViewById(R.id.ex_layout_type);
flType= (LinearLayout) findViewById(R.id.father_llayout_type);
clType = (LinearLayout) findViewById(R.id.child_content_llayout_type);
ivDropType = (ImageView) findViewById(R.id.iv_drop_icon_type);
//步骤: 先后台请求数据,请求成功后,再调用init 方法
elType.init(flType, clType, ivDropType);//参数依次为:父布局,子布局,显示箭头的imageView控件
代码
package com.example.cp.mileager.View;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Transformation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import com.example.cp.mileager.R;
/**
* 可折叠布局
*/
public class ExpandableLayout extends LinearLayout {
private Context mContext;
private LinearLayout mHandleView;
private LinearLayout mContentView;
private ImageView mIconExpand;
int mContentHeight = 0;
int mTitleHeight = 0;
private boolean isExpand ;
private Animation animationDown;
private Animation animationUp;
private int index; //布局下标
public static ExpandableLayoutListener listener;
public ExpandableLayout(Context context, AttributeSet attrs) {
super(context, attrs);
this.mContext = context;
}
public void setListIndex(int index){
this.index = index;
}
/**
* 获取视图宽高
*/
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
if (mContentView != null && mHandleView != null) {
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);
}
/**
* 初始化
* @param mHandleView 父布局
* @param mContentView 内容布局
* @param mIconExpand 显示下拉箭头的imageView
*/
public void init(LinearLayout mHandleView, LinearLayout mContentView,
ImageView mIconExpand) {
this.mHandleView = mHandleView;
this.mContentView = mContentView;
this.mIconExpand = mIconExpand;
this.mHandleView.setOnClickListener(new ExpandListener());
isExpand = true;
}
public boolean isExpand(){
return isExpand;
}
private class ExpandListener implements View.OnClickListener {
@Override
public final void onClick(View paramView) {
//clearAnimation是view的方法
clearAnimation();
if (!isExpand) {
openWithAnim();
if(listener !=null){
listener.onExpandableClick(index);
}
} else {
closeWithAnim();
}
}
}
public void openWithAnim(){
if (animationDown == null) {
animationDown = new DropDownAnim(mContentView,
mContentHeight, true);
animationDown.setDuration(300); // SUPPRESS CHECKSTYLE
}
startAnimation(animationDown);
mIconExpand.setImageResource(R.drawable.update_detail_up);
isExpand = true;
}
public void closeWithAnim(){
isExpand = false;
if (animationUp == null) {
animationUp = new DropDownAnim(mContentView,
mContentHeight, false);
animationUp.setDuration(300); // SUPPRESS CHECKSTYLE
}
startAnimation(animationUp);
mIconExpand.setImageResource(R.drawable.update_detail_down);
}
public void close(){
if(mContentView != null){
mContentView.setVisibility(View.GONE);
isExpand = false;
}
}
/**
* 自定义动画
*/
class DropDownAnim extends Animation {
/** 目标的高度 */
private int targetHeight;
/** 目标view */
private View view;
/** 是否向下展开 */
private boolean down;
/**
* 构造方法
*
* @param targetview
* 需要被展现的view
* @param vieweight
* 目的高
* @param isdown
* true:向下展开,false:收起
*/
public DropDownAnim(View targetview, int vieweight, boolean isdown) {
this.view = targetview;
this.targetHeight = vieweight;
this.down = isdown;
}
//down的时候,interpolatedTime从0增长到1,这样newHeight也从0增长到targetHeight
@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;
}
}
public static void setExpandableLayoutListener(ExpandableLayoutListener exListener){
listener = exListener;
}
public interface ExpandableLayoutListener {
/**
* 点击了list中哪个布局
* @param index 本布局所在list 的下标
*/
public void onExpandableClick(int index);
}
}