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 小时前
鸿蒙开发:实现文本渐变效果
android·ios·harmonyos
光头闪亮亮4 小时前
Fyne ( go跨平台GUI )项目实战-项目开发必备基础知识(中)
android·c++·go
光头闪亮亮4 小时前
Fyne ( go跨平台GUI )项目实战-项目开发必备基础知识(上)
android·sqlite·go
光头闪亮亮4 小时前
Fyne ( go跨平台GUI )项目实战-项目开发必备基础知识(下)
android·c++·go
Ch_champion4 小时前
2018年之前的Android 项目上使用的技术点(及第三方库)
android
龚礼鹏4 小时前
RK Android16 wifi 投屏失败问题排查
android
晓梦林7 小时前
[ACTF2020 新生赛]BackupFile学习笔记
android·笔记·学习
ITmaster07317 小时前
告别 IDE?Android CLI 来了,开发进入 AI Agent 时代
android·ide·人工智能
aaajj8 小时前
【Android】获取设备音频
android
清泓y9 小时前
UE移动开发技术面试题
android·面试·ue5·ue4·游戏程序