gpt4 book ai didi

Android:膨胀布局需要很长时间

转载 作者:太空宇宙 更新时间:2023-11-03 12:23:08 25 4
gpt4 key购买 nike

Found a Solution!

I now use a ViewPager instead of a ViewFlipper. The Views are now generated within my run() method (which is already there because I fetch data from the web) and saveed in a Map. In my Handler I only call pagerAdapter.notifyDataSetChanged() the pagerAdapter uses the Map of views and it works smooth and fast. So I'm now looking for a away to have the ViewPager scroll endless, but thats another problem not connected to this one ;)

Thank all of you for your answers and keep up the good support.

我是 Android 开发的新手,在扩充(巨大的)布局时遇到了问题。我从一个工作正常的 Web 服务获取一些数据,然后我在我的 Activity 中使用一个处理程序将这些数据带到前端。这是我的句柄消息:

    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();

}

我的问题是此代码运行速度很慢,进程对话框在消失并显示布局之前卡住了大约 1 秒。游戏由 34 个条目组成,其中本身包含 9 个条目。所以我添加了 34 个 View ,其中包含一个包含表格的 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,因为对我来说它是 tabledata ;)

table

最佳答案

你似乎在构建一个列表,你应该考虑使用 ListView,它的优点是只需要为当前显示的行数构建 UI,并且还可以重用 View ,这样您就不需要膨胀那么多行。

关于Android:膨胀布局需要很长时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9642665/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com