Why is requestLayout being called directly after invalidate

后端 未结 4 878
庸人自扰
庸人自扰 2020-12-09 18:27

I\'m learning about custom views and wanted to learn about invalidate() and requestLayout().

Please refer to this answer and its diagram:

4条回答
  •  孤城傲影
    2020-12-09 19:03

    After seeing the following diagram, I was under the impression that calling requestLayout() would eventually result in an onDraw.

    Therefore, there would be no need to call these together because it would be redundant.

    invalidate();
    requestLayout();
    

    However, it turns out that that diagram is misleading. Some views might in fact invalidate themselves when there is a layout change, but this is not a certainty. Calling requestLayout() is not guaranteed to result in onDraw being called.

    My source (thanks to this comment) is the Romain Guy (who is an Android engineer at Google):

    requestLayout() itself does not lead to a draw pass but some views might react to a Layout change by calling invalidate.

    Therefore, to be certain a relayout will result in a redraw, then you should pair an invalidate() with the requestLayout(). (The opposite is not true, though. If you only need a redraw, then there is no need to call requestLayout(). A single invalidate() will do.)

提交回复
热议问题