简体   繁体   English

使用x,y坐标查找布局

[英]Find layout with x,y coordinate

I have an app filled with custom buttons for Android. 我有一个装有自定义按钮的Android应用。 I would like to allow user to rearrange these buttons like image buttons of Home or Application panel. 我想允许用户重新排列这些按钮,例如“主页”或“应用程序”面板的图像按钮。

I researched on this and found out that I can use drag & drop functionality to interact with user's motion. 我对此进行了研究,发现可以使用拖放功能与用户的动作进行交互。 But in my case parent layout can be different. 但就我而言,父级布局可以不同。 OnMove or OnDrop event, I need to actually move that button in that corresponding layout. OnMove或OnDrop事件,我实际上需要在相应的布局中移动该按钮。

So question is how I can find a layout that contains coordinate x & y and put the button in it. 所以问题是我如何找到一个包含坐标x和y的布局并将按钮放进去。

@Override
public boolean onTouchEvent(MotionEvent event) {


    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
        status = START_DRAGGING;
        break;
    case MotionEvent.ACTION_UP:
        status = STOP_DRAGGING;
        break;
    case MotionEvent.ACTION_MOVE:
        if(status == START_DRAGGING){
            //parentLayout.setPadding((int)event.getRawX(), 0,0,0);
            //**What to do here**
            parentLayout.invalidate();
        }                           
        break;

    }

    return true;
}

You can loop through all the controls in the parent container and compare each child's bounds with the current X, Y. You can get a views bounds by calling this: 您可以遍历父容器中的所有控件,并将每个子对象的边界与当前X,Y进行比较。您可以通过调用以下方法获得视图边界:

View.getHitRect() View.getHitRect()

So something like this: 所以像这样:

for(View v : parent.children())
{
    // only checking ViewGroups (layout) obviously you can change
    // this to suit your needs
    if(!(v instanceof ViewGroup))
        continue;

    if(v.getHitRect().contains(x, y))
        return v;
}

This is just Psuedo-code and will need to be adapted for whatever you use is (ie adding recursion for nested controls). 这只是Psuedo代码,将需要根据您的使用情况进行调整(即为嵌套控件添加递归)。

Hope that helps. 希望能有所帮助。

I would suggest something to loop through the root XML and check the visible coordinates of any contained ViewGroups; 我建议通过根XML循环并检查任何包含的ViewGroups的可见坐标。 something like this, though this is untested: 像这样的东西,尽管这未经测试:

ViewGroup root = (ViewGroup)findViewById(R.id.id_of_your_root_viewgroup);
//get event coordinates as int x, int y

public ViewGroup findContainingGroup(ViewGroup v, int x, int y) {
    for (int i = 0; i < v.getChildCount(); i++) {
        View child = v.getChildAt(i);
        if(child instanceof ViewGroup) {
            Rect outRect = new Rect();
            child.getDrawingRect(outRect);
            if(outRect.contains(x, y)) return child;
        }
    }
}

ViewGroup parent = findContainingGroup(root, x, y);

I would suggest using a TableLayout . 我建议使用TableLayout As it's made of rows and columns, you can dynamically reorder them by inserting / deleting rows or columns and rebuilding your whole layout on the fly. 由于它是由行和列组成的,因此您可以通过插入/删除行或列并动态重建整个布局来动态地对其重新排序。

But that probably means you'd have to set you layout programmatically, I can see how you can do that from an XML layout. 但这可能意味着您必须以编程方式设置布局,我可以看到如何从XML布局中进行设置。

(The following is pseudo-code) (以下是伪代码)

if (dropping button) {
    calculate new layout based on which row/column button was moved, and where it was dropped;
    generate new layout (TableLayout --> addRow --> addView);
    apply it to buttons view (Buttons.setView(TableLayoutView));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM