2

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);
}
4

3 回答 3

1

Android 根据视图在布局树表中的位置管理 z-index。具有最大索引的视图将自己绘制在其他视图之上。在您的布局中,您的自定义网格位于其他网格之上,因为它是布局中的最后一个视图,因此它获得了最大的索引。

在您的代码中,我可以看到您正在调用

draggedItem = getDraggedView();

在 onLongClick(View view) 事件中。getDraggedView() 可能会返回一个视图,该视图是您正在拖动的视图并且它不会显示在顶部(这只是一个猜测,因为您没有发布 getDraggedView() 代码)。如果是这种情况,您需要知道拖动视图的父级,例如父级是主窗口,那么您的整个布局将位于其之上。所以我最好的猜测是在你的布局中添加拖动的视图,因为它将是最后一个视图,它将放在顶部。

此外,我注意到你在打电话

mImage.getLayoutParams().height = displayHeight / 2;

在您的图像视图中以更改其高度。我不认为这是正确的方法,因为如果在第 10130 行查看 View 的源代码其中 setLayoutParams 代码是您会看到除了 layoutparams 集之外还有其他一些事情正在发生。但是我不知道这是否与您的问题有关。

我知道我的答案更多的是猜测而不是完整的答案,但根据问题的数据,这是我能做的最好的。

希望这可以帮助...

于 2013-07-10T11:47:52.077 回答
0

感谢所有的帮助。最后,bringToFront() 方法似乎对我不起作用。现在我可以通过使用从 ViewGroups 覆盖的 getChildDrawingOrder() 方法来解决我的问题。

使用此方法,您可以更改子项的绘制顺序,从而最后绘制您想要放置在所有其他项之上的项目。一个例子:

@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 是应该放在前面的变量。所以我用最后一个改变了这个变量的绘图位置......希望这对任何有同样问题的人有帮助:D

于 2013-07-15T09:14:56.510 回答
0

我尝试了接受的答案,但只要有未显示的子视图,就会不断崩溃。问题是参数childCounti指的是可见的孩子,而不是孩子的整个列表。该解决方案建立在该答案的基础上,并将此调整合并到索引中。

@Override
public int getChildDrawingOrder(int childCount, int i) {
   int firstVisiblePosition = getFirstVisiblePosition();
   if (mDragged < firstVisiblePosition || mDragged > getLastVisiblePosition())
      return super.getChildDrawingOrder(childCount, i);
   int adjustedDragged = mDragged - firstVisiblePosition;
   if (i == adjustedDragged)
       return childCount - 1;
   if (i == (childCount - 1))
      return adjustedDragged;
   return i;
}
于 2016-06-20T05:12:06.330 回答