I want a Viewpager that shows loading while content is coming in from the background. Basically I expect the first View to be loaded, but View+1 and View-1 will still be loading. If the user swipes to either side I want them to be presented with a spinning dialog while it loads
Would I just add AsyncTasks into the ViewPager with some conditions determining when they will run? I dont want too many AsyncTasks to be loading as the viewpager will have many views off to the sides.
I think the Trulia app does this, it is similar to what I am looking for. Apartment image viewing shows a loading screen while the images are loading in that viewpage.
Also for the record, can I just treat viewpagers like onCreate functions of an activity? That would really clear things up
Insight appreciated
Have a look at the supplied FragmentPagerAdapter if you want to perform more Activity-like lifecycle management of each page.
Related
The images in my fragment are slow to show? or load?
I have a constraint layout that displays up to about 40 various ImageViews. All videos are shown simultaneously. When I navigate to the fragment it can take up to a second or two for the navigation to end.
The images are in my project as drawables and programmatically found by their drawable id and displayed programmatically, not in a list.
How can I do this better for a better experience?
While using RecyclerView with GridLayoutManager would make your life really easy, If you are really not willing then here's a few suggestions:
I am assuming by referring to time taken for navigation you are saying that the actual transition from a fragment/activity to this particular fragment happens really slowly. So for most of the time you are staring at a blank screen.
i) first, place some blank placeholder images to your ImageViews. So that the user at least knows that the app hasn't stopped responding.
ii) secondly, if you are using any of the onCreate(), onCreateView(), onViewCreated() methods to initialize your ImageViews programmatically, I suggest you move this code to onStart() method of your fragment.
iii) lastly, use Glide library to asynchronously load your ImageViews with the images.
I am creating an app which will have multiple 'grids' containing a title and image thumbnails in each grid square.
Each Grid will have different content stored in it.
I have so far created one activity that initialises an instance of GridView, and uses a custom GridAdapter. (See photo for what it currently looks like) I was planning to swipe left to create a new empty grid in which the user can upload content. There may be anywhere up to 50 grids.
I'm just learning how to implement the gesture, and how to create a new instance of the activity, but from what I've read, I am thinking I have designed it badly.
I was planning for each grid to be an Activity (each takes up the full screen).
I envisaged an Activity as being like a Class in java that you can create instances from a blueprint. I thought if I created one 'Grid' I could create a new instance of it each time. Fragments didn't seem appropriate at the time, as the android tutorials often described them as being purposed to add components to activities.
I'm starting to think though that I am using the wrong methodology here and I need to change it? Can someone guide me in the right direction? I have written all the code already - if I need to change it, do Fragments and Activities share any methods, meaning I can retain some work?
Like you mentioned, using activities for holding content in your use case where switching may be triggered using gestures will definitely be resource heavy and cumbersome. Since, you mentioned swipe gestures, I believe fragments would be much lightweight in this situation. In fact, I would suggest you even look at ViewPager which even recycles fragments for you and optimizes user experience by loading the next fragment for a smoother experience. It will also handle swipe gestures for you!
[UPDATE]
Based on your updated explanation of the user flow, I'm certain that the ViewPager would fare as a better option mainly because it allows for a much better control and user navigation. It will also take care of handling swipe gestures and memory issues that come with these types of flows. Moreover, it will even allow for a page titles and bottom tab indicators in case you need them.
It will require each of its pages to be a fragment (your ViewPager will itself reside in an Activity). Once the user clicks on a grid cell, you can show a dialog window from where user input can be captured. This setup should be optimal for you resource wise in my opinion.
APPLICATION IDEA: Idea is simple. When application is launched it will connect to web service which will return number of rows in particular column in database, then it will retrive data from first row and display in activity. When user interacts with its finger, left or right (e.g swiping through pages), it needs to load next (or previous) row from database.
Please check following picture you may understand better.
PROBLEM: Since I'm new in Android I would like to know which scenario best fits my needs. I actually need to load data from database when user swipes and show this on my current screen.
WHAT I'VE TRIED: I already made my "homework" and I searched google for my problem. I used implementation of ViewPager class with PagerAdapter class. But when I swipe on the next page nothing is showed ( Yes I used AsynTask and I sucessfully pulled data to my device), but instead is showed on the NEXT page.
So I need to know if ViewPager is really what I need? Do I need to use ViewPager together with FragmentPagerAdapter so that my text will load exactly when my page is showed?
ViewPager suits this scenario well.
Note that ViewPager by default loads the next and previous pages while the current page is being shown (off-screen page limit 1). Your adapter likely has some bugs that make it pull data for the wrong page. To get help with them, please post a more specific question.
Is this possible to preload Fragments for ViewPager so that there's no lag when user swipes to next page? I've been trying two kinds of adapters but there's no use in switching from Fragmentstatepageradapter (I am using fragments to generate N pages) to fragmentpageadapter. I've got a lot of bitmaps to load in my pages. Setting viewpager offscreen pages doesn't help. Is there a way to preload them and inflate in backgdound? I know this is a silly question but I am a bit desperate about performance. Basically in my app when user scrolls to next page there is kind of lag, just between pages, and it's so annoying... Please help or point the right direction. I am targeting Gingerbread so I need to use that what Android Support gives.
Is this possible to preload Fragments for ViewPager so that there's no lag when user swipes to next page?
Sure. Have your adapter preload the fragments. You are responsible for returning fragments in getItem() -- whether those are "preloaded" or not is your job.
That being said, your problem most likely is not with the fragments themselves, but something that the fragments are doing. For example, you "got a lot of bitmaps to load in my pages", and if you are doing work related to those on the main application thread (e.g., using BitmapFactory), you are causing your own lag.
So, instead of racing off to "preload" stuff, a talented programmer would find out specifically what is causing the lag, using tools like Traceview. Then, and only then, would a talented programmer start working on a fix.
I'm using FragmentPagerAdapter with a tabbed interface. My interface happens to have three tabs. I can see that as soon as my app is created, the adapter requests that both tabs one and two both be created immediately (I assume this is so swiping to the next tab appears smooth to the user).
Is there a way to stop the automatic loading of the next tab? My tabs are pretty heavy, and loading two right at startup is taking a good deal of time,
Thanks
Is there a way to stop the automatic loading of the next tab?
Sorry, no. This is driven by setOffscreenPageLimit(), and the minimum value is 1, meaning that ViewPager will always try to create and hold onto at least 1 page on each side.
It sounds like you need to move more logic into background threads.