How to bring a view to the forefront without calling the bringToFront () method?

In Android, there is an error that breaks View.bringToFront .

If I have views ordered in a GridView and you want to bring the view to the beginning by calling the showToFront () method, you can move it to the lower right position in the GridView.

Something shadow happens there, so I cannot use the bringToFront () method. When I do not call it, everything works fine. But overlapping views - if I use scaled animation to scale the view, it is partially hidden by the views below and to the right.

I checked the source of bringToFront and it calls parent.bringChildToFront (...)

this method

 public void bringChildToFront(View child) { int index = indexOfChild(child); if (index >= 0) { removeFromArray(index); addInArray(child, mChildrenCount); child.mParent = this; } } 

he apparently removes the view from himself! No mercy! Is the ViewGroup so dumb that it can't control the Z-indexes in any other way that moves the controls? Obviously, when a GridView removes its child and then adds it back, it is placed at the end of the grid!

Is there anything I can do to show a top view of others without resorting to hacking? One thing that comes to my mind is to create another view over the GridView that will seem to be taller than the one I'm trying to bring ToFront (), but I don't like this solution.

+6
android layout view z-index viewgroup
source share
5 answers

Apparently, I missed the android:clipChildren="false" property in the GridView. This solves my problem.

+3
source share

Have you tried to make View focal and just calling requestFocus ()?

+1
source share

The call to the bringToFront () function changes the order in the GridView. Try creating an ImageView with the image of the view you want to animate and animate instead.

 Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); view.draw(canvas); final ImageView imageView = new ImageView(getActivity()); imageView.setImageBitmap(bitmap); ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(view.getWidth() , view.getHeight()); imageView.setLayoutParams(params); rootview.addView(imageView); 

Add animation for the animation and remove the ImageView at the end of the animation.

+1
source share

From API 21 you can call:

 view.setZ(float) 
+1
source share

If you target the API above 21, you can simply add the android:translationZ="xxx dp" attribute to your XML.

Note that if you add an elevation in the form of a cardview view, you go to the Z axis. And if you want the view to come to the fore using this method, you just need to make it higher than the level you set.

Example: A 6x boost in map view will require 7dp in the translationZ attribute for the view you want foreground.

0
source share

All Articles