Android开发 View的生命周期结合代码详解

咱们以TextView控件为例:

/**
* Created by SunshineBoy on 2020/9/23.
*/
public class TestTextView extends android.support.v7.widget.AppCompatTextView {
public TestTextView(Context context) {
super(context);
Log.e("TestTextView","TestTextView(Context context)");
}

public TestTextView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
Log.e("TestTextView","TestTextView(Context context, @Nullable AttributeSet attrs)");
}

public TestTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}

@Override
protected void onVisibilityChanged(View changedView, int visibility) {
super.onVisibilityChanged(changedView, visibility);
Log.e("TestTextView","onVisibilityChanged");
}

@Override
protected void onFinishInflate() {
super.onFinishInflate();
Log.e("TestTextView","onFinishInflate");
}

@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
Log.e("TestTextView","onAttachedToWindow");
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
Log.e("TestTextView","onMeasure");
}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
Log.e("TestTextView","onSizeChanged");
}

@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
Log.e("TestTextView","onLayout");
}

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Log.e("TestTextView","onDraw");
}

@Override
protected void onWindowVisibilityChanged(int visibility) {
super.onWindowVisibilityChanged(visibility);
Log.e("TestTextView","onWindowVisibilityChanged="+visibility);
}

@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
Log.e("TestTextView","onDetachedFromWindow");
}
}

先看看默认状态下生命周期:

2020-09-23 17:34:40.882 15132-15132/com.freddy.chat E/TestTextView: TestTextView(Context context, @Nullable AttributeSet attrs)
2020-09-23 17:34:40.882 15132-15132/com.freddy.chat E/TestTextView: onFinishInflate
2020-09-23 17:34:40.927 15132-15132/com.freddy.chat E/TestTextView: onAttachedToWindow
2020-09-23 17:34:40.927 15132-15132/com.freddy.chat E/TestTextView: onWindowVisibilityChanged=0
2020-09-23 17:34:40.927 15132-15132/com.freddy.chat E/TestTextView: onVisibilityChanged
2020-09-23 17:34:40.928 15132-15132/com.freddy.chat E/TestTextView: onMeasure
2020-09-23 17:34:41.056 15132-15132/com.freddy.chat E/TestTextView: onMeasure
2020-09-23 17:34:41.056 15132-15132/com.freddy.chat E/TestTextView: onSizeChanged
2020-09-23 17:34:41.056 15132-15132/com.freddy.chat E/TestTextView: onLayout
2020-09-23 17:34:41.065 15132-15132/com.freddy.chat E/TestTextView: onDraw

接下来看看setVisibility(View.INVISIBLE)状态:

2020-09-23 17:37:14.191 15132-15132/com.freddy.chat E/TestTextView: onVisibilityChanged

再看看setVisibility(View.VISIBLE)状态:

2020-09-23 17:38:07.338 15132-15132/com.freddy.chat E/TestTextView: onVisibilityChanged
2020-09-23 17:38:07.535 15132-15132/com.freddy.chat E/TestTextView: onDraw

再看看离开Activity挂起状态:

2020-09-23 17:39:33.128 15132-15132/com.freddy.chat E/TestTextView: onWindowVisibilityChanged=8
2020-09-23 17:39:33.267 15132-15132/com.freddy.chat E/TestTextView: onVisibilityChanged

然后Activity挂起恢复:

2020-09-23 17:40:32.178 15132-15132/com.freddy.chat E/TestTextView: onWindowVisibilityChanged=4
2020-09-23 17:40:32.361 15132-15132/com.freddy.chat E/TestTextView: onVisibilityChanged
2020-09-23 17:40:32.414 15132-15132/com.freddy.chat E/TestTextView: onWindowVisibilityChanged=0
2020-09-23 17:40:32.551 15132-15132/com.freddy.chat E/TestTextView: onDraw

最后再看看Activity销毁状态:

2020-09-23 17:41:31.610 15132-15132/com.freddy.chat E/TestTextView: onWindowVisibilityChanged=8
2020-09-23 17:41:31.837 15132-15132/com.freddy.chat E/TestTextView: onDetachedFromWindow

由上可知View的生命周期是:

    首先创建带两个参数的构造函数;然后从xml文件中inflate完成后调onFinishInflate方法;然后在Activity执行onResume之后依附Activity窗口调onAttachedToWindow方法;然后调窗口状态发生改变调onWindowVisibilityChanged方法;然后调view的状态发生改变调onVisibilityChanged方法;然后开始测量view尺寸调onMeasure方法;然后测量结果与上次测量结果不一致时调onSizeChanged方法;然后为子view分配位置调onLayout方法;然后view渲染绘制内容细节调onDraw方法;最后Activity销毁时调onDetachedFromWindow方法。

原创:https://www.panoramacn.com
源码网提供WordPress源码,帝国CMS源码discuz源码,微信小程序,小说源码,杰奇源码,thinkphp源码,ecshop模板源码,微擎模板源码,dede源码,织梦源码等。

专业搭建小说网站,小说程序,杰奇系列,微信小说系列,app系列小说

Android开发 View的生命周期结合代码详解

免责声明,若由于商用引起版权纠纷,一切责任均由使用者承担。

您必须遵守我们的协议,如果您下载了该资源行为将被视为对《免责声明》全部内容的认可-> 联系客服 投诉资源
www.panoramacn.com资源全部来自互联网收集,仅供用于学习和交流,请勿用于商业用途。如有侵权、不妥之处,请联系站长并出示版权证明以便删除。 敬请谅解! 侵权删帖/违法举报/投稿等事物联系邮箱:2640602276@qq.com
未经允许不得转载:书荒源码源码网每日更新网站源码模板! » Android开发 View的生命周期结合代码详解
关注我们小说电影免费看
关注我们,获取更多的全网素材资源,有趣有料!
120000+人已关注
分享到:
赞(0) 打赏

评论抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

您的打赏就是我分享的动力!

支付宝扫一扫打赏

微信扫一扫打赏