2012-03-10 54 views
3

找到解決方案!Android:充氣佈局需要很長時間

我現在使用ViewPager代替ViewFlipper。 視圖現在在我的run()方法(已經存在,因爲我從Web獲取數據)中生成並保存在一個Map中。 在我的Handler中,我只調用pagerAdapter.notifyDataSetChanged(),pagerAdapter使用視圖的Map,它的工作流暢而快速。 因此,我現在正在尋找一個離開有ViewPager滾動無止境,但多數民衆贊成在另一個問題沒有連接到這一個;)

感謝大家的答案,並保持良好的支持。

我對Android開發頗爲陌生,在擴大(巨大)佈局時遇到問題。 我從一個Web服務獲得一些數據,它工作正常,然後我在我的活動中使用一個處理程序將這些數據帶到前端。這是我的handleMessage:

public void handleMessage(Message msg) { 
     LayoutInflater inflater = getLayoutInflater(); 

     List<Integer> gamedays = new ArrayList<Integer>(games.keySet()); 
     Collections.sort(gamedays); 

     for (Integer gameday : gamedays) { 

      View gamedaytable = inflater.inflate(R.layout.gamedaytable, null); 
      TableLayout table = (TableLayout) gamedaytable.findViewById(R.id.gameDayTable); 
      table.removeAllViews(); 
      List<Game> gamelist = games.get(gameday); 
      int rowcount = 2; 
      for (Game game : gamelist) { 

       View tableRow = inflater.inflate(R.layout.gamedayrow, null); 

       TextView homeTeam = (TextView) tableRow.findViewById(R.id.gameDayHome); 
       TextView awayTeam = (TextView) tableRow.findViewById(R.id.gameDayAway); 
       TextView gameResult = (TextView) tableRow.findViewById(R.id.gameDayResult); 
       gameResult.setBackgroundResource(R.drawable.resultbackground); 
       homeTeam.setText(game.getHomeTeam().getName()); 
       awayTeam.setText(game.getAwayTeam().getName()); 
       if (game.getHomegoals() < 0 || game.getAwaygoals() < 0) { 
        gameResult.setText("-:-"); 
       } else { 
        gameResult.setText(game.getHomegoals() + ":" + game.getAwaygoals()); 
       } 

       if (rowcount % 2 == 0) { 
        tableRow.setBackgroundColor(0xffdee0dd); 

       } else { 
        // setting alternative background 
        tableRow.setBackgroundColor(0xfff1f3f0); 
       } 
       rowcount++; 

       table.addView(tableRow); 
      } 
      flipper.addView(gamedaytable); 
     } 
     flipper.setDisplayedChild(thisgameday - 1); 
     pd.dismiss(); 

    } 

我的問題是,這個代碼運行很慢,d爲processdialog凍結約1秒鐘就消失,佈局如圖之前。 遊戲由34個條目組成,其中包含9個條目。 因此,我添加了34個包含一個relativeLayout()的視圖,其中包含表 我認爲問題在於,android開始繪製和計算佈局,這需要很長時間。 如果我是正確的,我不能使用AsynTask,因爲我不能在那裏做UI的東西,我只做UI的東西。

我正在尋找一種方法讓進程對話框在執行此操作時不會凍結。 或者,也許我正在做一些完全地錯

R.layout.gamedaytable:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/relativeLayout1" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="#fff1f3f0" 
android:focusableInTouchMode="false" > 

<TableLayout 
    android:id="@+id/gameDayTable" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerHorizontal="true" 
    android:layout_centerVertical="true" 
    android:focusableInTouchMode="false" > 
</TableLayout> 

</RelativeLayout> 

R.layout.gamedayrow:

<TableRow xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/tableRow1" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:focusableInTouchMode="false" 
android:paddingBottom="5dp" 
android:paddingTop="5dp" > 

<TextView 
    android:id="@+id/gameDayHome" 
    style="@style/textsizeSmallScreen" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginLeft="5dp" 
    android:text="Mannschaft 1" /> 

