Many a time we face a problem where due to large number of images the listview is not srolling smoothly. If you are also facing the same problem than, Instead of using ImageView Use this class as your Imageview in your ListView adapter.
public class CustomImageView extends ImageView {
private boolean mBlockLayout;
public CustomImageView (Context context) {
super(context);
init();
}
public CustomImageView (Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public CustomImageView (Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
@Override
public void requestLayout() {
if (!mBlockLayout) {
super.requestLayout();
}
}
@Override
public void setImageResource(int resId) {
mBlockLayout = true;
super.setImageResource(resId);
mBlockLayout = false;
}
@Override
public void setImageURI(Uri uri) {
mBlockLayout = true;
super.setImageURI(uri);
mBlockLayout = false;
}
@Override
public void setImageDrawable(Drawable drawable) {
mBlockLayout = true;
super.setImageDrawable(drawable);
mBlockLayout = false;
}
@Override
public void setImageBitmap(Bitmap bm) {
mBlockLayout = true;
super.setImageBitmap(bm);
mBlockLayout = false;
}
}
When you scroll your list, you can notice some lag when images are displayed. This is due to the setImageDrawable(Drawable d) which can call a requestLayout (which can take some time which means delay in scrolling).There is a way to block the requestLayout,
Use this CustomImageView in place of imageView where ever you want in your ListView Adapter .
This will block requestLayout on scroll ,which means makes your listview scroll smoothly.
public class CustomImageView extends ImageView {
private boolean mBlockLayout;
public CustomImageView (Context context) {
super(context);
init();
}
public CustomImageView (Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public CustomImageView (Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
@Override
public void requestLayout() {
if (!mBlockLayout) {
super.requestLayout();
}
}
@Override
public void setImageResource(int resId) {
mBlockLayout = true;
super.setImageResource(resId);
mBlockLayout = false;
}
@Override
public void setImageURI(Uri uri) {
mBlockLayout = true;
super.setImageURI(uri);
mBlockLayout = false;
}
@Override
public void setImageDrawable(Drawable drawable) {
mBlockLayout = true;
super.setImageDrawable(drawable);
mBlockLayout = false;
}
@Override
public void setImageBitmap(Bitmap bm) {
mBlockLayout = true;
super.setImageBitmap(bm);
mBlockLayout = false;
}
}
When you scroll your list, you can notice some lag when images are displayed. This is due to the setImageDrawable(Drawable d) which can call a requestLayout (which can take some time which means delay in scrolling).There is a way to block the requestLayout,
Use this CustomImageView in place of imageView where ever you want in your ListView Adapter .
This will block requestLayout on scroll ,which means makes your listview scroll smoothly.
0 comments:
Post a Comment