1

我有一个相对复杂的嵌套线性布局和文本视图设置,它们都是在运行时根据用户偏好创建的。

问题是启动应用程序需要大约五秒钟(智能手机时间的永恒)。

在此处输入图像描述

这是设置图。我在滚动视图中有大约 30 个红色线性布局,每个布局都包含 2 到 30 个紫色文本视图,它们包裹在 1 到 6 个橙色线性布局中。

我可以做些什么来加快这个 UI 的加载,或者保存它,以便当用户退出并重新打开应用程序时,它不必重新生成所有内容?

谢谢你。

我的代码在下面,它利用了这篇文章中的 populateLinks 函数。

public void displayUnits(){
    LinearLayout ll = new LinearLayout(MainActivity.this);
    ll = (LinearLayout) findViewById(R.id.fromunitslayout);

    if (ll.getChildCount() > 3) {
        ll.removeViewsInLayout(2,ll.getChildCount()-2);
    }

    fromunitlls = new ArrayList<LinearLayout>();
    fromunitlls.clear();

    int colorcount = 0;

    for (int utcount = 0; utcount < unittypes.size(); utcount++) {
        UnitType currenttype = null;
        for (int typecount = 0; typecount < unittypes.size(); typecount++) {
            if (unittypes.get(typecount).getOrder() == utcount) {
                currenttype = unittypes.get(typecount);
                Log.v("unittype disp", String.valueOf(unittypes.indexOf(currenttype)));             
            }
        }
        if (currenttype.getHidden() && !showallunits) {
            continue;
        }
        for (int unitcount = 0; unitcount < ft.size(); unitcount++) {
            if (ft.get(unitcount).getType().getID() == currenttype.getID()) {

                // Create and set up Red LLs
                LinearLayout llhorz = new LinearLayout(MainActivity.this);
                LinearLayout llvert = new LinearLayout(MainActivity.this);
                LinearLayout lllist = new LinearLayout(MainActivity.this);
                llhorz.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
                llhorz.setOrientation(LinearLayout.HORIZONTAL);
                llvert.setLayoutParams(new LayoutParams(140,LayoutParams.MATCH_PARENT));
                llvert.setOrientation(LinearLayout.VERTICAL);
                lllist.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
                lllist.setOrientation(LinearLayout.VERTICAL);

                if (colorcount % 2 != 0) {
                    llhorz.setBackgroundResource(R.color.DarkGreyT);
                }
                colorcount++;

                // Add Blue Text View to Vertical LL            
                AutoResizeTextViewSmall txtTypeName = new AutoResizeTextViewSmall(MainActivity.this);
                txtTypeName.setTag("unittype" + unittypes.indexOf(currenttype));
                txtTypeName.setPadding(5,0,5,0);
                txtTypeName.setText(currenttype.getName().toUpperCase().replace(" ","\n"));
                txtTypeName.setLayoutParams(new LayoutParams(140,LayoutParams.WRAP_CONTENT, Gravity.CENTER));
                txtTypeName.setTextSize(12);
                txtTypeName.setTypeface(Typeface.DEFAULT_BOLD);
                txtTypeName.setGravity(Gravity.CENTER);

                txtTypeName.setOnLongClickListener(new OnLongClickListener(){
                    public boolean onLongClick(View view){
                        String typenum = view.getTag().toString();
                        typenum = typenum.substring(8);
                        Log.v("typenum",typenum);
                        Intent intent = new Intent(MainActivity.this, CatSettingsActivity.class);
                        intent.putExtra("TYPENUMBER", Integer.parseInt(typenum));
                        startActivity(intent);
                        return false; 
                    }
                });
                llvert.addView(txtTypeName);
                fromunitlls.add(lllist);
                fromunitlls.get(fromunitlls.indexOf(lllist)).setId(unittypes.indexOf(currenttype));

                llhorz.addView(llvert);
                //llhorz.addView(txtTypeName);
                llhorz.addView(fromunitlls.get(fromunitlls.indexOf(lllist)));

                ll.addView(llhorz);

                txtTypeName.reSize();
                break;
            }
        }
    }

    // Call other function to populate red LLs with orange LLs and purple TVs
    for (int utcount = 0; utcount < unittypes.size(); utcount++) {
        if (unittypes.get(utcount).getHidden()) {
            continue;
        }
        for (int unitcount = 0; unitcount < ft.size(); unitcount++) {
            if (ft.get(unitcount).getType().getID() == unittypes.get(utcount).getID()) {
                populateLinks(unittypes.get(utcount), ft);
                break;
            }
        }
    }
}
4

1 回答 1

0

您可以尝试通过使用RelativeLayout而不是嵌套线性布局来减少布局数量。

于 2013-03-05T19:09:11.667 回答