to use in data binding. * @param activity The Activity whose content View should change. * @param layoutId The resource ID of the layout to be inflated, bound, and set as the * Activity's content. * @return The binding associated with the inflated content view. */ public static <T extends ViewDataBinding> T setContentView(Activity activity, int layoutId, DataBindingComponent bindingComponent) { // 设置ContentView activity.setContentView(layoutId); // 获取View实例 View decorView = activity.getWindow().getDecorView(); // 查找内容 ViewGroup contentView = (ViewGroup) decorView.findViewById(android.R.id.content); // 返回绑定后添加的View return bindToAddedViews(bindingComponent, contentView, 0, layoutId); } 3.最后我们会发明他经由过程校验节点数量去绑定并添加View返回
- // 办法名键明其意 绑定并添加到View
- private static <T extends ViewDataBinding> T bindToAddedViews(DataBindingComponent component,
- ViewGroup parent, int startChildren, int layoutId) { // 获取到parent下子节点数量
- final int endChildren = parent.getChildCount(); // 校验其下节点数量
- final int childrenAdded = endChildren - startChildren; if (childrenAdded == 1) { // 如不雅只有一个节点,获取实例并添加绑定返回即可
- final View childView = parent.getChildAt(endChildren - 1); return bind(component, childView, layoutId);
- } else
推荐阅读
郑州,为智慧城市升级提供模板
产学研一体化,催生聪明应用模式
“郑州大年夜六、七年前就开端做数字城市,然则它的数据信息是静止的,二维的。和数字城市比拟,聪明城市请求是可测量、可感知、可分析、可视化的,也>>>详细阅读
本文标题:Android Study之findViewById变迁之路
地址:http://www.17bianji.com/lsqh/35987.html
1/2 1