次のシナリオをアニメーション化する必要があります。
ListViewがあり、各セルには3つのレイアウトがあります。
- Preview_layout (青):contents_layoutのコンテンツのプレビューが含まれます。
- contents_layout (緑):いくつかのボタンを含む長いテキストが含まれています。
- wrapper_layout(赤):preview_layoutとcontents_layoutが含まれています。
contents_layoutは可視性が「gone」に設定されているため、preview_layoutのみがリストに表示されます。
ListViewのセルが押されたら、contents_layoutをスライドダウンアニメーションで表示する必要があります。
今まで私は次の解決策を使用しました:
ListViewのgetViewで:
// Preview_layouts height
int hPreview = 70;
// Views
final View previewView = rowView.findViewById(R.id.preview);
final View contentsView = rowView.findViewById(R.id.contents);
// On previewView click
previewView.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
if (!contentsView.isShown()) {
// Close all the contents_layout of the ListView (menuListView)
for (int i=0; i < menuListView.getChildCount(); i++) {
View v = menuListView.getChildAt(i).findViewById(R.id.contents);
if (v.isShown()) { v.startAnimation(new SlideUpAnimation(v, hPreview)); }
}
// Slide down the selected contents_layout (contentsView)
contentsView.startAnimation(new SlideDownAnimation(contentsView, hPreview));
}
}
});
SlideDownAnimationクラス:
public class SlideDownAnimation extends Animation {
private View target;
private LayoutParams targetResize;
private int mFromHeight, mToHeight;
public SlideDownAnimation( View targetToSlideDown, int fromHeight ) {
// Show the contents_layout target
target = targetToSlideDown;
target.setVisibility(View.VISIBLE);
// Animation property
setDuration(500);
setInterpolator(new DecelerateInterpolator());
// Target
targetResize = targetToSlideDown.getLayoutParams();
mFromHeight = fromHeight;
mToHeight = targetResize.height - fromHeight;
targetResize.height = 1;
}
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
// Set the Alpha to fade in the contents_layout target
t.setAlpha(interpolatedTime);
// Set the height to slide down the contents_layout target
targetResize.height = (int) (mToHeight * interpolatedTime) + mFromHeight;
target.requestLayout();
}
}
このソリューションの大きな問題は、android:layout_height = "wrap_content"に設定されていて、可視性がGONEの場合、contents_layoutの高さを取得できず、単に「0」を返すことです。
私が必要とするアニメーションを実行するための他の解決策はありますか?