android 根據搜索條件動態添加控件

最近做了個應用,是一個列表,列表上方有一些搜索條件,其中類型條件是個下拉列表框,根據選擇的下拉框的值不同,調用接口,顯示不同類型和不同數量的控件;

1. 在代碼中自定義控件

private LinearLayout getDataLayout(String label, String type) {
        LinearLayout linearLayout = new LinearLayout(mContext);
//        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 68);
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) ll_date.getLayoutParams();
        linearLayout.setLayoutParams(params);
        linearLayout.setBackgroundResource(R.drawable.bg_doc_line);
        TextView tvLabel = new TextView(mContext);
        tvLabel.setText(label);
        LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT);
        params1.weight=2;
        params1.setMargins(0,30,0,30);
        tvLabel.setTextColor(getResources().getColor(R.color.color_333333));

        tvLabel.setLayoutParams(params1);
        tvLabel.setTextSize(14);
        Log.e(TAG,"tvLabel="+tvLabel.getText().toString());
        ViewParent vp = tvLabel.getParent();
        if (vp != null) {
            ((ViewGroup)vp).removeView(tvLabel);
        }

        linearLayout.addView(tvLabel);
        if (type.equals("date")) {
            TextView tvContent = new TextView(mContext);
//        LinearLayout.LayoutParams params2 = (LinearLayout.LayoutParams) tvContent.getLayoutParams();
            LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT);
            tvContent.setBackgroundColor(getResources().getColor(R.color.trans));
            params2.weight = 5;
            params2.setMargins(0,30,0,30);
            tvContent.setLayoutParams(params2);
            tvContent.setTextColor(getResources().getColor(R.color.color_hint));
            tvContent.setTextSize(14);
            linearLayout.addView(tvContent);
            tvContent.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    new DatePickerDialog(mContext, AlertDialog.THEME_DEVICE_DEFAULT_DARK, new DatePickerDialog.OnDateSetListener() {
                        @Override
                        public void onDateSet(DatePicker datePicker, int i, int i1, int i2) {
                            Log.d(TAG,"tvContent  i="+i+",i1="+i1+",i2="+i2);
                            tvContent.setText(dataFormat(i,i1,i2));
                        }
                    }, mYear, mMonth, mDay).show();
                }
            });
        } else {
            EditText etContent = new EditText(mContext);
            LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT);
            etContent.setBackgroundColor(getResources().getColor(R.color.trans));
            etContent.setPadding(0,0,0,0);
            params2.weight = 5;
            params2.setMargins(0,30,0,30);

            etContent.setTextSize(14);
            etContent.setTextColor(getResources().getColor(R.color.color_hint));
            etContent.setLayoutParams(params2);
            linearLayout.addView(etContent);
        }
        return linearLayout;
    }

2. 使用:

  根據類型下拉列表的選項,調用接口,獲取其他搜索條件:

  下拉列表的第一項,顯示默認的控件(在xml中定義的);

