public final View inflate() { ViewParent viewParent = getParent(); if (viewParent == null || !(viewParent instanceof ViewGroup)) { throw new IllegalStateException("ViewStub must have a non-null ViewGroup viewParent"); } else if (this.mLayoutResource != 0) { LayoutInflater factory; ViewGroup parent = (ViewGroup) viewParent; if (this.mInflater != null) { factory = this.mInflater; } else { factory = LayoutInflater.from(getContext()); } View view = factory.inflate(this.mLayoutResource, parent, false); if (this.mInflatedId != -1) { view.setId(this.mInflatedId); } int index = parent.indexOfChild(this); parent.removeViewInLayout(this); LayoutParams layoutParams = getLayoutParams(); if (layoutParams != null) { parent.addView(view, index, layoutParams); } else { parent.addView(view, index); } this.mInflatedViewRef = new WeakReference(view); if (this.mInflateListener != null) { this.mInflateListener.onInflate(this, view); } return view; } else { throw new IllegalArgumentException("ViewStub must have a valid layoutResource"); } }
@Test public void testRemoveViewInLayout() { root.removeViewInLayout(new View(context)); assertThat(root.getChildCount(), equalTo(3)); root.removeViewInLayout(child2); assertThat(root.getChildCount(), equalTo(2)); assertThat(root.getChildAt(0), sameInstance(child1)); assertThat(root.getChildAt(1), sameInstance((View) child3)); root.removeViewInLayout(child2); assertThat(root.getChildCount(), equalTo(2)); assertThat(root.getChildAt(0), sameInstance(child1)); assertThat(root.getChildAt(1), sameInstance((View) child3)); root.removeViewInLayout(child1); root.removeViewInLayout(child3); assertThat(root.getChildCount(), equalTo(0)); }
public static void replaceView(View oldChild, View newChild, boolean transferLayoutParams) { final ViewGroup parent = oldChild.getParent() instanceof ViewGroup ? (ViewGroup) oldChild.getParent() : null; final int index = parent.indexOfChild(oldChild); parent.removeViewInLayout(oldChild); final LayoutParams layoutParams = (transferLayoutParams ? oldChild.getLayoutParams() : null); if (layoutParams != null) parent.addView(newChild, index, layoutParams); else parent.addView(newChild, index); }
/** * remove a view * * @param parent * @param view */ public static void removeView(ViewGroup parent, View view) { // 这里不使用post来做,这样代码更可控,而是改为将refresh下拉动作延后一帧处理,见@link // 这里调用removeViewInLayout方法,可以在onLayout的时候调用,否则会产生问题 if (parent != null && view != null) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { if (parent.isInLayout()) { parent.removeViewInLayout(view); } else { parent.removeView(view); } } else { parent.removeView(view); } } }
/** OnDrag event actions */ @Override public boolean onDrag(View v, DragEvent event) { switch (event.getAction()) { case DragEvent.ACTION_DRAG_STARTED: break; case DragEvent.ACTION_DRAG_ENTERED: break; case DragEvent.ACTION_DRAG_EXITED: break; case DragEvent.ACTION_DROP: View vTmp = (View) event.getLocalState(); if (vTmp instanceof CardView) { CardView view = (CardView) vTmp; ViewGroup parent = (ViewGroup) (view.getParent()); parent.removeViewInLayout(view); addView(view); if (parent instanceof HandView) { CardView card = (CardView) view; card.setOnDragListener(null); Gallery g2 = (Gallery) GameActivity.getActivity() .findViewById(R.id.playerview_slider_board_cardgallery); if (g2 != null) { SliderbarCardGallery a2 = (SliderbarCardGallery) g2.getAdapter(); a2.notifyDataSetChanged(); } } else if (parent instanceof BoardView) { // Cards grouped moving if (cardsGroup.getTouching().size() > 1) cardsGroup.move(event.getX(), event.getY(), getWidth(), getHeight()); } MarginLayoutParams marginParams = new MarginLayoutParams(view.getLayoutParams()); int left = (int) (event.getX() - (view.getWidth() / 2)); int top = (int) (event.getY() - (view.getHeight() / 2)); int right = (int) (((View) view.getParent()).getWidth() - left + view.getWidth()); int bottom = (int) (((View) view.getParent()).getHeight() - top + view.getHeight()); marginParams.setMargins(left, top, right, bottom); view.setLayoutParams(new RelativeLayout.LayoutParams(marginParams)); if (parent instanceof HandView) { Log.i(WifiDirectProperty.TAG, "1"); MoveCardAction movecard = new MoveCardAction("Player", GA.user.getId(), view.getCard(), "BoardView"); movecard.setPourcentageX(left * 100 / ((View) view.getParent()).getWidth()); movecard.setPourcentageY(top * 100 / ((View) view.getParent()).getHeight()); Log.i( WifiDirectProperty.TAG, movecard.getPourcentageX() + " " + movecard.getPourcentageY()); GameActivity.getActivity() .getWifiDirectManager() .sendEvent(new WifiDirectEventImpl(WifiDirectEvent.EVENT, movecard)); } else if (parent instanceof DrawPileView) { MoveCardAction movecard = new MoveCardAction("DrawPileView", "BoardView", view.getCard()); movecard.setPourcentageX(left * 100 / ((View) view.getParent()).getWidth()); movecard.setPourcentageY(top * 100 / ((View) view.getParent()).getHeight()); Log.i( WifiDirectProperty.TAG, movecard.getPourcentageX() + " " + movecard.getPourcentageY()); GameActivity.getActivity() .getWifiDirectManager() .sendEvent(new WifiDirectEventImpl(WifiDirectEvent.EVENT, movecard)); } else if (parent instanceof DiscardPileView) { MoveCardAction movecard = new MoveCardAction("DiscardPileView", "BoardView", view.getCard()); movecard.setPourcentageX(left * 100 / ((View) view.getParent()).getWidth()); movecard.setPourcentageY(top * 100 / ((View) view.getParent()).getHeight()); Log.i( WifiDirectProperty.TAG, movecard.getPourcentageX() + " " + movecard.getPourcentageY()); GameActivity.getActivity() .getWifiDirectManager() .sendEvent(new WifiDirectEventImpl(WifiDirectEvent.EVENT, movecard)); } View tmp; for (int i = 0; i < getChildCount(); i++) { tmp = getChildAt(i); if (tmp instanceof CardView) tmp.setVisibility(View.VISIBLE); } vTmp.setVisibility(View.VISIBLE); } else { vTmp.setVisibility(View.VISIBLE); } cardsGroup.getTouching().clear(); break; case DragEvent.ACTION_DRAG_ENDED: break; default: break; } return true; }