动态添加重复布局以及各子布局点击事件
来源:互联网 发布:寂静岭知乎电影 编辑:程序博客网 时间:2024/05/23 16:57
要求实现一个可以一次创建多个班级的功能,效果如下图:
其中班级名和专业是一个可以复用的子view,直接使用View addView = LayoutInflater.from(CreateClassActivity.this).inflate(R.layout.add_class_view, null);
parent.addView(addView);总是会出现一个问题,不管点选哪一个子view的专业都改变的是最后一个。出现这个问题的原因就是没有区别开view的id。借鉴http://blog.csdn.net/qq_21451721/article/details/51613513 这篇博客仅以记录。
容器布局:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <RelativeLayout android:id="@+id/rl_title" android:layout_width="match_parent" android:layout_height="@dimen/base50dp" android:background="@drawable/retangle_white"> <include android:id="@+id/back" layout="@layout/back_button" /> <TextView android:id="@+id/tv_title" style="@style/tv_contact_title" android:text="创建班级" /> </RelativeLayout> <ScrollView android:id="@+id/add_class_dialog" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@+id/bt_add_class" android:layout_below="@+id/rl_title"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center_vertical" android:orientation="vertical"> <!-- 装重复动态布局的容器--> <LinearLayout android:id="@+id/parent" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> </LinearLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="@dimen/base60dp" android:layout_alignParentRight="true" android:layout_marginTop="@dimen/base5dp"> <View android:id="@+id/v_delete_class" android:layout_width="@dimen/base35dp" android:layout_height="@dimen/base35dp" android:layout_centerVertical="true" android:layout_marginRight="@dimen/base20dp" android:layout_toLeftOf="@+id/v_add_class" android:background="@drawable/delete_mem" /> <View android:id="@+id/v_add_class" android:layout_width="@dimen/base35dp" android:layout_height="@dimen/base35dp" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:layout_marginRight="@dimen/base20dp" android:background="@drawable/add_mem" /> </RelativeLayout> </LinearLayout> </ScrollView> <Button android:id="@+id/bt_add_class" style="@style/button_background" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:layout_marginBottom="@dimen/base22dp" android:text="确定" /></RelativeLayout>
重复使用布局:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/add_class_add_view" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center_vertical" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="@dimen/base10dp" android:orientation="horizontal" android:paddingLeft="@dimen/base20dp"> <TextView android:layout_width="0dp" android:layout_height="@dimen/base40dp" android:layout_weight="1" android:gravity="center" android:text="班级名:" android:textColor="#000000" android:textSize="18sp" /> <EditText android:id="@+id/create_class_et_class_name" android:layout_width="0dp" android:layout_height="@dimen/base40dp" android:layout_marginRight="@dimen/base20dp" android:layout_weight="3" android:background="@drawable/background_tv_name" android:ellipsize="end" android:gravity="center_vertical" android:hint="请输入班级名称" android:singleLine="true" android:textColor="#000000" android:textSize="16sp" /> </LinearLayout> <LinearLayout android:id="@+id/create_class_ll_pro_name" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="@dimen/base10dp" android:orientation="horizontal" android:paddingLeft="@dimen/base20dp"> <TextView android:layout_width="0dp" android:layout_height="@dimen/base40dp" android:layout_weight="1" android:gravity="center" android:text="专业:" android:textColor="#000000" android:textSize="18sp" /> <RelativeLayout android:layout_width="0dp" android:layout_height="@dimen/base40dp" android:layout_marginRight="@dimen/base20dp" android:layout_weight="3" android:background="@drawable/background_tv_name" android:gravity="center_vertical"> <TextView android:id="@+id/create_class_tv_pro_name" android:layout_width="match_parent" android:layout_height="match_parent" android:ellipsize="end" android:gravity="center_vertical" android:hint="请输入" android:singleLine="true" android:textColor="#000000" android:textSize="16sp" /> <View android:layout_width="10dp" android:layout_height="10dp" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:layout_gravity="bottom" android:layout_marginBottom="@dimen/base15dp" android:background="@drawable/select_right_down" /> </RelativeLayout> </LinearLayout></LinearLayout>
初始化:
private List<View> viewList; private List<ViewHolder> viewHolderList; private int mark = 0; private String classNames = ""; private String proIds = ""; protected void initView() { viewList = new ArrayList<>(); viewHolderList = new ArrayList<>(); View addView = LayoutInflater.from(CreateClassActivity.this).inflate(R.layout.add_class_view, null); addView.setId(mark); parent.addView(addView, mark); getViewInstance(addView); } private void getViewInstance(View addView) { ViewHolder vh = new ViewHolder(); vh.id = addView.getId(); vh.create_class_et_class_name = (EditText) addView.findViewById(R.id.create_class_et_class_name); vh.create_class_ll_pro_name = (LinearLayout) addView.findViewById(R.id.create_class_ll_pro_name); vh.create_class_tv_pro_name = (TextView) addView.findViewById(R.id.create_class_tv_pro_name); // 设置监听 vh.create_class_ll_pro_name.setOnClickListener(selectProListener); viewHolderList.add(vh); viewList.add(addView); } View.OnClickListener selectProListener = new View.OnClickListener() { @Override public void onClick(View v) { View view = (View) v.getParent(); for (int i = 0; i < parent.getChildCount(); i++) { ViewHolder viewHolder = viewHolderList.get(i); DebugUtil.E("createclass", "view.getId()==" + view.getId() + " viewHolder.id==" + viewHolder.id); if (view.getId() == viewHolder.id) { onProfession(App.sp.getString(GlobalConstants.ORGID, ""), App.sp.getString(GlobalConstants.SCHID, ""), viewHolder.create_class_tv_pro_name, viewHolder.id); } } } };
public class ViewHolder { private int id = -1; private EditText create_class_et_class_name; private LinearLayout create_class_ll_pro_name; private TextView create_class_tv_pro_name; }
添加删除事件:
mark++; View addView = LayoutInflater.from(CreateClassActivity.this).inflate(R.layout.add_class_view, null); addView.setId(mark); parent.addView(addView); getViewInstance(addView);
int childNum = parent.getChildCount(); if (childNum > 1) { parent.removeViewAt(childNum - 1); viewHolderList.remove(viewHolderList.size() - 1); viewList.remove(viewList.size() - 1); mark--; }
最后是保存,保存的时候循环遍历就行:
for (int i = 0; i < parent.getChildCount(); i++) { ViewHolder viewHolder = viewHolderList.get(i); String className = viewHolder.create_class_et_class_name.getText().toString().trim(); String proName = viewHolder.create_class_tv_pro_name.getText().toString().trim(); String proId = (String) viewHolder.create_class_tv_pro_name.getTag(); DebugUtil.E("addClass", "className==" + className); DebugUtil.E("addClass", "proName==" + proName); DebugUtil.E("addClass", "proId==" + proId); }
最后,需要说明的是,动态添加的子布局中需要有点击事件的布局必须放在第二层布局中,否则通过view.getParent()得到的就和原本存储的父布局不是同一个,会造成点击事件没有响应。
0 0
- 动态添加重复布局以及各子布局点击事件
- Android 动态生成复杂的布局(View布局的子控件含自己的点击事件)
- android:代码中动态添加子布局
- 在LinearLayout里动态添加子布局
- android:代码中动态添加子布局
- 关于recyclerview的横向布局的宽度以及item的点击事件的添加
- ViewPager 子布局点击事件的代替方法
- LinearLayout添加子布局
- Android动态设置布局,为LinearLayout动态添加子控件
- 【Android】利用Java代码布局,按钮添加点击事件
- RecyclerView使用(二)多种Item布局、添加点击事件
- 父布局与子布局控件的实现响应点击事件冲突问题。
- 动态添加一个布局
- 动态添加布局
- 动态添加布局
- 动态添加布局文件
- 动态添加布局-LayoutParams
- 动态添加布局
- 终于完成Android及其kenel源码编译!
- two arguments of main function
- 蓝牙底层HCI驱动的实现
- Python菜鸟--元组
- String和StringBuffer类型数据进行参数传递问题
- 动态添加重复布局以及各子布局点击事件
- java中的this详解
- Makefile详解-工作方式
- 安卓开发之调用服务里的方法
- 使用Class.getResource(),ClassLoader.getResource(),ResourceBundle.getBundle ()进行资源定位
- 第九章 函数再探
- Python菜鸟--字典(Dictionary)
- 语音端点检测(1):双门限法(简单教学版)
- 51nod-1133 不重叠的线段