以编程方式创建动态列表行布局

时间:2013-05-10 10:19:48

标签: android listview layout dynamic template-engine

感谢您阅读我的查询。

我们正在寻找一个基于模板的解决方案来列出行,其中每个列表行可能有不同的2字段,它们的对齐方式将由服务器以json的形式驱动。

此json将包含有关字段和标签,图像及其对齐等字段类型的信息

我们想出了一个解决方案,对于每一行,在getView()方法中,我们解析布局json并以编程方式创建字段并添加相应的值,然后将其添加到相对布局,然后将所有这些相对布局添加到父线性垂直方向的布局。然后将此布局分配给convertview,这样就创建了行。

使用此解决方案列表视图并不顺畅,因为正在进行大量计算以生成动态字段。

任何帮助都非常适合。下面是样本布局json的代码

{
  "layouts": [
  {
  "templateId": "HCI",
  "width": "100%",
  "backgroundColor": "#87CEFA",
  "selectedBackgroundColor": "#96F2CD",
  "cornerRadius": "0",
  "borderWidth": "1",
  "borderColor": "#000000",
  "children": [
    {
      "width": "100%",
      "height": "20",
      "rightAlign": "0",
      "children": [
        {
          "type": "label",
          "field": "claimantName",
          "textColor": "#EB6208",
          "alignment": "left",
          "fontSize": "16",
          "style": "upper",
          "horizontalPadding": "5",
          "bold": "1"
        },
        {
          "type": "label",
          "field": "workType",
          "textColor": "#EB6208",
          "alignment": "left",
          "fontSize": "16",
          "style": "upper",
          "horizontalPadding": "5",
          "bold": "0"
        },
        {
          "type": "label",
          "field": "workkardNumber",
          "backgroundColor": "#2D5DB9",
          "textColor": "#FFFFFF",
          "alignment": "right",
          "horizontalPadding": "3"
        }
      ]
    },
    {
      "width": "100%",
      "height": "15",
      "children": [
        {
          "type": "label",
          "field": "address",
          "textColor": "#458B00",
          "alignment": "left",
          "fontSize": "16",
          "style": "upper",
          "horizontalPadding": "5",
          "bold": "1"
        },
        {
          "type": "label",
          "field": "lossDate",
          "textColor": "#838B83",
          "alignment": "",
          "fontSize": "16",
          "style": "lower",
          "horizontalPadding": "5",
          "bold": "0"
        }
      ]
    },
    {
      "width": "100%",
      "height": "15",
      "children": [
        {
          "type": "label",
          "field": "county",
          "textColor": "#838B83",
          "alignment": "left",
          "fontSize": "16",
          "style": "upper",
          "horizontalPadding": "5",
          "bold": "0"
        },
        {
          "type": "label",
          "field": "zip",
          "textColor": "#0000FF",
          "alignment": "",
          "fontSize": "16",
          "style": "lower",
          "horizontalPadding": "5",
          "bold": "0"
        },
        {
          "type": "label",
          "field": "state",
          "textColor": "#458B00",
          "alignment": "",
          "fontSize": "16",
          "style": "upper",
          "horizontalPadding": "5",
          "bold": "0"
        },
        {
          "type": "label",
          "field": "reportedDate",
          "textColor": "#838B83",
          "alignment": "",
          "fontSize": "16",
          "style": "lower",
          "horizontalPadding": "5",
          "bold": "0"
        }
      ]
    },
    {
      "width": "100%",
      "height": "50",
      "children": [
        {
          "type": "spacer",
          "width": "20"
        },
        {
          "type": "label",
          "field": "lossType",
          "style": "upper",
          "width": "280",
          "textColor": "#458B00",
          "bold": "1",
          "fontSize": "16"
        }
      ]
    },
    {
      "width": "100%",
      "height": "20",
      "children": [

      ]
    }
  ]
}

] }

以下是我解析和动态字段创建的逻辑

