【发布时间】:2011-07-01 01:00:10
【问题描述】:
我有动态且相当大的内容,需要在用户交互时填充(单击下一页/上一页)。
在 myTableLayout 上以编程方式显示的动态内容(参见下面的 xml)。每次用户点击下一个/上一个
页面然后我打电话给myTableLayout.removeAllViews(),做一些事情并重新填充myTableLayout.addView()。
在小内容上它绘制得非常快,但我注意到如果填充大内容会延迟两到三秒。
我已经看到一个应用程序与我的功能相同(内容也很大),但它的分页速度真的很快 即使在低端设备上。
需要一些关于如何提高布局性能的建议。
谢谢。
注意:上方的TableLayout用作标题(标题),下方的LinearLayout是放置内容的地方。
<TableLayout
android:layout_width="fill_parent"
android:layout_height="45dip"
android:stretchColumns="0,2"
android:shrinkColumns="0,2"
>
<TableRow>
<LinearLayout
android:id="@+id/llLeft"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/tvLeft1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
<LinearLayout
android:id="@+id/llCenter"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/tvCenter1"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
/>
<TextView
android:id="@+id/tvCenter2"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
/>
</LinearLayout>
<LinearLayout
android:id="@+id/llRight"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/tvRight1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
</TableRow>
</TableLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<com.MyScrollView
android:id="@+id/myScrollView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<TableLayout
android:id="@+id/**myTableLayout**"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:shrinkColumns="1"
android:stretchColumns="1"
/>
</com.MyScrollView>
</LinearLayout>
这是创建动态内容的代码。从内存(ArrayList)中获取的内容应该不是问题。
private void gotoPage() {
myTableLayout.removeAllViews();
GlobalVar g = GlobalVar.getInstance();
ArrayList<String> arrScripts = g.getDatasMap().get(SCRIPT_INDEX);
ArrayList<String> arrTrans = g.getTranslationMap().get(SCRIPT_INDEX);
Log.i(APP_NAME, "finished preparing data");
try {
// Data Title
tvCenter1.setText(arrScripts.get(0));
tvCenter2.setText(" (" + SCRIPT_INDEX + ")");
int countScripts = arrScripts.size();
for(int i=1; i<countScripts; i++) {
// Row Script
TableRow trScript = new TableRow(this);
trScript.setPadding(0, 5, 0, 0);
TextView tvIndex = new TextView(this);
tvIndex.setGravity(Gravity.CENTER);
tvIndex.setWidth(40);
tvIndex.setText("" + i);
TextView tvScript = new TextView(this);
tvScript.setGravity(Gravity.RIGHT);
tvScript.setTypeface(g.getFontTypeFace());
tvScript.setTextSize(FONT_SIZE);
tvScript.setPadding(0, 0, 5, 0);
tvScript.setText(arrScripts.get(i));
trScript.addView(tvIndex);
trScript.addView(tvScript);
TableRow trTrans = null;
if(IS_USE_TRANSLATION) {
// Row Translation
trTrans = new TableRow(this);
TextView tvBlank = new TextView(this);
tvBlank.setPadding(0, 0, 0, 5);
TextView tvTrans = new TextView(this);
tvTrans.setPadding(0, 0, 0, 5);
tvTrans.setText(arrTrans.get(i));
trTrans.addView(tvBlank);
trTrans.addView(tvTrans);
}
// Place for line separator
View vSep = new View(this);
ViewGroup.LayoutParams p = new ViewGroup.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
p.height = 3;
vSep.setLayoutParams(p);
// Combine rows to table
if(IS_USE_SCRIPT) myTableLayout.addView(trScript);
if(IS_USE_TRANSLATION) myTableLayout.addView(trTrans);
myTableLayout.addView(vSep);
}
} catch (Exception e) {
Log.e(APP_NAME, e.getMessage());
}
}
这里是traceview result。
【问题讨论】:
标签: android performance layout