2

私はAndroidで拡張可能なメニュー項目を作成しようとしています。これはボタンのように見え、ボタンをクリックすると、ボタンがアニメーションで下に拡張されます。ビューをクリックしたときに展開したいレイアウトに展開アニメーションを設定しましたが、アニメーションに問題があります。ビューをクリックしてもすぐには起動せず、ビューのコンテナを下または上にスクロールすると起動します。また、コンテナがスクロールできない場合、アニメーションは開始されません。私は何が間違っているのですか?

これが私のexpandメソッド、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;

    }

onClick:

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

問題を解決していない場合、または他の誰かがこの問題に遭遇した場合は、ここに簡単な解決策があります。onClick(メソッド内で)クリックするたびに親ビューを無効にします。これは、親がスクロール可能かどうかに関係なく機能するはずです。

つまり、コードは次のようになります。


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

これに再び直面するかもしれない誰にでも。

アニメーション化しているビューが消えた状態になっている場合は、アニメーションを開始する前に、Visiblityをinvisibleに設定します。そして、それは最初の段階で機能します。

ここからのソース。

于 2014-03-30T14:44:03.853 に答える
1

レイアウトの最後に、Zインデックスを兄弟の上に保つように宣言したという見方がありました。アニメーションを機能させるには、ページをタッチする必要がありました。

そこで、Javaを介してZインデックスを再度設定すると、機能しました。

view.bringToFront();
于 2014-01-29T11:40:22.623 に答える
1

さて、2行だけで問題を修正しました。

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

幸運を

于 2014-09-19T16:17:59.743 に答える
0

「コンテナがスクロールできない場合、アニメーションは開始されません」->同様の問題を見てください。スクロールするには短すぎると、Scrollviewがスワイプしません。私の場合、Android2.1以下のタッチバグであることがわかりました。

于 2011-08-03T02:46:56.353 に答える