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.

相关推荐
Meteors.8 分钟前
Android性能优化:01. 指标体系 + 分析工具链
android
jike_20261 小时前
安卓平台免费录音转文字工具深度测评:5 款 APP 功能对比与选型指南
android·智能电视
Code Man2 小时前
Windows 下使用 Appium
android·windows·appium
码农coding2 小时前
android 12 中的VSYNC接收
android
GitLqr2 小时前
Flutter 3.44 性能飞跃:深度解析 Android Platform View 的 HCPP 新特性
android·flutter·性能优化
码农coding2 小时前
android 12 SurfaceFlinger中的CompositionEngine
android
Mem0rin3 小时前
[MySQL] 聚合函数、分组查询、连接查询
android·mysql
码龙-DragonCoding4 小时前
一键批量提取音频、提取视频
android·音视频·提取
祉猷并茂,雯华若锦4 小时前
Win下完美解决Allure报错,生成Web自动化测试报告
android·python·selenium·自动化
IT小盘5 小时前
08-FastAPI加MySQL实现AI对话记录持久化
android·mysql·fastapi