1

LinearLayoutAndroidでいくつかのコンポーネントを動的に削除しようとしています。私が実装したロジックは次のとおりです。誰かが私の(サイドバー)のボタンをクリックすると、クリックしたものを上に移動し、その他のボタンをディスプレイの左側からFragment移動するアニメーションを開始します。アニメーションは、アニメーションが完了した後、レイアウト内のすべてのボタンを反復し、クリックされていないすべてのボタンを削除することをButtonトリガーします。AnimationListener問題は、フェード アウト アニメーションを無効にして、デバッガーにステップインすると、その後もまだそこにあるremoveView()ことがわかります。Views

さらに大きな問題はButton、後で X と Y の位置を手動で設定すると、まだ表示されているはずの (クリックされたもの) が消えて再び表示されることです。

削除中にボタンを「修正」できる方法はありますremoveView()か?実際にビューを削除/レイアウトを更新するのはいつですか?

私はすでにすべてのビューを削除してから、同じ結果でボタンを再度追加しようとしました。

少し物事を片付けるためのいくつかのスニペット:

//Start Animation over all Components
//Commented out the move outside animation to ensure the Layout doesn't get streched in this process
        for (mComponentIterator = mLayout.getChildCount() - 1; mComponentIterator >= 0; mComponentIterator--) {
            final ImageTextButton currentButton = (ImageTextButton) mLayout
                    .getChildAt(mComponentIterator);
            ObjectAnimator buttonAnimation;
            if (!currentButton.equals(pClickedButton)) {
//              buttonAnimation = ObjectAnimator.ofFloat(currentButton, View.X, POSITION_OUTSIDE);
            } else {
//              buttonAnimation = ObjectAnimator.ofFloat(currentButton, View.Y, POSITION_UPPER);
                animations.add( ObjectAnimator.ofFloat(currentButton, View.Y, POSITION_UPPER));
                currentButton.setFocused(true);
                mSelectedButton = currentButton;
            }
//          animations.add(buttonAnimation);
        }
            buttonAnimations.playTogether(animations);
        buttonAnimations.setDuration(mShortAnimationDuration);
        buttonAnimations.setInterpolator(new DecelerateInterpolator());
        buttonAnimations.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator arg0) {
                for (mComponentIterator = mLayout.getChildCount() - 1; mComponentIterator >= 0; mComponentIterator--) {
                    final ImageTextButton currentButton = (ImageTextButton) mLayout
                            .getChildAt(mComponentIterator);
                    if (currentButton.equals(mSelectedButton)) {
                        if (mCurrentFragment != null) {

                             //This changes the Layout in another 
                             mListener.onChangeLayoutRequest(R.id.maincontent, mCurrentFragment);
                        }
                    } else {
                                mLayout.removeView(currentButton);
                    }
                }
            }
        });
        buttonAnimations.start();

