ViewStub

1.作用

1.性能优化:

用到的时候再去加载,不调用加载的话,不会显示

2.使用

1.xml

其中hecate_listview2x为里面加载的布局

xml 复制代码
<ViewStub
        android:id="@+id/hecate1_recycler_vs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout="@layout/hecate_listview2x"
        />

2.Java代码

  1. 一定要先设置加载监听setOnInflateListener,再调用加载inflate(),不然会报错
kotlin 复制代码
class MainActivity : ComponentActivity() {
    private lateinit var mainBinding: ActivityMainBinding
    private lateinit var hecateListview2xBinding: HecateListview2xBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mainBinding= ActivityMainBinding.inflate(layoutInflater)
        setContentView(mainBinding.root)
        //viewStub加载布局的监听方法
        mainBinding.hecate1RecyclerVs.setOnInflateListener { viewStub, view ->
            hecateListview2xBinding= HecateListview2xBinding.bind(view)//viewbinding绑定viewStub里面的布局
        }
        //viewStub加载
        mainBinding.hecate3RecyclerVs.inflate()
        //对viewStub加载布局里面的view方法的调用
        hecateListview2xBinding.textView.setOnClickListener {
            Toast.makeText(
                this,
                "我是textView",
                Toast.LENGTH_SHORT
            ).show() }
    }


}

3.总结

4.遇到的问题

1.一定要先设置加载监听setOnInflateListener,再调用加载inflate(),不然会报错

2.

3.

4.

5.

6.

相关推荐
alexhilton7 小时前
Android XR 开发入门完全指南
android·kotlin·android jetpack
2501_9159090612 小时前
全面解析iOS应用上架到App Store的完整流程与关键注意事项
android·macos·ios·小程序·uni-app·cocoa·iphone
淡淡的香烟14 小时前
AndroidAI使用心得
android
chjif15 小时前
Android 设备管控开发实战:无障碍如何准确识别当前前台 App?
android
chjif15 小时前
Android 设备管控开发实战:企业受控终端的 DNS 域名策略实现
android·华为·harmonyos
c2385617 小时前
MySQL 基础用法(下):查询进阶与核心特性
android·c语言·c++·mysql
math_hongfan17 小时前
鸿蒙Agent高级技能自定义开发:自定义意图服务/能力注册/Agent技能编排/服务插件开发实战
android·学习·华为·harmonyos·鸿蒙
2501_9151063218 小时前
iOS 证书类型及作用说明 账号证书与签名配置的完整解读
android·ios·小程序·https·uni-app·iphone·webview
九皇叔叔18 小时前
RHEL9 安装 MySQL 8.4.11.LTS 版本
android·mysql·adb
海兰18 小时前
【数据库】tdsql(MySQL )的事务隔离级别
android·数据库·mysql