android动态添加布局(基于mvvm和databinding)

本文介绍了在Android中动态添加布局的方法,特别是结合MVVM和DataBinding框架的应用。通过一个示例展示了如何根据后台返回的天数动态生成相应数量的布局,包括使用RecyclerView的详细实现,如封装的viewCtrl、子布局文件以及数据model的设计。此外,还提到了直接添加布局的另一种方式。
摘要由CSDN通过智能技术生成

android 常常需要动态添加布局例如:
这里写图片描述
这里写图片描述
需要后台返回的天数个数来动态添加布局,有一下几种写法:

方式1。 基于recycleView
recyclerView使用了基于mvvm和databinding的框架github地址

xml布局,通过后台返回天数的数量daySize显示多少个布局

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <data>
        <import type="me.tatarka.bindingcollectionadapter2.LayoutManagers"/>
        <variable
            name="viewCtrl"
            type="com.example.example.dynamicAddLayout.DynamicViewCtrl"/>
    </data>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
        <LinearLayout
            android:id="@+id/layout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_gravity="center_horizontal">
        </LinearLayout>
       <android.support.v7.widget.RecyclerView
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_gravity="center_horizontal"
           android:gravity=
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值