public LinearLayout generateLayout(Object object, WorkkardData workkard){
    this.workkardData=workkard;
    JSONObject json = (JSONObject)object;
    String template="";
    String cardBackground="";
    String cardSelectedBackground="";
    ArrayList child=new ArrayList();
    ArrayList subChild=new ArrayList();
    String fieldName="";
    try {
        template = json.getString("templateId");
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    try {
        cardBackground = json.getString("backgroundColor");
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    try {
        cardSelectedBackground = json.getString("selectedBackgroundColor");
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    try {
        cardBackground = json.getString("backgroundColor");
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    LinearLayout workkardrowLayout = new LinearLayout(context);
    workkardrowLayout.setOrientation(LinearLayout.VERTICAL);
    workkardrowLayout.setBackgroundColor(Color.parseColor(cardBackground));

    try {
        JSONArray childrentop = json.getJSONArray("children");
        child.add(childrentop);
        for (int i = 0; i < childrentop.length(); i++) {
            JSONArray subChildren = childrentop.getJSONObject(i).getJSONArray("children");
            subChild.add(subChildren);

            String width="350", height="0", rightAlignmen="0";
            int widthInt=0;
            try {
                width = childrentop.getJSONObject(i).getString("width");
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            try {
                height = childrentop.getJSONObject(i).getString("height");
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            try {
                rightAlignmen = childrentop.getJSONObject(i).getString("rightAlign");
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            if(width.equalsIgnoreCase("100%")){
                widthInt = 350;
            }else{
                widthInt = 350*(Integer.parseInt(width.trim().split("%")[0]))/100;
            }

            LinearLayout.LayoutParams paramsll1 = new LinearLayout.LayoutParams(widthInt,Integer.parseInt(height.trim()));
            /*if(rightAlignmen.equalsIgnoreCase("1")){
                                                                            paramsll1.gravity = Gravity.RIGHT;
                                                            }else{
                                                                            paramsll1.gravity = Gravity.LEFT;

                                                            }*/

            LinearLayout listLayoutRow1 = new LinearLayout(context);
            ll[i] = new LinearLayout(context);
            ll[i].setOrientation(LinearLayout.HORIZONTAL);
            ll[i].setWeightSum(10);
            ll[i].setLayoutParams(paramsll1);

            if(subChildren.length()==0){
                workkardrowLayout.addView(ll[i]);
            }else{
                for (int j = 0; j < subChildren.length(); j++) {

                    String type = "", field= "", backgroundColor ="", textColor ="", alignment ="",horizontalPadding ="", bold="",widthChild="",textColorChild="",fontSize="",style="lower";
                    try {
                        type = subChildren.getJSONObject(j).getString("type");
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    try {
                        field = subChildren.getJSONObject(j).getString("field");
                        fieldName=field;
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    try {
                        backgroundColor = subChildren.getJSONObject(j).getString("backgroundColor");
                        //ll[i].setBackgroundColor(Color.parseColor(backgroundColor));
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    try {
                        textColor = subChildren.getJSONObject(j).getString("textColor");
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    try {
                        alignment = subChildren.getJSONObject(j).getString("alignment");
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    try {
                        horizontalPadding = subChildren.getJSONObject(j).getString("horizontalPadding");
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    try {
                        bold = subChildren.getJSONObject(j).getString("bold");
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                    try {
                        widthChild = subChildren.getJSONObject(j).getString("width");
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    try {
                        textColorChild = subChildren.getJSONObject(j).getString("textColor");
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    try {
                        fontSize = subChildren.getJSONObject(j).getString("fontSize");
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    try {
                        style = subChildren.getJSONObject(j).getString("style");
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }


                    int padding=0;
                    try {
                        padding = Integer.parseInt(horizontalPadding);
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                    LinearLayout.LayoutParams llChild = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
                    //paramsll1. = Integer.parseInt(horizontalPadding);
                    //paramsll1.topMargin = 10;
                    //paramsll1.bottomMargin = 10;
                    //paramsll1.rightMargin = 10;
                    //paramsll1.gravity = Gravity.LEFT;
                    if(rightAlignmen.equalsIgnoreCase("1")){
                        llChild.gravity = Gravity.RIGHT;
                    }

                    if(type.equalsIgnoreCase("label")){
                        TextView listText1 = new TextView(context);
                        if(style.equalsIgnoreCase("upper")){
                            //listText1.setTypeface(null, Typeface.BOLD);
                            listText1.setText(this.getWorkkardMethod(field).toUpperCase());
                        }else{
                            listText1.setText(this.getWorkkardMethod(field));
                        }
                        if(field.equalsIgnoreCase("lossDate")){
                            listText1.setText("Loss Date:"+this.getWorkkardMethod(field));
                        }
                        if(field.equalsIgnoreCase("reportedDate")){
                            listText1.setText("Reported:"+this.getWorkkardMethod(field));
                        }
                        if (!backgroundColor.equalsIgnoreCase("")) {
                            listText1.setBackgroundColor(Color.parseColor(backgroundColor));
                        }
                        listText1.setTextColor(Color.parseColor(textColor));
                        if (!fontSize.equalsIgnoreCase("")) {
                            listText1.setTextSize(Integer.parseInt(fontSize));
                        }
                        listText1.setPadding(padding,padding, padding, padding);
                        if(alignment.equalsIgnoreCase("left")){
                            ll[i].setGravity(Gravity.LEFT);
                        }
                        if(alignment.equalsIgnoreCase("right")){
                            ll[i].setGravity(Gravity.RIGHT);
                        }
                        if(alignment.equalsIgnoreCase("centre")){
                            ll[i].setGravity(Gravity.CENTER);
                        }
                        if(bold.equalsIgnoreCase("1")){
                            listText1.setTypeface(null, Typeface.BOLD);
                        }
                        ll[i].addView(listText1);
                        ll[i].setLayoutParams(llChild);
                    }else if(type.equalsIgnoreCase("spacer")){
                        llChild.width=Integer.parseInt(widthChild);
                        ll[i].setLayoutParams(llChild);
                        TextView listText1 = new TextView(context);
                        listText1.setText(" ");
                        ll[i].addView(listText1);
                    }
                }
                workkardrowLayout.addView(ll[i]);
            }

        }
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }



    //                            LinearLayout workkardrowLayout = new LinearLayout(context);
    //                            workkardrowLayout.setOrientation(LinearLayout.VERTICAL);
    //                            workkardrowLayout.setBackgroundColor(Color.parseColor(cardBackground));

    //if()

    //                            LinearLayout listLayoutRow1 = new LinearLayout(context);
    //                            LinearLayout listLayoutRow2 = new LinearLayout(context);
    //                            LinearLayout listLayoutRow3 = new LinearLayout(context);
    //
    //                            listLayoutRow1.setOrientation(LinearLayout.HORIZONTAL);
    //                            listLayoutRow2.setOrientation(LinearLayout.HORIZONTAL);
    //                            listLayoutRow3.setOrientation(LinearLayout.HORIZONTAL);
    //
    //                            listLayoutRow1.setWeightSum(20);
    //                            listLayoutRow2.setWeightSum(20);
    //                            listLayoutRow3.setWeightSum(20);
    //
    //                            LinearLayout.LayoutParams paramsll1 = new LinearLayout.LayoutParams(300, LayoutParams.WRAP_CONTENT);
    //                            paramsll1.leftMargin = 10;
    //                            paramsll1.topMargin = 10;
    //                            paramsll1.bottomMargin = 10;
    //                            paramsll1.rightMargin = 10;
    //                            paramsll1.gravity = Gravity.LEFT;
    //                            listLayoutRow1.setLayoutParams(paramsll1);
    //                            listLayoutRow2.setLayoutParams(paramsll1);
    //                            listLayoutRow3.setLayoutParams(paramsll1);
    //
    //                            TextView listText1 = new TextView(context);
    //                            listText1.setText(template);
    //                            TextView listText2 = new TextView(context);
    //                            listText2.setText("Hurre............ Dynamic lauout row2:-)");
    //                            TextView listText3 = new TextView(context);
    //                            listText3.setText("Hurre............ Dynamic lauout row 3:-)");
    //
    //                            listLayoutRow1.addView(listText1);
    //                            listLayoutRow2.addView(listText2);
    //                            listLayoutRow3.addView(listText3);
    //
    //                            workkardrowLayout.addView(listLayoutRow1);
    //                            workkardrowLayout.addView(listLayoutRow2);
    //                            workkardrowLayout.addView(listLayoutRow3);

    return workkardrowLayout;

}

String getWorkkardMethod(String fieldName){
    String work="";
    if(fieldName.equalsIgnoreCase("template")){
        work=workkardData.getTemplate();
    }else if(fieldName.equalsIgnoreCase("workkardTitle")){
        work=workkardData.getWorkkardTitle();
    }else if(fieldName.equalsIgnoreCase("workkardDescription")){
        work=workkardData.getWorkkardDescription();
    }else if(fieldName.equalsIgnoreCase("workType")){
        work=workkardData.getWorkType();
    }else if(fieldName.equalsIgnoreCase("workkardNumber")){
        work=workkardData.getWorkkardNumber();
    }else if(fieldName.equalsIgnoreCase("claimantName")){
        work=workkardData.getClaimantName();
    }else if(fieldName.equalsIgnoreCase("reportedDate")){
        work=workkardData.getReportedDate();
    }else if(fieldName.equalsIgnoreCase("lossDate")){
        work=workkardData.getLossDate();
    }else if(fieldName.equalsIgnoreCase("lossType")){
        work=workkardData.getLossType();
    }else if(fieldName.equalsIgnoreCase("address")){
        work=workkardData.getAddress();
    }else if(fieldName.equalsIgnoreCase("county")){
        work=workkardData.getCounty();
    }else if(fieldName.equalsIgnoreCase("state")){
        work=workkardData.getState();
    }else if(fieldName.equalsIgnoreCase("zip")){
        work=workkardData.getZip();
    }

    return work;
}

1 个答案:

答案 0 :(得分:1)

您可以采取以下措施来改善观看效果:

  1. 重用视图(您为每个实例充实视图)。如果你有可变数量的字段,那么Adapter.getViewType和Adapter.getViewTypeCount就是你的朋友。
  2. FindViewById是昂贵的,并使性能较差的体验。阅读ViewHolder pattern
  3. 如果您仍遇到问题,则需要查看导致性能不佳的原因。我的预感是,如果在执行1和2后仍然表现不佳,那就是你在getView中解析json。考虑将数据预先准备好可以在调用getView后简单读取的对象。
  4. Supplemental视频(对于适配器来说很长但非常有用)。