Android get location of dynamicly added views

邮差的信 提交于 2019-12-11 09:43:39

问题


In my application in onCreate() method I need add to TableLayout certain number of rows and buttons for each row (for example 5x5, which means that I must add 5 rows and 5 buttons for each row) then I get something like square. This number I get from previous activity by intent.

Then I need change size of buttons to maximum, depending on TableLayout width (which I get by ViewTreeObserver), which is mach_parent. So size of each button will be btnWidth = btnHeight = (int) tlWidth / n.

After that all I need add each button to array and get it position, but when I'm trying get position by getLocationInWindow() method and I get the same position for each button - position of TableLayout. When I'm invocate getLeft() or getTop and getX() or getY() it always return 0. This happens even in onWindowFocusChanged() method!

Here is code, I had wrote In OnCreate

Intent intent = getIntent();
    gridSize = intent.getIntExtra("grid size", 5); //number of buttons I need add
    //Log.d("myLogs", "grid size is " + gridSize);

    corners = new BtnLocation[gridSize][gridSize]; //BtnLocation - object, which holds coordinates of top left corner and bottom right
    cells = new Button[gridSize][gridSize]; //holds each button

    grid = (TableLayout) findViewById(R.id.grid);
    grid.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {

        @SuppressLint("NewApi")
        @Override
        public void onGlobalLayout() {
            ViewTreeObserver observer = grid.getViewTreeObserver();
            final int btnSize = (int) grid.getWidth()/gridSize;

            for (int i = 0; i < gridSize; i++) {
                TableRow tr = createRow();
                tr.setGravity(Gravity.CENTER_HORIZONTAL);
                grid.addView(tr);

                for (int j = 0; j < gridSize; j++) {
                    Button btn = createButton();
                    btn.setId(gridSize*i+j);
                    tr.addView(btn);

                    // setting layout params
                    ViewGroup.LayoutParams params = btn.getLayoutParams();
                    params.width = btnSize;
                    params.height = btnSize;
                    btn.setLayoutParams(params);

                    cells[i][j] = btn;

                    int[] location = new int[2];
                    grid.getLocationInWindow(location);
                    Log.d("myLogs", Arrays.toString(location)); 
                    Log.d("myLogs", btn.getLeft() + " " + btn.getTop()); //always write two zeroes
                }
            }

            int[] location = new int[2];
            grid.getLocationInWindow(location);
            Log.d("myLogs", Arrays.toString(location));

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                   observer.removeOnGlobalLayoutListener(this);
               } else {
                   observer.removeGlobalOnLayoutListener(this);
            }
        }
    });
}

In onWindowFocusChanged()

for (int i = 0; i < cells.length; i++) {
        for (int j = 0; j < cells.length; j++) {
            int[] location = new int[2];
            grid.getLocationInWindow(location);
            Log.d("myLogs", Arrays.toString(location));
            Log.d("myLogs", cells[i][j].getLeft() + " " + cells[i][j].getTop());
        }
    }

Also I add methods to create new Button and TableRow because I couldn't invoce them in onGlobalLayout() method because of errors

public TableRow createRow(){
    return new TableRow(this);
}

public Button createButton(){
    return new Button(this);
}

来源:https://stackoverflow.com/questions/31264054/android-get-location-of-dynamicly-added-views

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!