List<ConditionBean> conditionBeans = new ArrayList<>();
    String[] valArr;
    private void getTypeSearchData() {
        HashMap<String, Object> baseParam = WebFrontUtil.getBaseParam();
        baseParam.put("type", String.valueOf(totalIndex));
        OkHttpUtil.post(TAG, WebApi.ACTIVITY_DOC_TYPE, baseParam, new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                Log.d(TAG,"getTypeSearchData  e="+e);
            }

            @Override
            public void onResponse(String response, int id) {
                Log.d(TAG,"getTypeSearchData  response="+response);
                try {
                    JSONObject object = new JSONObject(response);
                    if ((int)object.get("code") == 200) {
                        JSONArray array = object.getJSONArray("data");
                        Gson gson = new Gson();
                        conditionBeans.clear();

                        for (int i=0;i<array.length();i++) {
                            ConditionBean conditionBean = gson.fromJson(array.get(i).toString(), ConditionBean.class);
                            conditionBeans.add(conditionBean);
                        }

                        if (totalIndex == 0) {
                            ConditionBean bean = conditionBeans.get(conditionBeans.size()-1);
                            List<ValList> valLists = bean.getValList();
                            valArr = new String[valLists.size()];
                            for (int i=0;i<valLists.size();i++) {
                                ValList valList = valLists.get(i);
                                valArr[i] = valList.getText();
                            }
                            ArrayAdapter<String> adapter1 = new ArrayAdapter(mContext,R.layout.punish_spinner_item,valArr);
                            adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                            spinner_yuan.setAdapter(adapter1);
                        } else {
                            Log.e(TAG,"11111111111   "+totalIndex+","+ll_selectNormal.getChildCount());
                            ll_selectNormal.removeAllViews();
                            for (int i=0;i<conditionBeans.size();i++) {
                                ConditionBean bean = conditionBeans.get(i);
                                Log.e(TAG,"555555   "+bean.getText());
                                if (bean.getDatetype().equals("date")) {
                                    LinearLayout linearLayout = getDataLayout(bean.getText(),bean.getDatetype());
                                    ll_selectNormal.addView(linearLayout);
//                                    ll_selectNormal.addView(getViewLine());
                                } else if (bean.getDatetype().equals("input")) {
                                    LinearLayout linearLayout = getDataLayout(bean.getText(),bean.getDatetype());
                                    ll_selectNormal.addView(linearLayout);
//                                    ll_selectNormal.addView(getViewLine());
                                }
                            }
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

  接口獲取到的查詢條件示例:

{"code":200,"message":"成功","data":[{"text":"案件名稱","name":"0","datetype":"input"},{"text":"年月日","name":"6","datetype":"date"}]}

或者

{"code":200,"message":"成功","data":[{"text":"歸檔號","name":"30","datetype":"input"}]}

3. 對應的xml文件:

   動態添加的控件都添加到ll_selNormal裏,當類型選擇第一項時,顯示ll_select1

<LinearLayout
            android:id="@+id/ll_sel"
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <LinearLayout
                android:background="@drawable/bg_doc_line"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
                <TextView
                    android:text="類型:"
                    style="@style/office_text_label_style"
                    android:layout_weight="2"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"/>
                <Spinner
                    android:id="@+id/spinner_type"
                    android:spinnerMode="dialog"
                    style="@style/office_text_style"
                    android:gravity="right"
                    android:layout_weight="5"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"/>

            </LinearLayout>

            <LinearLayout
                android:id="@+id/ll_selNormal"
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
                <LinearLayout
                    android:id="@+id/ll_date"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">
                    <TextView
                        android:id="@+id/tv_searchLabel"
                        android:text="年月日:"
                        style="@style/office_text_label_style"
                        android:layout_weight="2"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"/>
                    <TextView
                        android:id="@+id/tv_date"
                        android:gravity="right"
                        style="@style/office_text_style"
                        android:layout_weight="5"
                        android:layout_width="0dp"
                        android:layout_height="match_parent"/>
                </LinearLayout>
            </LinearLayout>
            <LinearLayout
                android:id="@+id/ll_select1"
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
                <LinearLayout
                    android:id="@+id/ll_type1"
                    android:background="@drawable/bg_doc_line"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">
                    <TextView
                        android:text="登記號:"
                        style="@style/office_text_label_style"
                        android:layout_weight="2"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"/>
                    <EditText
                        android:id="@+id/tv_type1No"
                        style="@style/office_text_style"
                        android:gravity="right"
                        android:background="@null"
                        android:layout_weight="5"
                        android:layout_width="0dp"
                        android:layout_height="match_parent"/>
                </LinearLayout>
                <LinearLayout
                    android:id="@+id/ll_type1_date"
                    android:background="@drawable/bg_doc_line"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">
                    <TextView
                        android:text="登記時間:"
                        style="@style/office_text_label_style"
                        android:layout_weight="2"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"/>
                    <TextView
                        android:id="@+id/tv_type1Date"
                        style="@style/office_text_style"
                        android:gravity="right"
                        android:background="@null"
                        android:layout_weight="5"
                        android:layout_width="0dp"
                        android:layout_height="match_parent"/>
                </LinearLayout>

                <LinearLayout
                    android:background="@drawable/bg_doc_line"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">
                    <TextView
                        android:text="來源分類:"
                        style="@style/office_text_label_style"
                        android:layout_weight="2"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"/>
                    <Spinner
                        android:id="@+id/spinner_yuan"
                        android:spinnerMode="dropdown"
                        style="@style/office_text_style"
                        android:gravity="right"
                        android:dropDownVerticalOffset="@dimen/px100"
                        android:layout_weight="5"
                        android:layout_width="0dp"
                        android:layout_height="match_parent"/>

                </LinearLayout>
            </LinearLayout>
        </LinearLayout>

4. 獲取動態控件輸入的值:

  conditionBeans在第2步中獲取,customWheres查詢條件集合

private List<CustomWhere> customWheres = new ArrayList<>();
            case R.id.btn_docSearch:
                customWheres.clear();
                for (int i=0;i<conditionBeans.size();i++) {
                    ConditionBean bean = conditionBeans.get(i);
                    CustomWhere customWhere = new CustomWhere();
                    customWhere.setName(bean.getName());
                    if (totalIndex == 0) {
                        Log.e(TAG,(ll_select1.getChildAt(i)).getId()+"");
                        LinearLayout ll1 = (LinearLayout) ll_select1.getChildAt(i);
                        if (!bean.getDatetype().equals("select")) {
                            Log.e(TAG,((TextView)ll1.getChildAt(ll1.getChildCount()-1)).getText()+",2222"+","+i);
                            String value = ((TextView)ll1.getChildAt(ll1.getChildCount()-1)).getText().toString();

                            customWhere.setValue(value);
                        } else {
                            String value = ((Spinner)ll1.getChildAt(ll1.getChildCount()-1)).getSelectedItem().toString();
                            Log.e(TAG,"select="+value+",2222"+","+i);
                            customWhere.setValue(value);
                        }

                    } else {
                        Log.e(TAG,(ll_selectNormal.getChildAt(i)).getId()+"");
                        LinearLayout ll1 = (LinearLayout) ll_selectNormal.getChildAt(i);
                        Log.e(TAG,((TextView)ll1.getChildAt(ll1.getChildCount()-1)).getText()+",111"+","+i);
                        String value = ((TextView)ll1.getChildAt(ll1.getChildCount()-1)).getText().toString();
                        customWhere.setValue(value);
                    }

                    customWheres.add(customWhere);
                }
                getDocumentData(totalIndex,customWheres);
                break;

5. customWhere.java

public class CustomWhere {
    private String name;
    private String value;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章