2010-10-03 38 views
5

Tôi có chế độ xem danh sách sử dụng bộ điều hợp tùy chỉnh để hiển thị nội dung tùy chỉnh của tôi. Cách bố trí của nó là như sau.Thêm hoạt ảnh vào một ListView để mở rộng/thu gọn nội dung

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <LinearLayout 
     android:orientation="horizontal" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_weight="1"> 
     <ImageView 
      android:id="@+id/itemimage" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_weight="5" 
      android:scaleType="fitCenter"/> 
     <TextView 
      android:id="@+id/itemdescription" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:padding="10dp" 
      android:textSize="16sp" 
      android:layout_weight="1"/> 
    </LinearLayout>  
    <TextView 
      android:id="@+id/itemtext" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:text="TEXT CONTENT" 
      android:layout_weight="1"/> 
</LinearLayout>  

Tôi muốn rằng chế độ xem danh sách chỉ hiển thị chế độ xem với mục id và mô tả mặt hàng, giữ cho mục tiếp theo bị ẩn. Ý tưởng là để có một onclicklistener trên mỗi mục của danh sách, để mở rộng mục đó để nó cho thấy nội dung itemtext. Tôi biết tôi nên sử dụng hoạt hình Tweening để mở rộng/thu gọn từng mục, nhưng tôi không thể tìm ra cách để làm điều đó.

Có ai có thể giúp tôi không? Nếu bạn cần thêm đoạn mã, vui lòng hỏi.

Xin cảm ơn trước.

+0

Xin chào, cuối cùng bạn có tìm ra nó không? Tôi sẽ quan tâm đến giải pháp. – dragoon

Trả lời

7

Để làm điều đó, tôi đã xây dựng một lớp Hoạt ảnh, làm sinh động lề thành giá trị âm, làm cho mục biến mất.

Các hoạt hình trông như thế này:

public class ExpandAnimation extends Animation { 
@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(); 
    } 
} 
} 

Tôi có cả một ứng dụng ví dụ cho hình ảnh động này trên blog của tôi post

0

Cố gắng giải pháp Udinic, nhưng cuối cùng đã chọn thay thế này:

res/anim/scale_down.xml

<?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android" > 
<scale 
    android:duration="700" 
    android:fromXScale="1.0" 
    android:fromYScale="1.0" 
    android:pivotX="50%" 
    android:pivotY="0%" 
    android:toXScale="1.0" 
    android:toYScale="0.0" /> 
</set> 

An imate thực hiện ListView:

protected void animateView(final View v, final int animResId, final int endVisibility){ 
    Animation anim = AnimationUtils.loadAnimation(getApplicationContext(), 
      animResId); 
    anim.setAnimationListener(new Animation.AnimationListener() { 
     public void onAnimationStart(Animation animation) { 
      v.setVisibility(View.VISIBLE); 
     } 
     public void onAnimationEnd(Animation animation) { 
      v.setVisibility(endVisibility); 
     } 
     public void onAnimationRepeat(Animation animation) {} 
    }); 
    v.startAnimation(anim); 
} 

Ví dụ gọi để animate ListView của tôi (hoặc bất kỳ Xem lớp trẻ):

animateView(listView1, R.anim.scale_down, View.GONE); 
animateView(listView1, R.anim.scale_up, View.VISIBLE); 

Nó đang làm việc trên điện thoại KitKat của tôi.

Các vấn đề liên quan