Android opposite bringChildToFront

Is there a method that does the opposite to bringChildToFront. I want to send the baby back. How can i do this?

+4
source share
3 answers

There is no such method in the API But you can implement it yourself, here is an example:

public void moveChildToBack(View child) { int index = indexOfChild(child); if (index > 0) { detachViewFromParent(index); attachViewToParent(child, 0, child.getLayoutParams()); } } 

This method will only work in subclasses of the android.view.ViewGroup class, since it uses protected methods.

The main idea is to bring your child’s view to the first place on the child list, because the ViewGroup uses the natural order of its child to draw, which means that the first view in the list will have the lowest Z-order and the last view will be have the highest Z-order.

+2
source

Try the code below:

 private void moveToBack(View currentView) { ViewGroup vg = ((ViewGroup) currentView.getParent()); for(int i=0;i<vg.getChildCount();i++){ View v=vg.getChildAt(i); if(!v.equals(currentView)) { vg.bringChildToFront(v); break; } } } 

The idea is to bring the other child forward. if you need to do this to the extreme back, remove "break;".

0
source
 mLastIndex = ((ViewGroup)getParent()).indexOfChild(this); before you call bringChildToFront,you can record the mLastIndex,then you call moveToBack to send view back. private void moveToBack(View currentView) { ViewGroup viewGroup = ((ViewGroup) currentView.getParent()); for(int i = 0; i<viewGroup.getChildCount() - (mLastIndex+1); i++) { LogUtils.d(TAG,viewGroup.getChildAt(mLastIndex)+""); viewGroup.bringChildToFront(viewGroup.getChildAt(mLastIndex)); } } 
0
source

All Articles