(转)Android中动态添加view,删除view,获取view的内容

来源:互联网 发布:tp主人网络与访客网络 编辑:程序博客网 时间:2024/06/15 02:32

原地址:http://blog.csdn.net/hustlwz/article/details/52693768


最近在项目中碰到一个需求,在编辑一个红包详情页面的时候,需要动态的添加使用条件,并且可以删除,最后提交数据的时候需要获取到条件里面的数据。网上搜了一些答案没有比较满意的解决方法,所以写下此文。

效果图:


解决思路:

1.使用布局的addview方法,即可动态添加。

2.动态删除需要获取到当前条目在布局中的位置或者对象,然后用removeview方法删除。

3.提交服务器需要获取条目中的数据,可以通过维护一个hashmap集合来遍历获取其中的内容。

废话不多说了,上代码!

1.先上item的布局

[html] view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="51dp"  
  5.     android:id="@+id/ll_condition_edit_redbag2"  
  6.     android:orientation="vertical">  
  7.   
  8.   
  9.   
  10.     <LinearLayout  
  11.   
  12.         android:layout_width="match_parent"  
  13.         android:layout_height="50dp"  
  14.         android:background="@color/bg_white_color"  
  15.         android:layout_gravity="center_vertical"  
  16.         android:orientation="horizontal">  
  17.   
  18.         <TextView  
  19.             android:layout_width="wrap_content"  
  20.             android:layout_height="match_parent"  
  21.             android:layout_marginLeft="16dp"  
  22.             android:layout_marginRight="10dp"  
  23.             android:gravity="center_vertical"  
  24.             android:text="满"  
  25.             android:textColor="@color/text_black_color"  
  26.             android:textSize="16sp" />  
  27.   
  28.         <EditText  
  29.             android:id="@+id/ed_high"  
  30.             android:layout_width="50dp"  
  31.             android:layout_height="28dp"  
  32.             android:layout_centerVertical="true"  
  33.             android:background="@drawable/edit_bg_white"  
  34.             android:gravity="center"  
  35.             android:hint=""  
  36.             android:textColor="@color/text_dark_color"  
  37.             android:textColorHint="@color/text_hint_color"  
  38.             android:textSize="14sp" />  
  39.   
  40.         <TextView  
  41.             android:layout_width="wrap_content"  
  42.             android:layout_height="match_parent"  
  43.             android:layout_marginLeft="10dp"  
  44.             android:layout_marginRight="10dp"  
  45.             android:gravity="center_vertical"  
  46.             android:text="可使用"  
  47.             android:textColor="@color/text_black_color"  
  48.             android:textSize="16sp" />  
  49.   
  50.         <EditText  
  51.             android:id="@+id/ed_low"  
  52.             android:layout_width="50dp"  
  53.             android:layout_height="28dp"  
  54.             android:layout_centerVertical="true"  
  55.             android:background="@drawable/edit_bg_white"  
  56.             android:gravity="center"  
  57.             android:hint=""  
  58.             android:textColor="@color/text_dark_color"  
  59.             android:textColorHint="@color/text_hint_color"  
  60.             android:textSize="14sp" />  
  61.   
  62.         <TextView  
  63.             android:layout_width="wrap_content"  
  64.             android:layout_height="match_parent"  
  65.             android:layout_marginLeft="10dp"  
  66.             android:layout_marginRight="10dp"  
  67.             android:gravity="center_vertical"  
  68.             android:text="以下红包"  
  69.             android:textColor="@color/text_black_color"  
  70.             android:textSize="16sp" />  
  71.   
  72.         <LinearLayout  
  73.             android:layout_width="0dp"  
  74.             android:layout_height="match_parent"  
  75.             android:layout_weight="1"  
  76.             android:gravity="right|center_vertical">  
  77.   
  78.             <TextView  
  79.                 android:layout_width="wrap_content"  
  80.                 android:layout_height="match_parent"  
  81.                 android:layout_marginRight="10dp"  
  82.                 android:gravity="center_vertical"  
  83.                 android:text="|"  
  84.                 android:textSize="16sp" />  
  85.   
  86.             <ImageView  
  87.                 android:id="@+id/iv_del_item"  
  88.                 android:layout_width="wrap_content"  
  89.                 android:layout_height="match_parent"  
  90.                 android:layout_gravity="right"  
  91.                 android:paddingRight="16dp"  
  92.                 android:src="@drawable/icon_del"  
  93.   
  94.                 />  
  95.         </LinearLayout>  
  96.   
  97.     </LinearLayout>  
  98.     <ImageView  
  99.         android:layout_width="match_parent"  
  100.         android:layout_height="0.5dp"  
  101.         android:layout_alignParentEnd="true"  
  102.         android:layout_alignParentRight="true"  
  103.         android:layout_marginLeft="16dp"  
  104.         android:background="@color/text_hint_color" />  
  105.   
  106. </LinearLayout>  



