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.

相关推荐
DeepAgent6 分钟前
AI Agent 工程实践(17):Agent 为什么需要可观测性(Observability)?
android·llm·agent
提笔了无痕12 分钟前
MySQL SQL 从 EXPLAIN 到索引优化,搞懂 SQL 为什么慢
android·sql·mysql
zhangphil39 分钟前
Android OAID是什么?有什么功用?
android
Android-Flutter3 小时前
android fragment 使用
android·kotlin
迷茫中的自我3 小时前
KMP全栈开发:从Android到AI Agent的技术演进与实践
android·人工智能
随遇丿而安3 小时前
第13周:页面状态保存 + 数据恢复优化
android
万事可爱^4 小时前
Claude 新发布的 Opus 5,系统提示语删了 80%,半价还能逼近 Fable 5
android·服务器·数据库·人工智能·claude
alexhilton5 小时前
响应式的Android身份验证架构
android·kotlin·android jetpack
喵都学不动了6 小时前
Android 自动化测试完全指南(新人版)
android
码云骑士6 小时前
76-全量微调vs-LoRA-vs-QLoRA-三种微调方式对比与选型
android