找到解決方案!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因爲對我來說它的資料表;)
我不認爲這會解決問題,因爲我的代碼只包含UI更新(除了獲取視圖) 或者我可以在doInBackground中充氣佈局,只需在publishProgress中執行flipper.addView? – amfa 2012-03-10 00:49:44
在doInBackground上膨脹它們可能會起作用。請參閱:http://stackoverflow.com/questions/6691311/inflate-a-view-in-a-background-thread/6692007#6692007 – elevine 2012-03-10 01:27:16
但根據線程我不應該這樣做,因爲它可能有一些副作用。 也許我應該使用一個完全不同的解決方案,因爲即使創建將工作.. viewflipper本身在某種程度上很慢,並沒有這個「應用程序的感覺」;) 無論如何謝謝你的幫助 – amfa 2012-03-10 01:56:17