<TextView 
    android:id="@+id/textView2" 
    style="@style/textsizeSmallScreen" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:paddingLeft="10dp" 
    android:paddingRight="10dp" 
    android:text=":" /> 

<TextView 
    android:id="@+id/gameDayAway" 
    style="@style/textsizeSmallScreen" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Mannschaft 2" /> 

<TextView 
    android:id="@+id/gameDayResult" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginLeft="10dp" 
    android:layout_marginRight="5dp" 
    android:background="@drawable/resultbackground" 
    android:paddingLeft="10dip" 
    android:text="0:5" 
    android:textColor="#FFFFFF" 
    android:textSize="11dp" 
    android:textStyle="bold" 
    android:typeface="monospace" /> 

</TableRow> 

附加信息: 這是表應該如何看起來像。 所以我不知道這是否真的應該是一個ListView因爲對我來說它的資料表;)

table

回答

0

你絕對可以上AsyncTask做到這一點。雖然無法更新AsyncTask的doInBackground方法的UI,但您可以從onProgressUpdate

我會分手的代碼,所以你是通過迭代的項目,而在doInBackground,呼籲publishProgress每個項目,然後執行該項目的UI更新,當你在onProgressUpdate得到一個回調。

+0

我不認爲這會解決問題,因爲我的代碼只包含UI更新(除了獲取視圖) 或者我可以在doInBackground中充氣佈局,只需在publishProgress中執行flipper.addView? – amfa 2012-03-10 00:49:44

+0

在doInBackground上膨脹它們可能會起作用。請參閱:http://stackoverflow.com/questions/6691311/inflate-a-view-in-a-background-thread/6692007#6692007 – elevine 2012-03-10 01:27:16

+0

但根據線程我不應該這樣做,因爲它可能有一些副作用。 也許我應該使用一個完全不同的解決方案,因爲即使創建將工作.. viewflipper本身在某種程度上很慢,並沒有這個「應用程序的感覺」;) 無論如何謝謝你的幫助 – amfa 2012-03-10 01:56:17

4

你似乎在建立一個列表,你應該看看使用一個ListView,它具有隻需要爲當前顯示的行數構建UI的優點,使用,這樣你就不需要膨脹多少行。

+0

這不是一個列表,我認爲所有的行總是顯示。 目前可能看起來像一個普通的名單,但我打算通過將特定比賽日的足球結果分組進行分組來擴展表格/列表(在星期五和星期一之間可以是一天) 。 我不確定ListView是否是正確的解決方案。 – amfa 2012-03-10 00:42:08

+0

仍然聽起來像ListView或ExpandableListView將是要走的路。 – superfell 2012-03-10 01:54:12

+0

我會試一試 感謝您的幫助。 – amfa 2012-03-10 02:08:26

1

最好去Listview。即使我們可以以優化的方式在列表視圖中添加多個行設計,這會更好地改善性能。

+0

我還沒有試過ListView(對我很慚愧),但是如何安排聽衆的方式與他們在表格中一樣? 而不給textViews一個固定的寬度? 例如帶有「:」的textViews必須位於每個條目中相同的「列」中 – amfa 2012-03-10 11:40:19

+0

您可以使用Horizo​​natal方向在LinearLayout中添加子視圖。加總和權重爲4,並保持所有的孩子佈局權重爲1.不要爲獲得父母的寬度爲0(它是一個優化的方式說它將佔據整個屏幕)。 – Pavandroid 2012-03-10 17:01:29

2

找到解決方案!

我現在使用ViewPager代替ViewFlipper。視圖現在在我的run()方法中生成(它已經存在,因爲我從Web獲取數據)並保存在一個Map中。在我的Handler中,我只調用pagerAdapter.notifyDataSetChanged(),pagerAdapter使用視圖的Map,並且它的工作流暢且快速。所以我現在正在尋找一個讓ViewPager滾動無盡,但那是另一個問題沒有連接到這一個;)

謝謝大家的答案,並保持良好的支持。