How to avoid flickering when updating gridview?

I have a gridview. Im displays images from an array of 10 images. In 1 minute Iโ€™ll add another 5 images. To update gridview, I use the following code.

aImgAdapterL.notifyDataSetChanged(); 

aImgAdapterL is my ImgaeAdapter. New images will appear.
My problem is updating the gridview, flickering or blinking, occurring during image refresh. Can this flicker be hidden?

+7
source share
9 answers

I had the same problem and I was able to solve it like this:

In fact, this was due to my ListAdapter , which did not cope properly with when the whole list was updated. If so, then you want to save the elements that are already displayed on the screen as is.

To do this, in the adapterโ€™s getView method, when you get the recycled item, you need to check if it is the one you want to display. If so, just return it directly.

 @Override public View getView(int position, View convertView, ViewGroup container) { ImageView imageView; String src = this.getItem(position).getSrcThumbnail(); if (convertView == null) { imageView = new ImageView(getContext()); imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); int height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 120, getResources().getDisplayMetrics()); imageView.setLayoutParams(new GridView.LayoutParams(GridView.LayoutParams.MATCH_PARENT, height)); } else { imageView = (ImageView) convertView; // When you get a recycled item, check if it not already the one you want to display. String newSrc = (String) imageView.getTag(); if(newSrc.equals(src)){ // If so, return it directly. return imageView; } } loadBitmap(this.getItem(position).getSrcThumbnail(), imageView); // Here you set the unique tag that allow to identify this item. imageView.setTag(src); return imageView; } 
+6
source

If your adapter has stable identifiers, override hasStableIds to return true .

Grep android.GridView source code is here , it allows the grid to view reuse of the view for the same data, and not completely redraw it. This may help with the OP, because by default hasStableIds returns false .

+5
source

The flicker is due to the fact that updating the user interface is difficult. Please check that you are doing intensive processor functions, such as cropping the image in the getview () adapter. The idea is to simply save the getview () function and make the logic intensive process in separate threads.

+1
source

The solution is to not reload the image when it has not changed.

In your getView () do adapters:

 // schedule rendering: final String path = ... (get path here); if (holder.lastImageUrl == null || !holder.lastImageUrl.equals(path) || holder.headerImageView.getDrawable() == null) { // refresh image imageLoader.displayImage(imageUri, imageAware); } else { // do nothing, image did not change and does not need to be updated } 

If it succeeds (add ImageLoadingListener), you set the holder.lastImageUrl = path file, if it fails, cancel the installation of holder.lastImageUrl to null so that it reboots next time.

+1
source

Use the nostra13 Universal Image Loader to display your images, set some animations to show them.

 ...DisplayImageOptions.displayer(FadeInBitmapDisplayer)... 
0
source

here I wrote a detailed example of how to use the viewer template:

With this approach, you can avoid flickering.

0
source

Pass ImageViewAware (instead of ImageView) that does not take into account the actual size of the view: Intead of:

 imageLoader.displayImage(imageUri, imageView); 

follow these steps:

 ImageAware imageAware = new ImageViewAware(imageView, false) imageLoader.displayImage(imageUri, imageAware); 
0
source

A simple solution is 1) Have a method in your adapter that will add / reset your new data list to the old list of arrays and not call notifyDataSetChanged() When the user scrolls, getview will be called and therefore the list will be updated. It should not flicker.

eg. // Have a method in adapter class public void updateList(ArrayList<ABC> list) { / enter the code here '/ add to old one oldList.addAll (list); }

thats it `

0
source

You must enable OpenGL and hardware acceleration:

 <application android:hardwareAccelerated="true" ...> <uses-feature android:name="string" android:required=["true" | "false"] android:glEsVersion="integer" /> 

You can also use hardware caching in the elements of your gridview:

 view.setLayerType(View.LAYER_TYPE_SOFTWARE, null); 

Finally, you can overlay the grid view during the update and play with transparency.

0
source

All Articles