//Callback when Activity is ready
mCurrentFragment.SetOnActivityCreatedListener(new OnActivityCreatedListener() {
                @Override
                public void onActivityCreated(Activity activity) {
                    Drawable grayMenuBackground = getActivity().getResources().getDrawable(
                            R.drawable.bg_menuitem);
                    Drawable coloredMenuBackground = grayMenuBackground.getConstantState()
                            .newDrawable();
                    coloredMenuBackground.setColorFilter(mSelectedMenuColor,
                            PorterDuff.Mode.SRC_ATOP);
                    LinearLayout.LayoutParams rightGravityParams = new LinearLayout.LayoutParams(
                            LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
                    rightGravityParams.gravity = Gravity.TOP;
                    rightGravityParams.weight = 0;

                    Drawable seperator = getResources().getDrawable(R.drawable.seperator_menu);
                    seperator.setBounds(new Rect(0, 0, mSelectedButton.getWidth(), 1));

                    int insertIndex = 1;
                    for (Button menuButton : mCurrentFragment.getMenuItems()) {

                        //Some Button setupcode
                    mLayout.addView(menuButton, insertIndex++);
                }
                //Here the Button gets visible again
                mSelectedButton.setLayoutParams(rightGravityParams);
                mSelectedButton.setX(POSITION_LEFT);
                mSelectedButton.setY(POSITION_UPPER);
            }
        });

私はこの問題で2日間立ち往生しています。現在、ボタンは正しい方法でアニメーション化され、アニメーションが終了した直後に、すべてのボタンが消えます (クリックされたボタンも)。次に、1/2秒後。他のフラグメントをロードする必要があるため、クリックされたボタンが再び表示されます

PS: ''mLayout.post()'' を使用して削除をスケジュールしましたが、結果は同じでした

4

1 に答える 1

1

単純に次のようにするとどうなりますか。

button.setVisibility(View.GONE) ;

コードにいくつかのコメントを追加しました。

//Start Animation over all Components
//Commented out the move outside animation to ensure the Layout doesn't get streched in this process
    for (mComponentIterator = mLayout.getChildCount() - 1; mComponentIterator >= 0; mComponentIterator--) {
        final ImageTextButton currentButton = (ImageTextButton) mLayout
                .getChildAt(mComponentIterator);
        ObjectAnimator buttonAnimation;
        if (!currentButton.equals(pClickedButton)) {
//              buttonAnimation = ObjectAnimator.ofFloat(currentButton, View.X, POSITION_OUTSIDE);
        } else {
//              buttonAnimation = ObjectAnimator.ofFloat(currentButton, View.Y, POSITION_UPPER);

            /* 
             * You say that you want to keep the clicked button, yet you
             * add it to the animations?
             */

            animations.add( ObjectAnimator.ofFloat(currentButton, View.Y, POSITION_UPPER));
            currentButton.setFocused(true);
            mSelectedButton = currentButton;
        }
//          animations.add(buttonAnimation);
    }
        buttonAnimations.playTogether(animations);
    buttonAnimations.setDuration(mShortAnimationDuration);
    buttonAnimations.setInterpolator(new DecelerateInterpolator());
    buttonAnimations.addListener(new AnimatorListenerAdapter() {
        @Override
        public void onAnimationEnd(Animator arg0) {
            for (mComponentIterator = mLayout.getChildCount() - 1; mComponentIterator >= 0; mComponentIterator--) {
                final ImageTextButton currentButton = (ImageTextButton) mLayout
                        .getChildAt(mComponentIterator);
                if (currentButton.equals(mSelectedButton)) {
                    if (mCurrentFragment != null) {

                         //This changes the Layout in another 

                         /*
                          * What exactly does this do and why does it have to
                          * be done (numberOfButtons - 1) times?
                          */

                         mListener.onChangeLayoutRequest(R.id.maincontent, mCurrentFragment);
                    }
                } else {

                            /*
                             * This does indeed only remove the non selected buttons 
                             * I think, but the animation is still applied to 
                             * the selcted button.
                             * So it makes sense that you have to reset X and Y 
                             * to see it again.
                             */ 
                            mLayout.removeView(currentButton);
                }
            }
        }
    });
    buttonAnimations.start();

//Callback when Activity is ready
mCurrentFragment.SetOnActivityCreatedListener(new OnActivityCreatedListener() {
            @Override
            public void onActivityCreated(Activity activity) {
                Drawable grayMenuBackground = getActivity().getResources().getDrawable(
                        R.drawable.bg_menuitem);
                Drawable coloredMenuBackground = grayMenuBackground.getConstantState()
                        .newDrawable();
                coloredMenuBackground.setColorFilter(mSelectedMenuColor,
                        PorterDuff.Mode.SRC_ATOP);
                LinearLayout.LayoutParams rightGravityParams = new LinearLayout.LayoutParams(
                        LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
                rightGravityParams.gravity = Gravity.TOP;
                rightGravityParams.weight = 0;

                Drawable seperator = getResources().getDrawable(R.drawable.seperator_menu);
                seperator.setBounds(new Rect(0, 0, mSelectedButton.getWidth(), 1));

                int insertIndex = 1;
                for (Button menuButton : mCurrentFragment.getMenuItems()) {

                    //Some Button setupcode
                mLayout.addView(menuButton, insertIndex++);
            }
            //Here the Button gets visible again
            mSelectedButton.setLayoutParams(rightGravityParams);
            mSelectedButton.setX(POSITION_LEFT);
            mSelectedButton.setY(POSITION_UPPER);
        }
    });
于 2013-10-02T09:10:09.727 に答える