2013-03-05 38 views
1

我有一個相對複雜的嵌套線性佈局和文本視圖的設置,它們都是在運行時根據用戶的喜好創建的。Android - 如何提高複雜動態用戶界面的性能

問題是,啓動應用程序需要大約五秒鐘,因爲這(智能手機時間的永恆)。

enter image description here

Here is a diagram of the set up.我有大約30在滾動視圖紅色線性佈局和每一個從2到30的紫色textviews包裹成1至6橙線性佈局的任何位置包含。

有什麼我可以做的,以加快這個用戶界面的加載,或者保存它,使得當用戶退出並重新打開應用程序時,它不必重新生成一切?

謝謝。

我的代碼如下,它利用populateLinks功能從this post.

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; 
      } 
     } 
    } 
} 
+0

當您運行Traceview來確定問題的具體位置時,您學到了什麼? – CommonsWare 2013-03-05 19:00:46

+0

@ user2137040請在調用'displayUnits()'之前看看你做了什麼。之前發佈代碼。 – 2013-03-05 19:07:02

+2

您應該考慮切換到使用帶有CustomAdapter的ListView來處理創建行。這樣Activity在啓動時不會產生每一行,而只是爲了填充當前屏幕而需要的行。當用戶滾動時它會加載更多。 – FoamyGuy 2013-03-05 19:07:15

回答

0

你可以嘗試使用RelativeLayout代替嵌套的線性佈局,以降低佈局的數量。