首先我为我糟糕的英语道歉。
我有一个适用于 Android 的应用程序,其中有一个产品目录活动。产品列表和图片中的数据在单独的例程中下载。Activity 的唯一职责就是加载数据产品及其各自的照片。
为了演示数据,我们创建了一个列表视图,并在代码后面添加了一个继承自 BaseAdapter 的自定义适配器类;
一些考虑。
源图像的最大尺寸为 500 像素高度或宽度。
平均最大文件大小为 45 kb。
记录数在1200-2000之间
布局/catalogo.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<GridView
android:id="@+id/gv_catalogo_gridprods"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:horizontalSpacing="5dp"
android:numColumns="3"
android:verticalSpacing="5dp"
tools:listitem="@layout/catalogo_item" >
</GridView>
</LinearLayout>
布局/catalogo.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/fundo_imagem_catalogo"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:baselineAligned="false" >
<LinearLayout
android:layout_width="50dp"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/label_codigo"
android:textSize="12sp" />
<TextView
android:id="@+id/lb_catalogoitem_codigo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/label_000"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical" >
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/label_descricao"
android:textSize="12sp" />
<TextView
android:id="@+id/lb_catalogoitem_descricao"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/label_descricao"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
<ImageView
android:id="@+id/iv_catalogoitem_imagem"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="center"
android:baselineAligned="false"
android:contentDescription="Imagem Produto"
android:src="@drawable/icone_android" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:baselineAligned="false" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical" >
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/label_codbarras"
android:textSize="12sp" />
<TextView
android:id="@+id/lb_catalogoitem_codbarras"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/label_789000000"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/label_embalagem"
android:textSize="12sp" />
<TextView
android:id="@+id/lb_catalogoitem_emb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/label_cx012"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
Adater 到 GridView
public class AdapterImagensCatalogo extends BaseAdapter {
Context contexto;
List<ItemCatalogo> catalogo;
BitmapFactory.Options optBitMap;
public AdapterImagensCatalogo(Context contexto, List<ItemCatalogo> catalogo) {
this.contexto = contexto;
this.catalogo = catalogo;
this.optBitMap = new BitmapFactory.Options();
this.optBitMap.inSampleSize = 10;
}
@Override
public int getCount() {
return catalogo.size();
}
@Override
public Object getItem(int position) {
return catalogo.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = LayoutInflater.from(contexto).inflate(R.layout.catalogo_item, null);
TextView tv = (TextView) convertView.findViewById(R.id.lb_catalogoitem_codigo);
tv.setText(catalogo.get(position).getProduto());
tv = (TextView) convertView.findViewById(R.id.lb_catalogoitem_descricao);
tv.setText(catalogo.get(position).getDescricao());
tv = (TextView) convertView.findViewById(R.id.lb_catalogoitem_codbarras);
tv.setText(catalogo.get(position).getCodigoBarras());
tv = (TextView) convertView.findViewById(R.id.lb_catalogoitem_emb);
tv.setText(catalogo.get(position).getEmbalagem());
ImageView imgV = (ImageView) convertView.findViewById(R.id.iv_catalogoitem_imagem);
imgV.setImageBitmap(getMiniaturaImagem(catalogo.get(position).getPathArquivo()));
return convertView;
}
private Bitmap getMiniaturaImagem(String path){
return BitmapFactory.decodeFile(path, optBitMap);
}
}
当用户浏览图像列表时会出现问题。尤其是当他如此快速地导航时。
这是 LogCat 错误。
11-10 21:17:06.781: E/MessageQueue-JNI(2526): java.lang.OutOfMemoryError
11-10 21:17:06.781: E/MessageQueue-JNI(2526): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:651)
11-10 21:17:06.781: E/MessageQueue-JNI(2526): at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:407)
11-10 21:17:06.781: E/MessageQueue-JNI(2526): at com.cleber.myapp.adapter.AdapterImagensCatalogo.getMiniaturaImagem(AdapterImagensCatalogo.java:69)
11-10 21:17:06.781: E/MessageQueue-JNI(2526): at com.cleber.myapp.adapter.AdapterImagensCatalogo.getView(AdapterImagensCatalogo.java:62)
11-10 21:17:06.781: E/MessageQueue-JNI(2526): at android.widget.AbsListView.obtainView(AbsListView.java:2461)
11-10 21:17:06.781: E/MessageQueue-JNI(2526): at android.widget.GridView.makeAndAddView(GridView.java:1331)
11-10 21:17:06.781: E/MessageQueue-JNI(2526): at android.widget.GridView.makeRow(GridView.java:331)
11-10 21:17:06.781: E/MessageQueue-JNI(2526): at android.widget.GridView.fillDown(GridView.java:283)
11-10 21:17:06.781: E/MessageQueue-JNI(2526): at android.widget.GridView.fillGap(GridView.java:243)
11-10 21:17:06.781: E/MessageQueue-JNI(2526): at android.widget.AbsListView.trackMotionScroll(AbsListView.java:5549)
11-10 21:17:06.781: E/MessageQueue-JNI(2526): at android.widget.AbsListView.scrollIfNeeded(AbsListView.java:3429)
11-10 21:17:06.781: E/MessageQueue-JNI(2526): at android.widget.AbsListView.onTouchEvent(AbsListView.java:3921)
11-10 21:17:06.781: E/MessageQueue-JNI(2526): at android.view.View.dispatchTouchEvent(View.java:7392)
11-10 21:17:06.781: E/MessageQueue-JNI(2526): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2229)
11-10 21:17:06.781: E/MessageQueue-JNI(2526): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1964)
11-10 21:17:06.781: E/MessageQueue-JNI(2526): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2235)
11-10 21:17:06.781: E/MessageQueue-JNI(2526): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1978)
11-10 21:17:06.781: E/MessageQueue-JNI(2526): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2235)
11-10 21:17:06.781: E/MessageQueue-JNI(2526): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1978)
11-10 21:17:06.781: E/MessageQueue-JNI(2526): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2235)
11-10 21:17:06.781: E/MessageQueue-JNI(2526): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1978)
11-10 21:17:06.781: E/MessageQueue-JNI(2526): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2235)
11-10 21:17:06.781: E/MessageQueue-JNI(2526): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1978)
11-10 21:17:06.781: E/MessageQueue-JNI(2526): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2177)
11-10 21:17:06.781: E/MessageQueue-JNI(2526): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1482)
11-10 21:17:06.781: E/MessageQueue-JNI(2526): at android.app.Activity.dispatchTouchEvent(Activity.java:2483)
11-10 21:17:06.781: E/MessageQueue-JNI(2526): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2125)
11-10 21:17:06.781: E/MessageQueue-JNI(2526): at android.view.View.dispatchPointerEvent(View.java:7577)
11-10 21:17:06.781: E/MessageQueue-JNI(2526): at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3376)
11-10 21:17:06.781: E/MessageQueue-JNI(2526): at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3308)
11-10 21:17:06.781: E/MessageQueue-JNI(2526): at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4421)
11-10 21:17:06.781: E/MessageQueue-JNI(2526): at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4399)
11-10 21:17:06.781: E/MessageQueue-JNI(2526): at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4505)
11-10 21:17:06.781: E/MessageQueue-JNI(2526): at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:178)
11-10 21:17:06.781: E/MessageQueue-JNI(2526): at android.os.MessageQueue.nativePollOnce(Native Method)
11-10 21:17:06.781: E/MessageQueue-JNI(2526): at android.os.MessageQueue.next(MessageQueue.java:125)
11-10 21:17:06.781: E/MessageQueue-JNI(2526): at android.os.Looper.loop(Looper.java:124)
11-10 21:17:06.781: E/MessageQueue-JNI(2526): at android.app.ActivityThread.main(ActivityThread.java:4949)
11-10 21:17:06.781: E/MessageQueue-JNI(2526): at java.lang.reflect.Method.invokeNative(Native Method)
11-10 21:17:06.781: E/MessageQueue-JNI(2526): at java.lang.reflect.Method.invoke(Method.java:511)
11-10 21:17:06.781: E/MessageQueue-JNI(2526): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1043)
11-10 21:17:06.781: E/MessageQueue-JNI(2526): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:810)
11-10 21:17:06.781: E/MessageQueue-JNI(2526): at dalvik.system.NativeStart.main(Native Method)
11-10 21:17:06.781: D/AndroidRuntime(2526): Shutting down VM
11-10 21:17:06.781: W/dalvikvm(2526): threadid=1: thread exiting with uncaught exception (group=0x41a892a0)
11-10 21:17:06.781: E/AndroidRuntime(2526): FATAL EXCEPTION: main
11-10 21:17:06.781: E/AndroidRuntime(2526): java.lang.OutOfMemoryError
11-10 21:17:06.781: E/AndroidRuntime(2526): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:651)
11-10 21:17:06.781: E/AndroidRuntime(2526): at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:407)
11-10 21:17:06.781: E/AndroidRuntime(2526): at com.cleber.myapp.adapter.AdapterImagensCatalogo.getMiniaturaImagem(AdapterImagensCatalogo.java:69)
11-10 21:17:06.781: E/AndroidRuntime(2526): at com.cleber.myapp.adapter.AdapterImagensCatalogo.getView(AdapterImagensCatalogo.java:62)
11-10 21:17:06.781: E/AndroidRuntime(2526): at android.widget.AbsListView.obtainView(AbsListView.java:2461)
11-10 21:17:06.781: E/AndroidRuntime(2526): at android.widget.GridView.makeAndAddView(GridView.java:1331)
11-10 21:17:06.781: E/AndroidRuntime(2526): at android.widget.GridView.makeRow(GridView.java:331)
11-10 21:17:06.781: E/AndroidRuntime(2526): at android.widget.GridView.fillDown(GridView.java:283)
11-10 21:17:06.781: E/AndroidRuntime(2526): at android.widget.GridView.fillGap(GridView.java:243)
11-10 21:17:06.781: E/AndroidRuntime(2526): at android.widget.AbsListView.trackMotionScroll(AbsListView.java:5549)
11-10 21:17:06.781: E/AndroidRuntime(2526): at android.widget.AbsListView.scrollIfNeeded(AbsListView.java:3429)
11-10 21:17:06.781: E/AndroidRuntime(2526): at android.widget.AbsListView.onTouchEvent(AbsListView.java:3921)
11-10 21:17:06.781: E/AndroidRuntime(2526): at android.view.View.dispatchTouchEvent(View.java:7392)
11-10 21:17:06.781: E/AndroidRuntime(2526): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2229)
11-10 21:17:06.781: E/AndroidRuntime(2526): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1964)
11-10 21:17:06.781: E/AndroidRuntime(2526): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2235)
11-10 21:17:06.781: E/AndroidRuntime(2526): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1978)
11-10 21:17:06.781: E/AndroidRuntime(2526): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2235)
11-10 21:17:06.781: E/AndroidRuntime(2526): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1978)
11-10 21:17:06.781: E/AndroidRuntime(2526): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2235)
11-10 21:17:06.781: E/AndroidRuntime(2526): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1978)
11-10 21:17:06.781: E/AndroidRuntime(2526): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2235)
11-10 21:17:06.781: E/AndroidRuntime(2526): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1978)
11-10 21:17:06.781: E/AndroidRuntime(2526): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2177)
11-10 21:17:06.781: E/AndroidRuntime(2526): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1482)
11-10 21:17:06.781: E/AndroidRuntime(2526): at android.app.Activity.dispatchTouchEvent(Activity.java:2483)
11-10 21:17:06.781: E/AndroidRuntime(2526): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2125)
11-10 21:17:06.781: E/AndroidRuntime(2526): at android.view.View.dispatchPointerEvent(View.java:7577)
11-10 21:17:06.781: E/AndroidRuntime(2526): at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3376)
11-10 21:17:06.781: E/AndroidRuntime(2526): at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3308)
11-10 21:17:06.781: E/AndroidRuntime(2526): at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4421)
11-10 21:17:06.781: E/AndroidRuntime(2526): at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4399)
11-10 21:17:06.781: E/AndroidRuntime(2526): at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4505)
11-10 21:17:06.781: E/AndroidRuntime(2526): at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:178)
11-10 21:17:06.781: E/AndroidRuntime(2526): at android.os.MessageQueue.nativePollOnce(Native Method)
11-10 21:17:06.781: E/AndroidRuntime(2526): at android.os.MessageQueue.next(MessageQueue.java:125)
11-10 21:17:06.781: E/AndroidRuntime(2526): at android.os.Looper.loop(Looper.java:124)
11-10 21:17:06.781: E/AndroidRuntime(2526): at android.app.ActivityThread.main(ActivityThread.java:4949)
11-10 21:17:06.781: E/AndroidRuntime(2526): at java.lang.reflect.Method.invokeNative(Native Method)
11-10 21:17:06.781: E/AndroidRuntime(2526): at java.lang.reflect.Method.invoke(Method.java:511)
11-10 21:17:06.781: E/AndroidRuntime(2526): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1043)
11-10 21:17:06.781: E/AndroidRuntime(2526): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:810)
11-10 21:17:06.781: E/AndroidRuntime(2526): at dalvik.system.NativeStart.main(Native Method)
我试图用stackoverflow的以下discuçoes来解决这个问题,但是没有成功。
https://github.com/thest1/LazyList