2.把这个view插入到布局中,并放入到一个集合中,方便最后上传时使用

[java] view plain copy
  1. //1创建集合管理红包条件  
  2.     HashMap<Integer,LinearLayout> conditions = new HashMap<>();  
  3.     int itemId  =0;  
  4.     //红包使用条件的默认加入位置  
  5.     private int position = 7;  
  6.     private void createConditon() {  
  7.         //每次创建一个view  
  8.         LinearLayout ll_limit = (LinearLayout) View.inflate(this, R.layout.item_condition_edit_redbag, null);  
  9.         //删除按钮  
  10.         ImageView iv_del_item = (ImageView) ll_limit.findViewById(R.id.iv_del_item);  
  11.         //给每个删除条目绑定一个id  
  12.         iv_del_item.setTag(itemId);  
  13.         iv_del_item.setOnClickListener(new View.OnClickListener() {  
  14.             @Override  
  15.             public void onClick(View v) {  
  16.                 //获取绑定的id  
  17.                 int itemId = (int) v.getTag();  
  18.                 //布局中删除指定位置的view  
  19.                 ll_content_edit_redab2.removeView(conditions.get(itemId));  
  20.                 //集合中删除指定的item对象  
  21.                 conditions.remove(itemId);  
  22.                 //记录position最新位置  
  23.                 position--;  
  24.             }  
  25.         });  
  26.         //把当前对象保存到集合中  
  27.         conditions.put(itemId,ll_limit);  
  28.         ll_content_edit_redab2.addView(ll_limit, position);  
  29.         //每次添加以后,位置会变动  
  30.         position++;  
  31.         //每调用一次id++,防止重复  
  32.         itemId++;  
  33.     }  
代码注释已经很详细了,简单提下:我用position记录view在布局中的位置,用itemid记录绑定的view


3.上传的代码:

[java] view plain copy
  1. Set<Map.Entry<Integer, LinearLayout>> items = conditions.entrySet();  
  2.         //转换为迭代器  
  3.         Iterator iter = items.iterator();  
  4.         while (iter.hasNext()) {  
  5.             Map.Entry entry = (Map.Entry) iter.next();  
  6.             LinearLayout val = (LinearLayout) entry.getValue();  
  7.             EditText ed_high = (EditText) val.findViewById(R.id.ed_high);  
  8.             EditText ed_low = (EditText) val.findViewById(R.id.ed_low);  
  9.             String s1 = ed_high.getText().toString().trim();  
  10.             String s2 = ed_low.getText().toString().trim();  
  11.             if (TextUtils.isEmpty(s1) &&TextUtils.isEmpty(s2)){  
  12.                 Toast.makeText(this,"限制条件填写不完整",Toast.LENGTH_SHORT).show();  
  13.                 return;  
  14.             }  
  15.             limits.add(new RedbagLimit(s1,s2));  
  16.         }  
  17.         final String s = new Gson().toJson(limits);  
  18.         LogUtil.d("limit",s);  
[java] view plain copy
  1. <pre style="font-family: 宋体; font-size: 10.5pt; background-color: rgb(255, 255, 255);"><pre name="code" class="java">List<RedbagLimit> limits = new ArrayList<>();  
  2. class RedbagLimit{  
  3.    String price ;  
  4.    String useable;  
  5.    RedbagLimit(String price ,String useable){  
  6.       this.price = price;  
  7.       this.useable = useable;  
  8.    }  
  9. }  


因为上传的时候需要封装成一个数组,所以我先遍历获取数据再封装到集合中,通过gson转换成字符串。

第一次发文,有不对的地方请指正!


原创粉丝点击