使用View Binding,你不需要手动调用findViewById,而是可以直接通过生成的绑定类来访问视图。 Jetpack Compose:Jetpack Compose是一个用于构建原生UI的现代声明式UI工具包。它允许你使用Kotlin代码来定义UI,而不是使用XML布局文件。Compose提供了更简洁、更灵活的UI构建方式,并且完全避免了findViewById的使用。 3. 展示...
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { mContentView = inflater.inflate(layoutId, container, false) return mContentView } override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedIns...
private HashMap _$_findViewCache; protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.setContentView(1300009); TextView var10000 = (TextView)this._$_findCachedViewById(id.tv_demo); Intrinsics.checkExpressionValueIsNotNull(var10000, "tv_demo")...
可以看到, fragment 里面跟前面的基本原理类似,同样也是在findCachedViewById()中创建缓存 Map,区别在于 fragment 里面是通过getView()来findViewById()的,如果是在onCreateView方法里使用控件 ID,这个时候getView()会返回null,即var10000为null,这样findCachedViewById()就返回空了。 因此,千万要注意 fragment 里面...