1

我们正在寻找一种基于模板的解决方案来列出行,其中每个列表行可能具有不同的 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;
}
4

1 回答 1

1

Here are some things that you can do to improve your view's performance:

  1. Reuse views(you are inflating a view for every single instance). If you have variable number of fields then Adapter.getViewType and Adapter.getViewTypeCount are your friends.
  2. FindViewById's are expensive and make for a less performant experience. Read up on the ViewHolder pattern.
  3. If you are still running into problems you will need to look at what is causing the poor performance. My hunch is that if you still have poor performance after doing 1 and 2 that it's that you are parsing json in your getView. Consider preparsing your data into objects that can simply be read once getView is called.

Supplemental video(Long but very useful about adapters in general).

于 2013-05-10T15:03:29.903 回答