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.

相关推荐
时光呀时光慢慢走2 分钟前
MAUI 开发安卓 MQTT 客户端:实现远程控制 (完整源码 + 避坑指南)
android·物联网·mqtt·c#
成都大菠萝20 分钟前
2-2-44 快速掌握Kotlin-函数类型操作
android
有位神秘人1 小时前
Android中获取设备里面的音频文件
android
2501_915918411 小时前
使用 HBuilder 上架 iOS 应用时常见的问题与应对方式
android·ios·小程序·https·uni-app·iphone·webview
farewell-Calm2 小时前
01_Android快速入门
android
helloCat2 小时前
记录CI/CD自动化上传AppGallery遇到的坑
android·前端·api
WordPress学习笔记3 小时前
wordpress根据页面别名获取该页面的链接
android·wordpress
2501_916007473 小时前
iOS 崩溃日志的分析方法,将崩溃日志与运行过程结合分析
android·ios·小程序·https·uni-app·iphone·webview
浅箬4 小时前
Taro3的H5项目在Android、IOS 中因为兼容性问题导致的白屏问题
android·团队开发·taro
技术摆渡人4 小时前
Android 全栈架构终极指南:从 Linux 内核、Binder 驱动到 Framework 源码实战
android·linux·架构