我刚刚在 Galaxy Nexus 上升级了 Android 4.2,ListView 似乎比以前在 4.1 上要慢。这种 bindView 方法在 4.1 上非常快速,在新的 4.2 上我看到了小故障。我曾尝试在 中显式启用硬件加速AndroidManifest.xml
,但这并没有多大帮助。People (Contacts) 应用程序在 4.2 上完美运行,我猜这也是由 extended 实现的android.support.v4.widget.CursorAdapter
。我也已经尝试在没有明显改善的情况下进行android.support.v4.widget.CursorAdapter
更改android.widget.CursorAdapter
。
@Override
public void bindView(View view, Context context, Cursor cursor) {
final ViewHolder holder = (ViewHolder) view.getTag();
holder.nameView.setText(cursor.getString(mStreetColumnIndex));
holder.townView.setText(cursor.getString(mTownColumnIndex));
if (mDistanceColumn != null) {
float distance = cursor.getFloat(mDistanceColumn);
String distanceUnit;
if (distance >= 1000) {
distance /= 1000;
distanceUnit = " km";
} else {
distanceUnit = " m";
}
String decimalString = mDecimalFormat.format(distance);
holder.distanceView.setText(decimalString + distanceUnit);
holder.distanceView.setVisibility(View.VISIBLE);
} else {
holder.distanceView.setVisibility(View.GONE);
}
// read only brand first letter to be faster
cursor.copyStringToBuffer(mBrandColumnIndex, mBuffer);
if (mBuffer.sizeCopied > 0) {
if (mBuffer.data[0] == 'a') {
holder.logoImgView.setImageResource(R.drawable.agip);
} else {
holder.logoImgView.setImageResource(R.drawable.papoil);
}
} else {
holder.logoImgView.setVisibility(View.INVISIBLE);
}
}
编辑:添加行布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="@dimen/row_height"
android:background="@drawable/bg_list_selector"
android:layout_marginRight="@dimen/screen_padding"
android:layout_marginLeft="@dimen/screen_padding"
android:paddingLeft="@dimen/screen_padding">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_marginRight="@dimen/screen_padding"
android:layout_toLeftOf="@+id/txtDistance"
android:orientation="vertical"
>
<TextView
android:id="@+id/txtName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="@dimen/list_text_primary_size"
android:textStyle="bold"
/>
<TextView
android:id="@+id/txtTown"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="@dimen/list_text_primary_size"
/>
</LinearLayout>
<TextView
android:id="@+id/txtDistance"
android:layout_toLeftOf="@+id/imgLogo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:textColor="@color/grey_dark"
android:textSize="13sp"
android:textStyle="bold"
android:layout_marginRight="@dimen/screen_padding"/>
<ImageView
android:id="@+id/imgLogo"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/agip"
android:background="@color/grey_dark"
/>
</RelativeLayout>
编辑 2:这是使用 http://goo.gl/Yoe1u 制作的traceview http://goo.gl/UmS3w