I have a FragmentA(with Recyclerview) and a FragmentB(item). When I click in A to jump to B, the page lags a bit, maybe I updated the content in the main thread? But I don't seem to be doing that.
In FragmentB, I have some text and 3 Recyclerview, maybe the recyclerview cause the lag?
May be I need to load the data after the transition animation is complete? How to finish it?
It can be because of many things. Check if you are using a large image size that can use more power and time to render. Try to move your long operations into coroutines.
As your layouts has one image, 3 recycler view & other stuff there is many cases for layout lagging.
May the image you are loading is larger. (Try: load it with Glide or other image loading libraries)
2.May your 3 recycler listing has large number of items & you are loading them immediately after creating your fragment view. (Try: put them into the runnanble & load them after some delay of a time)
Related
I have multiple elements in my recyclerview's row -> By multiple I mean multiple in number as well as multiple in type i.e mutliple imageviews, textviews etc.
Following is the gist so that you can see all the elements: https://gist.github.com/Harshhb101/55e25da72e3a474aeeb422d5e231d3e3
The issue is that I need to hide/show these elements based on a parameter which can have upto 10 values. Thus I will have 10 types of rows. Currently I have created only one layout for the row have elements for all types of rows and in some mobiles, the scroll has a lag. Majorly I am getting the lag where the rows have images. I am using Glide to load the images. Following is the gist for the onBindView: https://gist.github.com/Harshhb101/e10feb2cccda9d698ff06487bbb879ef
I did look up on stackoverflow but could not find anything solid but came upon using multiple viewholders. My question is that if I refactor my code, will it make a substantial difference by using multiple viewholders? Or is there something wrong in my approach that could be fixed to get a good scoll.
You need to make your RecyclerView images smaller to save memory and cache them. Read this.
Also if your ViewHolder contains images that has size wrap_content then images are loaded full size what is really bad for performance for example if view size on screen is 48dp x 48dp and picture is full HD, then full HD drawable going to be loaded in to memory what is making your scrolling slow and not smooth.
I suggest using fixed size in ViewHolder or override image size when loading in Glide
Please declare many view type instead of one view type for many kind. It's actually make the code more readability, but not much the reduce the lag because the recycle view/listview is have the reused mechanism.
Recommended to use fixed size in RecycleView.
I saw the Glide used for image loading. So did you config the cache?
I have doubt that you loading the big resolution image
After looking into the code i found couple of improvements.
Don't make God ViewType (One view type for all kind of views). instead create
individual viewtype for each kind of posts. so it'll eliminate the rendering cost of all the views and making them visible and invisble at runtime.
Reference demo of recyclerview with multiple viewtypes
Use some image loading libraries so that you dont have to handle caching images and bitmap related manipulations (Picasso will be good).
Picasso
And lastly avoid doing heavy tasks in onbind() instead prepare your data class specifically for recyclerview (if much caluclation required like heavy string manipulations etc) before setting recyclerview adapter.
I have two fragments on the same screen. One is changing image after every 3 seconds and the other is showing a text with a horizontal animation (from left to right). The image fragment is setting the image from external memory using the method setImageURI.
The problem occurs when a new image is loaded in the view, animated text on the other fragment skips its frames and a kind of glitch appears on that fragment interface.
I have tried changing the image on a Runnable thread as well but that didnt work as well.
Can anyone suggest something better?
And also....can the problem be resolved using a device with a better RAM?
In my app I'm displaying images on a grid view. In some cases there are a a lot of images on a single grid view (up to 1,000). This is driven off user data and is unavoidable.
It's impractical to load all images in memory. I tried that and was hitting memory errors fairly quickly. So to avoid this I create AsyncTasks in the getView() method of my array adaptor which load the image in the background. This works very smoothly so far.
The problem is that the set of images can change. notifyDataSetChanged() will trigger getView() to be called on every item on screen. The result is a lot of CPU time spent re-loading images which don't need to be changed.
Is there any way to detect if getView() is "reusing" a view to display the same element as it's already displaying? Alternatively is it possible to detect if an ImageView has already has a Bitmap assigned?
I have to show an image into imageview in my layout for recycleview' s row.
RecycleView use ViewHolder pattern so my code is in custom ViewHolder.
To load the images I need to use an AsynkTask that return a File in onPostExecute(). In this onPostExecute() I setup imageView with Picasso.
I tried to obtain the same file without use AsynkTask. So I call directly Picaso.with()....
In this scenario, recycleview lag so much when scrolling, but i notified that Picasso cache all images.
The first scenario with AynkTask has more than one problem. Scrolling down and up for x times with 100 row make the queue of asynktask bigger.
Now, if I open a fragment of drawer, queue of Asynkask continues to load and this selected fragment make its work so slowly.
how can I fix it?
I would suggest you to checkout Image Management Library by Facebook that is Fresco that is pretty awesome and mature as compared to other Image Loading Library.
When image in a view goes out of screen it automatically recycles the bitmap, hence releasing the memory (Making scrolling smooth).
Fresco handles all the things caching of images with 3 Tier architecture ( BITMAP_MEMORY_CACHE, ENCODED_MEMORY_CACHE and DISK_CACHE).
It also reduces OOM (Out Of Memory) issues.
I need to display a list of items each with text and various number of images.
I'm currently using a ListView with custom adapter to show these items. And for each item, I used a HorizontalScrollView with a LinearLayout in it to display the images. In the getView method of the ListView, I read the image URIs of each item and dynamically create ImageViews, then load the images asynchronously. I used a ViewHolder to hold the LinearLayout which contains all the ImageViews of each item.
The problem is, if I scroll down the ListView and scroll back, I'll lost the content of the item, which means I have to load the images again. And most of the images are too large and loads very slow. Actually on my app the screen can show only about 2 or 3 items once, so the scroll happens very frequently.
I have some ideas to improve this, but I'm not sure whether one of them will results better.
Since I'm just showing a thumbnail of each image, maybe I can save the thumbnails into a temp dir and load them dynamically, loading small images will be much faster. And I might have to clear that temp dir when it gets large.
I have at most 9 images for each item, so it might still be slow even if I cache the thumbnails and scroll frequently. And maybe I have to show the list manually instead of using ListView, so each item will not be reused, and the load will happen only once. But, the list will grow large in the future, if I preserve like 100 items in the LinearLayout my app may still crash.
Other better options...
Any advice will be helpful! Thanks!
You can use LazyLoading to display your image. You can use Universal Image Loader for this purpose. What lazyloading does is download an image once, cache it and display the image from cache during subsequent requests.