androider androider - 5 months ago 19
Android Question

Is it possible to bring ViewGroups items in front of ViewGroups siblings?

I'm not sure if my question is understandable. I have an

Image
and a
ViewGroup
in my layout. The
ViewGroup
is comparable with a
GridView
, where I have items, that I can drag and drop.
Now I'm interested in dragging the items of my
ViewGroup
over the image, but every time I try, the items seems to have a lower z-index, because the disappear behind the image.

I already tried combinations with
bringToFront
on the
ViewGroup
and its items, but nothing worked. Now my question is, whether it is even possible to bring the
viewgroups
items in front of the
viewgroups
sibling. Does anyone have experience with that?

My xml looks like this, where
GridView
is my
Viewgroup
...the image resource and visiblity of the image is set in code:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/relative_gridview_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:tag="@string/tag_fragment_GridView"
android:background="#FFF"
android:orientation="vertical" >

<ImageView
android:id="@+id/img"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:visibility="gone" />


<de.test.GridView
android:id="@+id/grid_view1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/img"
android:background="#FFF"
android:gravity="center"
android:numColumns="auto_fit"
android:stretchMode="none"
android:tag="grid_view_tag" >
</de.test.GridView>




EDIT:
I use a BaseAdapter to display the layout, because I have a dynamically amount of pages, that can be flipped using this library https://github.com/openaphid/android-flip.

The layout I create like the following:

public View loadView(final View view, final int position) {

final GridView dgv = (GridView) view
.findViewById(R.id.grid_view1);


dgv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(android.widget.AdapterView<?> arg0, View v,
int position, long arg3) {

// Simple click listener for the items...



}
});

dgv.getViewTreeObserver().addOnGlobalLayoutListener(
new OnGlobalLayoutListener() {

@Override
public void onGlobalLayout() {
if (Build.VERSION.SDK_INT < 16) {
dgv.getViewTreeObserver()
.removeGlobalOnLayoutListener(this);
} else {
dgv.getViewTreeObserver()
.removeOnGlobalLayoutListener(this);
}

int displayHeight = view.getHeight();
int displayWidth = view.getWidth();


dgv.initChilds(displayWidth, displayHeight,
2,
2);
} else {
mImage = (ImageView) view
.findViewById(R.id.img);
mImage.getLayoutParams().height = displayHeight / 2;

mImage.setImageResource(R.drawable.bild);


dgv.initChilds(displayWidth,
displayHeight / 2, 2,
2);
}

updateGridView();
}

});

mList.add(view);

return view;
}


the initChilds()-Methods determines the size of each item of the viewgroup. I would like to set the viewgroup item to the front, when it is choosen by longclick. The longclicklistener looks like that:

@Override
public boolean onLongClick(View view) {

draggedItem = getDraggedView(); draggedItem.bringToFront();
callback.bringGridToFront();

return true;
}


I try to bring the item to the front like this, but it do not work...the callback variable is an instance of the baseadapter class, which calls the bringGridToFront()-Method, which currently executes the following code:

public void bringGridToFront() {
View view = mList.get(mCurrentPage);
RelativeLayout rl = (RelativeLayout) view.findViewById(R.id.relative_gridview_layout);
GridView dgv = (GridView) view.findViewById(R.id.grid_view1);
rl.bringChildToFront(dgv);
}

Answer

thanks for all the help. Finally the bringToFront()-Method seems to do not work for me. Now I'm able to solve my problem by using the getChildDrawingOrder()-Method overrided from ViewGroups.

With this method you can change the drawing order of the children and so draw the item you want to lay above all the others at last. An example:

@Override
public int getChildDrawingOrder(int childCount, int i) {
    if(mDragged != -1) {

        if(i == mDragged) {
            return childCount-1;
        }
        else if(i == (childCount - 1)) {
            return mDragged;
        }       
        else {
            return i;
        }
    }

    return i;
}

mDragged is the variable that should lay in the front. So i changed the drawing position of this variable with the last one...hope this help for anyone who had the same problem :D