2

我正在尝试在 android 中创建一个可扩展的菜单项,它看起来像一个按钮,并且在单击按钮时,按钮将随着动画向下展开。我为布局设置了一个展开动画,当单击我的视图时我想展开它,但动画有问题。当我单击视图时它不会立即启动,而是在我向下滚动或向上滚动视图容器时启动。如果容器不可滚动,我的动画永远不会开始。我究竟做错了什么?

这是我的扩展方法、onClick 方法和我的自定义视图的布局 xml 文件,它们将执行此操作:

扩张:

public void expand(final View v) {

        try {
            Method m = v.getClass().getDeclaredMethod("onMeasure", int.class, int.class);
            m.setAccessible(true);
            m.invoke(v,
                    MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
                    MeasureSpec.makeMeasureSpec(((View)v.getParent()).getMeasuredWidth(), MeasureSpec.AT_MOST)
            );
        } catch (Exception e) {
            Log.e(TAG , "Caught an exception!", e);
        }
        final int initialHeight = v.getMeasuredHeight();
        Log.d("test", "initialHeight="+initialHeight);

        v.getLayoutParams().height = 0;
        v.setVisibility(View.VISIBLE);


        Animation a = new Animation() {

            @Override
            protected void applyTransformation(float interpolatedTime,
                    Transformation t) {
                final int newHeight = (int) (initialHeight * interpolatedTime);
                v.getLayoutParams().height = newHeight;
                v.requestLayout();
            }

            @Override
            public boolean willChangeBounds() {
                return true;
            }

        };

        a.setDuration(1000);
        a.setInterpolator(AnimationUtils.loadInterpolator(context,
                android.R.anim.accelerate_decelerate_interpolator));
        v.startAnimation(a);

        isExpanded = !isExpanded;

    }

点击:

public void onClick(View v) {
    if (!isExpanded) {
        expand(subButtonsLayout);
    } else {
        collapse(subButtonsLayout);
    }
}

自定义菜单项视图的布局 xml:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:mtx="http://schemas.android.com/apk/res/com.matriksdata.trademaster"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:gravity="center_horizontal">
    <LinearLayout
        android:id="@+id/xExpandableMenuButtonTop"
        android:background="@drawable/opened_menu_bg_top"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    </LinearLayout>
    <LinearLayout
        android:background="@drawable/opened_menu_bg_center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:gravity="center_vertical">
        <LinearLayout
            android:id="@+id/xExpandableMenuButtonTitle"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center_vertical">
            <TextView
                android:id="@+id/xExpandableMenuButtonText"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:layout_marginLeft="10dip"
                android:layout_marginRight="10dip"
                        android:textAppearance="@style/expandable_menu_button_textstyle"
                android:text="Button Text">
            </TextView>
            <ImageView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="6"
                android:src="@drawable/menu_button_down_arrow">
            </ImageView>
        </LinearLayout>
    </LinearLayout>
    <LinearLayout
        android:id="@+id/xExpandableMenuButtonSubButtonsLayout"
        android:background="@drawable/opened_menu_bg_center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:gravity="center_vertical"
        android:visibility="gone">
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:orientation="vertical">

            <com.myproject.control.XSubMenuButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                mtx:XSubMenuButtonText="SubMenu1">
            </ccom.myproject.control.XSubMenuButton>
            <com.myproject.control.XSubMenuButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                mtx:XSubMenuButtonText="SubMenu2">
            </com.myproject.control.XSubMenuButton>
            <com.myproject.control.XSubMenuButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                mtx:XSubMenuButtonText="SubMenu3">
            </com.myproject.control.XSubMenuButton>

        </LinearLayout>
    </LinearLayout>
    <LinearLayout
        android:id="@+id/xExpandableMenuButtonBottom"
        android:background="@drawable/opened_menu_bg_bottom"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    </LinearLayout>
</LinearLayout>
4

5 回答 5

2

对于任何可能再次面临这种情况的人。

如果您的视图(您正在制作动画的视图)处于消失状态,那么在这种情况下,在开始动画之前,请将 Visiblity 设置为不可见。它会在第一时间起作用。

来源从这里

于 2014-03-30T14:44:03.853 回答
2

如果您还没有解决您的问题或其他人遇到此问题,这里有一个快速解决方案。每次单击时(在onClick方法中)使您的父视图无效。无论父级是否可滚动,这都应该有效。

也就是说,您的代码将类似于:


public void onClick(View v) {
    if (!isExpanded) {
        expand(subButtonsLayout);
    } else {
        collapse(subButtonsLayout);
    }
    rootView.invalidate();
}
于 2011-06-09T13:14:06.487 回答
1

Well, I fixed the problem using only 2 lines.

scroll.setFocusableInTouchMode(true); 
scroll.requestFocus();
animation.Start();//Start anim

Good luck

于 2014-09-19T16:17:59.743 回答
1

我有一个视图,我在布局结束时声明将其 Z 索引保持在其兄弟姐妹之上。我必须触摸页面才能使动画工作。

所以我通过Java再次设置了Z索引,它工作了。

view.bringToFront();
于 2014-01-29T11:40:22.623 回答
0

“如果容器不可滚动,则动画永远不会启动”-> 看看我的类似问题,Scrollview 在太短无法滚动时不会滑动。我发现在我的情况下,这是 Android 2.1 及更低版本中的触摸错误。

于 2011-08-03T02:46:56.353 回答