How to cache recycler view items once loaded - android

I am creating a Xamarin.Android application using MvvmCross framework.
I am currently using RecyclerViews to show a list of data and images, but once the item is out of the frame and loaded again it also loads the images.
I don't want that to happen, I want the images to be cached once loaded.
If anyone has any idea about how can I acheive that? I will thankful.

Related

Android ListView with Custom Adapter, slow loading

I have a Custom Adapter for the ListView. The Layout has three images, some text. When loading the Listview, it takes a while, because of the images. Its something like Posts.
Same app in iOS is loading very fast, I think that UITableView works different than Listview.
Is there a way, in place to load all posts, only load for example 3 posts and when the user scrolls the Listview down, load the next 3, scroll down, load the next 3 and so on. This could give a better performance.
Normally, android listview work that way. Let's say there are 5 views that user can reach at the moment. ListView creates 9 views and when user scrolls it loads the bottom ones. You can think it this way. Your main problem is how are you loading your images and create the custom view. There is a common pattern for custom adapters which handles the fast recycling views(ViewHolder pattern). You should checkout the link for ViewHolder pattern. https://www.javacodegeeks.com/2013/09/android-viewholder-pattern-example.html
It probably loads slow because the images should be resized every time, if you could save thumbnails it would go much faster.
Have you tried to use Recycler View instead? As long as I know using the Recycler View is the best practice nowadays. You can find a very good tutorial here:
http://www.vogella.com/tutorials/AndroidRecyclerView/article.html
You use the Recycler View almost the same way you do with List View: adapter, viewHolder, etc. It's good to mention though that you need to pay special attention to the use of the LayoutManager since the Recycler View it itself doesn't "know how to draw" the stuff on the screen.

Android Webview Performance improvement by caching

I am working on and Android app which has legacy code and we are using ListView and optimizing the listview by using ViewHolder.
Our listview has different types of custom views which are a bit complext. The scrolling performance of the listview is pretty good till now.
A new requirement has come up to support Webviews in the same listview. The webview content to be loaded from disk and javascript is enabled.
The problem is that when I add webviews in list the scrolling become jittery. During the scrolling I can see that the webview content is blank as it is still to be loaded.
I think the performance degradation is due to two reasons.
1) creation+Loading of new Webviews everytime getView of listview adapter is called
2) Displaying the content of webview
To solve these issues I started caching 7 webviews and I am recycling the webviews by detaching from previous parent view and attaching to new parent view. For example if the current visible item index is 5. I will also create the webview for three next(6,7,8) and three prev(2,3,4) items and keep them in a cache based on some id of listview item content. So when there is requirement for next item say for index 6, I will simply return one item from cache and update the both from front and back to hold 3,4,5 and 7,8,9 index items.
I was thinking that by creating a pool of webviews I will solve both the problems. 1) creating new webview ever time getview is called. And by prefetching the next/prev 3-3 items I can preload url so display will happen in background.
But I can see that display is still taking time and scrolling is still jittery.
So my questions are:-
Does webview display the content only when it is attach to a window ? (In my case the webviews in caches are not attached to window and I am loading the url in them.)
Does webview display the content only when it is visible ?
Is there a way to render the content of webview in background so that by creating cache of Webview I can do smooth scrolling.

Load asynchronous images in listView only on scroll

So I've got this ListView and its adapter. It's using lazy loading of images with a transition effect from previous image to new.
It works great, but I have a problem and it's that my only reason to use a lazy load is to make scroll smooth. If I need to resort the listview I don't want images to lazy load as the transition is completely unnecessary.
I really don't know how to know inside the adapter whether the user is scrolling or the listview has been resorted. Any help is welcome, I found similar questions but none addressed this issue.
The concept of LazyLoading is that it loads the images for the first transition only and the images are cached. If the user again scrolls or if the ListView restores, it never downloads it again, images are taken from the local cache present on the device.
There are many Libraries that provide this feature :
Lazy List
Universal Loader
Volley
Picasso
Tutorial

Out of memory error when nesting gridview inside listview

I'm trying to add a GridView of images (ImageView) for my android application. I've successfully implemented lazy loading and caching of images and all of my high quality images work flawlessly when I have a simple GridView. However, the problem occurs when I try to nest my GridView inside a ListView.
When I simply have a GridView, the app only has to keep reference to images that are currently being rendered on the screen. But when I nest a GridView of, say 100 hi-res images, inside a ListView, the app tries to keep a reference to all 100 images, when only 20 of them are being rendered on the screen at the time. I found this out through my Log output, where the app would print a message every time an image is "read". I would see the app read 20 images that are being displayed on the screen, but then it would keep going and read more images. When it reaches around 100~ images, the app would run out of memory and crash. This behavior does not happen when I have just the GridView; the app will just read 20 images, and load more as I scroll the screen.
How can I make it so that my ListView does not try to load all elements inside a child view (especially the one that's not being displayed on screen)?
Thanks!
EDIT:
For those wondering why I would want a GridView inside a ListView, it's because I'm trying to create something like this:
Except I'd like to have it in a ListView, as opposed to ExpandableListView.
You are getting this Exception because in an expanded GridView all the data is loaded in memory, if your data is too much it will cause OutOfMemory Exception.The solution is: divide your data in small parts and then try.
You can not nest scrollable Views in Android. Components such as ListView, GridView, ScrollView can't be nested into each other.
You should take a look at Fragments (http://developer.android.com/guide/components/fragments.html) and stop to re-think your UI a little bit.
And... for large datasets, image/data lazy-loading is not enough... it is highly recommended that you implement a paging mechanism that would allow the user to page trough the results/content.
Let's say you'll display 20 items at a time. You will have to add a Next and a Previous buttons on the sides of the GridView UI (or you can do it the cool way with gestures)... and tweak your GridView's Adapter logic to pull only 20 items from your Model or from the Server at a time.
I found a really simple implementation of paging with a grid view among my bookmarks: http://paulononaka.wordpress.com/2011/03/19/how-to-implement-a-paging-feature-in-android/ ... I bet there are better ways to do it, but it could give you ideas.
Good luck!

Loading pictures in Android

What is the best practice for loading images from the Web into a ListView?
Do I have to load them first into SQLite database or should I load them directly into a ListView?
And also after clicking an item in the ListView I have to show the same image in another activity. Is it the right way to pass this image to another activity or it is better to load it from Web / SQLite once again?
Look here Lazy loading of images
Loading them directly into the listview will make scrolling in it slow, as it will reload them everytime they get visible.
do that Google recommended http://developer.android.com/training/displaying-bitmaps/index.html

Categories

Resources