Android 等待view 加载布局完成 (包括动态生成View)

前言 

在实际开发中,有很多组件需要 根据数据,动态生成,或者 追加 / 减少 子view,由于View布局需要时间,此时想要获取父View的最新宽高值,要么手动测量,要么等待布局完成后再获取

ps:如果使用View树监听观察方法,只调用一次 也是拿不到父View最新值宽高值的。

    private boolean initFlag = false; // 初始化,确保只执行一次

    public BaseView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                if (!initFlag) {
                    fistInit();
                    initFlag = true;
                }
            }
        });
    }

    // 只执行一次的方法
    public void fistInit() {}

1、案例:因为View布局间隔时间,导致当前父View宽高 和 布局完成后父View宽高不同

    @Override
    public void onWindowFocusChanged(boolean hasWindowFocus) {
        super.onWindowFocusChanged(hasWindowFocus);
        LinearLayout testBox2 = (LinearLayout) findViewById(R.id.test_box);

        Log.d("TAG", "AAA getMeasuredHeight:" + testBox2.getMeasuredHeight());

        for (int i = 0; i < 2; i++) {
            TextView textView = new TextView(getContext());
            textView.setBackgroundColor(0x14F9230A);
            textView.setGravity(Gravity.CENTER);
            textView.setText("测试 -- 遍历生成的");
            testBox2.addView(textView);
        }

        Log.d("TAG", "BBB getMeasuredHeight:" + testBox2.getMeasuredHeight());

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                Log.d("TAG", "CCC getMeasuredHeight:" + testBox2.getMeasuredHeight());
            }
        }, 1000);

    }

2、解决方案

2.1 方式一:通过 通知系统重新测量,获取最新的值。
2.2 方式二:当前组件执行post方法,从主线程 重新获取值,post这种属于等待布局完成后 执行。

ps:方式一,是在View没有布局完成就可以拿到最新的值,获取值的速度最快;方式二,post 是等待View布局完成后 执行;根据业务需求自行选择。

    @Override
    public void onWindowFocusChanged(boolean hasWindowFocus) {
        super.onWindowFocusChanged(hasWindowFocus);
        LinearLayout testBox2 = (LinearLayout) findViewById(R.id.test_box);

        Log.d("TAG", "AAA getMeasuredHeight:" + testBox2.getMeasuredHeight());

        for (int i = 0; i < 2; i++) {
            TextView textView = new TextView(getContext());
            textView.setBackgroundColor(0x14F9230A);
            textView.setGravity(Gravity.CENTER);
            textView.setText("测试 -- 遍历生成的");
            testBox2.addView(textView);
        }

        Log.d("TAG", "BBB getMeasuredHeight:" + testBox2.getMeasuredHeight());

        // 方式一:通过 通知系统重新测量,获取最新的值
        testBox2.measure(0, 0);
        Log.d("TAG", "CCC getMeasuredHeight:" + testBox2.getMeasuredHeight());

        // 方式二:当前组件执行post方法,从主线程 重新获取值,post这种属于等待布局完成后 执行
        testBox2.post(new Runnable() {
            @Override
            public void run() {
                Log.d("TAG", "DDD getMeasuredHeight:" + testBox2.getMeasuredHeight());
            }
        });
        // 或者 从根视图也可以
        // getRootView().post(new Runnable() {
        //    @Override
        //    public void run() {
        //        Log.d("TAG", "DDD getMeasuredHeight:" + testBox2.getMeasuredHeight());
        //    }
        // });

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                Log.d("TAG", "EEE getMeasuredHeight:" + testBox2.getMeasuredHeight());
            }
        }, 1000);

    }

相关推荐

  1. AndroidView&Adapter

    2023-12-09 18:18:01       31 阅读
  2. View->View测量布局中requestLayout和forceLayout的区别

    2023-12-09 18:18:01       8 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-09 18:18:01       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-09 18:18:01       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-09 18:18:01       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-09 18:18:01       20 阅读

热门阅读

  1. vue图片预览 90度旋转

    2023-12-09 18:18:01       34 阅读
  2. Verilog中generate的用法

    2023-12-09 18:18:01       36 阅读
  3. 浅谈Elasticsearch备份和恢复

    2023-12-09 18:18:01       28 阅读
  4. 计算机网络知识点合集【王道计算机考研】

    2023-12-09 18:18:01       36 阅读
  5. 层三交换机解析(Layer 3 Switch)层3交换机

    2023-12-09 18:18:01       35 阅读
  6. 【npm】npm中classnames包是干嘛的

    2023-12-09 18:18:01       38 阅读
  7. CVE-2002-20001处理方法

    2023-12-09 18:18:01       39 阅读
  8. Oracle数组循环表存在则删除

    2023-12-09 18:18:01       44 阅读