1. 程式人生 > >簡單展開收起某個隱藏佈局

簡單展開收起某個隱藏佈局

點選某個按鈕,實現另一個佈局隱藏和顯示之間的切換。

佈局檔案程式碼:

<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;
		}
	}
}

基本就是這樣了,用一個動畫類來實現隱藏顯示,特別簡單吶,不用理解也能夠用。

這個使用時資料已有的情況,如果是展開後加載資料,這個不適用了,也許改改就行了,也許不行。具體看需求,完事收工!