以爲ExpendableListView挺好用,可是就是代碼複雜了點,我一時半會理解不了,因而就直接本身寫個效果來實現。先來看一下expendableListView中展開的動畫效果:java
而後我模仿此效果,創建以下的item佈局:android
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:paddingTop="5dp" android:paddingBottom="5dp" android:layout_marginBottom="5dp" android:layout_marginTop="5dp"> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:id="@+id/titleTextView" android:layout_weight="1" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="10dp" android:minHeight="48dp" android:textSize="20sp" /> <ImageButton android:id="@+id/favorBtn" android:background="@null" android:minHeight="48dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@mipmap/favor_normal"/> </LinearLayout> <TextView android:padding="5dp" android:layout_margin="5dp" android:id="@+id/contentTextView" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="20sp" android:visibility="gone" /> </LinearLayout>
一個顯示的TextView用來顯示英文,一個隱藏的TextView用於顯示中文,在此ListView的item單擊時,顯示/隱藏那個中文的TextView。動畫效果以下:ide
public static void animateExpanding(final TextView view) { view.setVisibility(View.VISIBLE); //測量高寬 final int widthSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.EXACTLY); final int heightSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED); view.measure(widthSpec, heightSpec); //根據測量結果 建立屬性動畫 ValueAnimator animator = createHeightAnimator(view, 0, view.getMeasuredHeight()); animator.start(); } /** *建立屬性動畫 **/ public static ValueAnimator createHeightAnimator(final View view, int start, int end) { ValueAnimator animator = ValueAnimator.ofInt(start, end); animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator valueAnimator) { int value = (Integer) valueAnimator.getAnimatedValue(); ViewGroup.LayoutParams layoutParams = view.getLayoutParams(); layoutParams.height = value; view.setLayoutParams(layoutParams); } }); return animator; }
原本以爲挺簡單的效果,可是悲催的發現,以下:佈局
Item一展開,就不知道爲啥高度會那麼大,若是在測量的時候,對寬度也使用UNSPECIFIED,則會致使中文的的TextView始終爲一行,多餘的字符不能顯示出來。動畫
最終查閱一下,
code
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.EXACTLY);
對於代碼中的0所表明的參數是建議寬度,因而嘗試將父級view的寬度傳入,結果竟然好了:orm
public static void animateExpanding(View parentView,final View view) { view.setVisibility(View.VISIBLE); //設置建議寬度爲父級view的寬度 final int widthSpec = View.MeasureSpec.makeMeasureSpec(parentView.getWidth(), View.MeasureSpec.EXACTLY); final int heightSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED); view.measure(widthSpec, heightSpec); // Log.i("SSSS",view.getText().toString()+"||"+view.getLineCount()); // Log.i("SSSS", "Mheight:::" + view.getMeasuredHeight() + "h:" + view.getHeight() + "mW:" + view.getMeasuredWidth() + "w:" + view.getWidth()); ValueAnimator animator = createHeightAnimator(view, 0, view.getMeasuredHeight()); animator.start(); }
因而效果以下:xml
到這時,大部分的中文都能顯示出來正常了,可是仍然有部分的沒有換行,因此嘗試將傳入的View更改成視圖中的英文的TextView,並在測量高度時的建議高度設置爲英文TextView的高度,終於解決了。ip
final int heightSpec = View.MeasureSpec.makeMeasureSpec(v.getHeight(), View.MeasureSpec.UNSPECIFIED);