如何改善gridView性能?滚动并不顺利。
这是我的代码:
public View getView(final int position, View convertView, ViewGroup parent) {
View MyView;
// Inflate the layout
LayoutInflater li = ((Activity) MyContext).getLayoutInflater();
MyView = li.inflate(R.layout.gallery_adapter, null);
ImageButton imageFolders = (ImageButton) MyView.findViewById(R.id.folder);
try {
imageFolders.setImageBitmap(bMap);
imageFolders.setOnClickListener(this);
imageFolders.setId(products.get(position).getId());
} catch (Exception e) {
}
MyView.setLayoutParams(70, 70);
return MyView;
}
我在这个gridView上有200个图像,但性能非常非常糟糕。
答案 0 :(得分:3)
您可以像这样修改代码:
public View getView(final int position, View convertView, ViewGroup parent) {
View MyView;
if(convertView==null)
{
// Inflate the layout
LayoutInflater li = ((Activity) MyContext).getLayoutInflater();
MyView = li.inflate(R.layout.gallery_adapter, null);
ImageButton imageFolders = (ImageButton) MyView.findViewById(R.id.folder);
try {
imageFolders.setImageBitmap(bMap);
imageFolders.setOnClickListener(this);
imageFolders.setId(products.get(position).getId());
} catch (Exception e) {
}
MyView.setLayoutParams(70, 70);
}
else
MyView = convertView;
return MyView;
}
已加载的图像将不会重新加载。你尝试过这种可能吗?
答案 1 :(得分:0)
public View getView(final int position, View convertView, ViewGroup parent){
View MyView;
ImageButton imageFolders;
if(convertView==null){
LayoutInflater li = ((Activity) MyContext).getLayoutInflater();
MyView = li.inflate(R.layout.gallery_adapter, null);
}else{
MyView = convertView;
}
imageFolders = (ImageButton) MyView.findViewById(R.id.folder);
try {
imageFolders.setImageBitmap(bMap);
imageFolders.setOnClickListener(this);
imageFolders.setId(products.get(position).getId());
} catch (Exception e) {}
MyView.setLayoutParams(70, 70);
return MyView;
}
我没有测试它,但试一试
答案 2 :(得分:0)
在L2
中执行textView.setText("text");
之类的所有布局修改,对我来说非常有效。
此外,如果您需要在if(convertView == null)
阻止后立即致电super.getView();
。
if(convertView == null)
部分else
。
答案 3 :(得分:0)
答案 4 :(得分:0)
最好的方法是:
在CustomAdapter的构造函数中初始化上下文,LayoutInflayer和位图(或Drawables)
private Context ctx;
private int resource;
private Bitmap[] bmps;
private LayoutInflater inflater;
MyCustomAdapter(Context ctx, Bitmap[] bmps) {
this.ctx = ctx;
this.bmps = bmps;
this.resource = R.layout.gallery_adapter;
this.inflater = LayoutInflater.from(ctx);
}
如果不为null,请回收您的convertView
public View getView(final int position, View convertView, ViewGroup parent) {
final ImageButton imgBtn;
// Recycle convertView
if (convertView == null) {
imgBtn = (ImageButton) inflater.inflate(resource, null, false);
} else {
imgBtn = (ImageButton) convertView;
}
// Set Attributes
imgBtn.setImageBitmap(bmps[position]);
// imgBtn.setLayoutParams(70, 70); // Remove this and put it in your xml resource
...
return imgBtn;
此代码假定您的xml资源(R.layout.myImageButton)如下:
<?xml version="1.0" encoding="utf-8"?>
<ImageButton xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/someId"
android:layout_width="70dp"
android:layout_height="70dp"
...
/>
答案 5 :(得分:-3)
获取较低细节的图像,或获得更快的手机 要么是这样,要么以只在你开始看到图像时加载图像的方式对gridview进行编程。