Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

force onLayout in a subView whose dimensions doesn't change

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?

like image 266
Luis Avatar asked Jul 05 '12 11:07

Luis


1 Answers

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

like image 132
Luis Avatar answered Oct 29 '22 11:10

Luis