1. 程式人生 > >動態新增重複佈局以及各子佈局點選事件

動態新增重複佈局以及各子佈局點選事件

要求實現一個可以一次建立多個班級的功能,效果如下圖:
這裡寫圖片描述

其中班級名和專業是一個可以複用的子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()得到的就和原本儲存的父佈局不是同一個,會造成點選事件沒有響應。