Вопрос

I am creating a TutorialManager that handles all the user tutoring for app usage.

I have a ViewGroup that adds views to itself including a GridView that is filled with items after onMeasure() is called.

I am calling TutorialManagers method that's supposed to perform an action on the gridview but it needs to know when the gridview is filled in order to use any of it items

I added a callback method on the ViewGroup that is called right after all the view have been added

The problem is that the TutorialManager needs to know the X and Y coordinate of the views in order to highlight them using ShowCaseView

I've added a workaround using postDelayed(action, 100); which seems to work on my handset but what if on some device the view layout takes more than 100ms? It'll screw up the whole tutoring system.

I can't figure out any other way than creating a custom view that has a callback method which is called after the views position is known.

What's the common way of handling the issue that the laying view on screen is happening asynchronously?

Это было полезно?

Решение

You can try using an OnGlobalLayoutListener which fires whenever there is a layout change (Ie, your GridView and children have been added and measured. So where ever you initialize the GridView do this:

LayoutListener mLayoutListener = new LayoutListener;
mGridView.getViewTreeObserver().addOnGlobalLayoutListener(mLayoutListener);

private class LayoutListener implements OnGlobalLayoutListener {
    @Override
    public void onGlobalLayout() {
        ...Do your calculations here...

        mGridView.getViewTreeObserver().removeOnGlobalLayoutListener(this); //If you only need this to execute once.
    }
}

Note this will be called quite often. I'd recommend removing as soon as you don't need it.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top