2013-10-02 39 views
1

我試圖動態刪除Android中的LinearLayout中的某些組件。我實現的邏輯是:如果有人點擊我的(邊欄)Fragment中的按鈕,我開始一個動畫,將點擊的Button向上移動,其他按鈕移出顯示左側。動畫觸發AnimationListener,在動畫完成後,迭代佈局中的所有按鈕並刪除所有未點擊的按鈕。 問題是:如果我禁用淡出動畫,並介入調試器,我可以看到,在removeView()之後,Views仍然存在。removeView不會刪除立即查看並銷燬佈局

更大的問題是我的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); 
      } 
     }); 

林停留在這個問題現在兩天。目前Buttons以正確的方式動畫化,然後,在動畫結束後,所有按鈕消失(也是Clicked按鈕)。然後,1/2秒後。另一個片段需要加載,點擊的按鈕將再次出現

PS:我已經安排了與去除'mLayout.post()「」具有相同的結果

回答

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

同樣的結果,exept的按鈕,然後會出現** **下額外添加按鈕(在'onActivityCreated'回調添加的那些真正奇怪的是:與取出後,在按鈕會發生什麼 – chuck258

+0

聽起來很奇怪..當你調用mLayout.addView(menuButton,insertIndex ++);,你是否也引用了被刪除的按鈕呢?認爲如果你引用它,它可能也應該被排除在外。 – cYrixmorten

+0

不,''mCurrentFragment.getMenuItems()''只是返回一個額外的按鈕列表,'mSelectedButton'發生的所有事情都在上面的代碼中 因爲我不刪除按鈕我想保留我在1開始'insertIndex''(我想在刪除al之後l其他按鈕,''mSelectedButton''應該在索引0? – chuck258