force onLayout in a subView whose dimensions doesn't change

醉酒当歌 提交于 2020-01-03 13:02:49

问题


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:


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



来源:https://stackoverflow.com/questions/11343500/force-onlayout-in-a-subview-whose-dimensions-doesnt-change

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