Android 动态生成复杂的布局(View布局的子控件含自己的点击事件)

本文介绍了如何在Android中动态生成包含点击事件的复杂布局,以满足工作计划模板的需求。避免使用ListView带来的复用问题,通过自定义View和LinearLayout作为容器,实现每个Item的独立控制和更新。文中提供了关键代码示例和思路总结。
摘要由CSDN通过智能技术生成

有时候会遇到一些工作计划的需求,大多数工作计划都会有统一的模板,即计划内容分类是一致的。

比如说周计划管理:每周都有7天,对于我们来说就会有两个方案:1、画7个相同布局 2、动态生成7个布局

之前有想过直接用ListView来做,其实也是可以的,但是涉及到一些复杂的需求,例如I:tem的子控件加点击事件,并通过该事件引发一系列操作刷新此Item内容,获取输入框内部内容等,ListView的劣势就体现出来,ListView是复用同一个ItemView,刷新的同时position的控制问题总是一个BUG。所以没有考虑用ListView来做。

借用ListView的思想,我们是不是可以创建一个满足工作计划复杂性的View,并动态获取创建,赋予丰富的ID属性,以及点击事件,答案是可以的。

下面是关键代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
        <LinearLayout
            android:id="@+id/month_mactivitys_ll_province"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_marginTop="10dp"
            android:background="@color/white"
            android:padding="10dp" >

            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:ems="6"
                android:text="省份:"
                android:textColor="@color/unread"
                android:textSize="@dimen/main_text_size" />

            <TextView
                android:id="@+id/month_mactivitys_tv_province"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:ems="10"
                android:hint="请选择省份"
                android:textColor="@color/unread"
                android:textSize="@dimen/main_text_size" />

               <!--  <requestFocus /> -->
        </LinearLayout>

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="0.5dp"
            android:layout_marginLeft="5dp"
            android:background="@color/listview_divider" />

        <LinearLayout
            android:id="@+id/month_mactivitys_ll_startTime"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:background="@color/white"
            android:padding="10dp" >

            <TextView
                android:layout_width="wrap_content"
              
评论 25
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值