1

我正在尝试LinearLayout在 Android 中动态删除一些组件。我实现的逻辑是:如果有人单击我的(侧边栏)中的按钮,我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);
            }
        });

我在这个问题上停留了两天。目前按钮以正确的方式进行动画处理,然后,在动画完成后,所有按钮都会消失(也是 Clicked Button)。然后,在 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 回答