gpt4 book ai didi

android - removeView 不会立即移除 View 并破坏 Layout

转载 作者:行者123 更新时间:2023-11-30 03:20:59 24 4
gpt4 key购买 nike

我正在尝试动态删除 Android 中 LinearLayout 中的一些组件。我实现的逻辑是:如果有人单击我的(侧边栏)Fragment 中的一个按钮,我将启动一个动画,将单击的 Button 向上移动,将其他按钮从左侧移出显示器的一侧。 Animation 触发一个 AnimationListener,在 Animation 完成后,它遍历 Layout 中的所有 Buttons 并删除所有未点击的 Buttons。问题是:如果我禁用淡出动画并使用调试器介入,我可以看到,在 removeView() 之后,Views 仍然存在。

更大的问题是,如果我稍后手动设置 X 和 Y 位置,我的 Button 应该仍然可见(被点击的那个)消失了并且只会再次出现。

有什么方法可以在删除按钮时“修复”按钮,removeView() 何时真正删除 View /更新布局?

我已经尝试删除所有 View ,然后再次添加按钮,结果相同。

一些 fragment 来澄清一下:

//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);
}
});

我已经在这个问题上卡了两天了。目前,按钮以正确的方式进行动画处理,然后,在动画完成后,所有按钮都会消失(也包括单击的按钮)。然后,在 1/2 秒后。另一个 fragment 需要加载,点击的按钮再次出现

PS:我已经用 ''mLayout.post()'' 安排了删除,结果相同

最佳答案

如果你只是这样做会发生什么:

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);
}
});

关于android - removeView 不会立即移除 View 并破坏 Layout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19133309/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com