您的位置:首页 > 其它

简单展开收起某个隐藏布局

2016-02-16 15:15 459 查看
点击某个按钮,实现另一个布局隐藏和显示之间的切换。

布局文件代码:

<ImageView
android:id="@+id/img_click"
android:layout_gravity="center_horizontal"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:src="@mipmap/ic_open"/>
<LinearLayout
android:id="@+id/ll_show"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical"
android:visibility="gone"
android:layout_marginBottom="-1dp"
android:background="#cccccc">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我就是隐藏的内容"
android:layout_marginTop="10dp"/>
</LinearLayout>


这里说明一下,LinearLayout的marginBottom是必须的,它应该是可以避免的,但是我没有想到好的方法,为什么必须在下面代码就知道了。这个marginBottom值只在第一次时影响布局,后面就不会再产生影响了,如果初始是隐藏状态,这个几乎不会有任何影响;如果初始是显示,那么就给他个负数的值。

很简单的activity代码:

imgClick = (ImageView) findViewById(R.id.img_click);
llShow = (LinearLayout) findViewById(R.id.ll_show);
imgClick.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//第一个view参数是点击后修改的对象控件,第二个view参数是需要隐藏显示的布局控件,第三个参数是动画持续时间,单位毫秒
ExpandAnimation animation = new ExpandAnimation(imgClick,llShow,500);
llShow.startAnimation(animation);
}
});

imgClick和llShow是声明好的两个空间,在这里初始化,关键的内容是这个ExpandAnimation的内容,也就是一个自定义的动画类;

关键的ExpandAnimation类代码:

/**
* This animation class is animating the expanding and reducing the size of a view.
* The animation toggles between the Expand and Reduce, depending on the current state of the view
* @author Udinic
*
*/
public class ExpandAnimation extends Animation{
private View mAnimatedView;
private LayoutParams mViewLayoutParams;
private int mMarginStart, mMarginEnd;
private boolean mIsVisibleAfter = false;
private boolean mWasEndedAlready = false;

private ImageView arrow;
/**
* Initialize the animation
* @param view The layout we want to animate
* @param duration The duration of the animation, in ms
*/
public ExpandAnimation(View open, View view, int duration) {

setDuration(duration);
arrow = (ImageView) open;
mAnimatedView = view;
mViewLayoutParams = (LayoutParams) view.getLayoutParams();

// if the bottom margin is 0,
// then after the animation will end it'll be negative, and invisible.
mIsVisibleAfter = (mViewLayoutParams.bottomMargin == 0);

//根据状态获取动画起始参数
mMarginStart = mIsVisibleAfter ? 0:(0- view.getHeight());
mMarginEnd = (mMarginStart == 0 ? (0- view.getHeight()) : 0);

arrow.setImageResource(R.mipmap.ic_close);
mAnimatedView.setVisibility(View.VISIBLE);
}

@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
super.applyTransformation(interpolatedTime, t);

if (interpolatedTime < 1.0f) {

// Calculating the new bottom margin, and setting it
mViewLayoutParams.bottomMargin = mMarginStart
+ (int) ((mMarginEnd - mMarginStart) * interpolatedTime);

// Invalidating the layout, making us seeing the changes we made
mAnimatedView.requestLayout();

// Making sure we didn't run the ending before (it happens!)
} else if (!mWasEndedAlready) {
mViewLayoutParams.bottomMargin = mMarginEnd;
mAnimatedView.requestLayout();

if (mIsVisibleAfter) {
mAnimatedView.setVisibility(View.GONE);
arrow.setImageResource(R.mipmap.ic_open);
}
mWasEndedAlready = true;
}
}
}


基本就是这样了,用一个动画类来实现隐藏显示,特别简单呐,不用理解也能够用。
这个使用时数据已有的情况,如果是展开后加载数据,这个不适用了,也许改改就行了,也许不行。具体看需求,完事收工!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: