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

问题描述 投票:1回答: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;
}
android listview layout dynamic template-engine
1个回答
1
投票

您可以采取一些措施来改善视图的性能:

  1. 重用视图(您正在为每个实例增加一个视图)。如果您拥有可变数量的字段,那么Adapter.getViewType和Adapter.getViewTypeCount是您的朋友。
  2. FindViewById的价格昂贵,并且会降低性能。阅读ViewHolder pattern
  3. 如果您仍然遇到问题,则需要查看导致性能下降的原因。我的直觉是,如果您在执行1和2之后仍然表现不佳,那就是您正在解析getView中的json。考虑将数据准备为对象,一旦调用getView即可简单地读取它们。

[Supplemental video(很长,但是对于适配器来说非常有用)。

© www.soinside.com 2019 - 2024. All rights reserved.