force onLayout in a subView whose dimensions doesn

2019-06-20 19:07发布

I have a Custom ViewGroup with some views inside.

In response to an event, a requestLayout is called and in OnLayout some of the views will get a new layout position.

One of them maintain its position but needs to be rearranged inside. However this view's onlayout method will not be called since its layout position doesn't change. Trying to call requestLayout, forceLayout or invalidate in this view doesn't work.

The only dirty trick that I have found is to change some of its value position to force the layout change, but I think there should be a better way. So, by now I do something (horrible) like:

    int patch = 0;
    @Override protected void onLayout(boolean changed, int l, int t, int r, int b) {
        ...
        _myView.patchedLayout(patch);
        _myview.layout(l1, t1 , r1, t1+patch);
        patch = (patch+1) % 2;
        ...
    }       

Any way to get the same result in a better way?

1条回答
戒情不戒烟
2楼-- · 2019-06-20 19:50

I finally got the solution: I need to override onMeasure and be sure to call mesure in my view:

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    ...
    _myview.measure(widthMeasureSpec, heightMeasureSpec);
    ...

It will set the LAYOUT_REQUIRED flag in the view's private field mPrivateFlags so it will force to call onLayout

查看更多
登录 后发表回答