android ListView條目中TextView隱藏到顯示時的測量

來源:互聯網
上載者:User

標籤:

覺得ExpendableListView挺好用,但是就是代碼複雜了點,我一時半會理解不了,於是就直接自己寫個效果來實現。先來看一下expendableListView中展開的動畫效果:

然後我模仿此效果,建立如下的item布局:

<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。動畫效果如下:

   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始終為一行,多餘的字元不能顯示出來。

最終查閱一下,

View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.EXACTLY);

對於代碼中的0所代表的參數是建議寬度,於是嘗試將父級view的寬度傳入,結果居然好了:

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

於是效果如下:

android ListView條目中TextView隱藏到顯示時的測量

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.